ansible playbook 安装 java jdk,tomcat,批量添加用户等案例分享
---
- hosts: webservers
become: yes
tasks:
- name: Install Java JDK
apt:
name: default-jdk
state: present
- name: Install Tomcat
apt:
name: tomcat9
state: present
- name: Add system users for Tomcat
user:
name: tomcat
system: yes
createhome: no
- name: Copy Tomcat configuration files
copy:
src: "{{ item.src }}"
dest: "{{ item.dest }}"
owner: "{{ item.owner }}"
group: "{{ item.group }}"
mode: "{{ item.mode }}"
with_items:
- { src: 'conf/tomcat-users.xml', dest: '/opt/tomcat9/conf/tomcat-users.xml', owner: 'tomcat', group: 'tomcat', mode: '0600' }
- { src: 'conf/context.xml', dest: '/opt/tomcat9/conf/context.xml', owner: 'tomcat', group: 'tomcat', mode: '0600' }
# 其他配置文件同理复制
- name: Start Tomcat service
service:
name: tomcat9
state: started
- name: Add multiple system users
user:
name: "{{ item }}"
system: yes
createhome: no
with_items:
- user1
- user2
- user3
# 添加更多用户同理
- name: Ensure directory for project exists
file:
path: /opt/tomcat9/webapps/myapp
state: directory
- name: Deploy application war file
copy:
src: myapp.war
dest: /opt/tomcat9/webapps/myapp.war
owner: tomcat
group: tomcat
mode: 0755
- name: Restart Tomcat to deploy application
service:
name: tomcat9
state: restarted
# 注意:在生产环境中,可能需要更复杂的监控和管理Tomcat的方式来确保平稳部署和稳定运行。
...
这个Ansible playbook示例展示了如何安装Java JDK、Tomcat服务器,以及如何批量添加系统用户。同时,展示了如何复制配置文件、部署应用程序war包,并且重启Tomcat服务器来使部署生效。这个示例为学习和使用Ansible提供了一个实践入口。
评论已关闭