tomcat安装脚本:
--- - name: Tomcat install and configuration hosts: "{{ host }}" user: root vars: tomcat_home: "{{ tomcat_home }}" tasks: - name: absent old tomcat file: path={{ item }} state=absent with_items: - "{{ tomcat_home }}" - /geelyapp/auto_scripts/tomcat.sh - name: get tomcat_tar_gz get_url: url={{ url }} dest=/tmp/tomcat.tar.gz - name: Create the dir file: path={{ item }} state=directory with_items: - /geelyapp/gc_log - /geelyapp/auto_scripts - "{{ tomcat_home }}" - name: tar xzf tomcat_tar_gz unarchive: src=/tmp/tomcat.tar.gz dest={{ tomcat_home }} copy=no - name: init server.xml template: src=/ansible/roles/tomcat/templates/server.xml.j2 dest={{ tomcat_home }}/conf/server.xml - name: init setenv.sh template: src=/ansible/roles/tomcat/templates/setenv.sh.j2 dest={{ tomcat_home }}/bin/setenv.sh mode=0755 - name: init the tomcat maintain script template: src=/ansible/roles/tomcat/templates/tomcat.sh.j2 dest=/geelyapp/auto_scripts/tomcat.sh owner={{ user|default('root') }} group={{ user|default('root') }} mode=0755 - name: rename the tomcat scripts shell: mv /geelyapp/auto_scripts/tomcat.sh /geelyapp/auto_scripts/`echo "{{ tomcat_home }}" |awk -F "/" '{print $3}'`.sh - name: change owner tomcat and scripts to {{ user|default('root') }} file: path={{ tomcat_home }} owner={{ user|default('root') }} group={{ user|default('root') }} recurse=yes ignore_errors: yes
使用说明:
ansible-playbook tomcat-install.yml --extra-vars "{'host':'192.168.11.111', 'tomcat_home':'/opt/tomcat-test', 'url':'http://www.apache.com/tomcat-7.0.57.tar.gz'}"
tomcat起动:
--- - name: start the remote server app gather_facts: False hosts: "{{ host }}" tasks: - name: Clean tomcat cache. file: path={{ tomcat_home }}/work/Catalina state=absent ignore_errors: yes - name: Start tomcat service. shell: nohup {{ tomcat_home }}/bin/startup.sh & register: command_result failed_when: "'not' in command_result.stderr"
tomcat停止:
--- - name: stop the remote server app gather_facts: False hosts: "{{ host }}" tasks: - name: Stop the tomcat service action: shell ps -fe | grep {{ tomcat_home }} |grep {{ java_home|default('/usr/local/java')}} | grep -v grep | tr -s " "|cut -d" " -f2 |xargs kill -9 ignore_errors: yes
tomcat应用部署:
--- - name: deploy app to remote server gather_facts: False hosts: "{{ host }}" user: "{{ user|default('root') }}" vars: tomcat_home: "{{ tomcat_home }}" app_new: "{{ tomcat_home }}/newfile" app_bak: "{{ tomcat_home }}/oldfile" project_name: url: tasks: - name: Create the dir file: path={{ item }} state=directory with_items: - "{{ tomcat_home }}/newfile" - "{{ tomcat_home }}/oldfile" - name: Download app get_url: url={{ url }} dest={{ tomcat_home }}/newfile/{{ project_name }}.war - name: Stop the tomcat service action: shell ps -ef |grep java |grep -v grep |tr -s " " |cut -d " " -f2 |xargs kill -9 ignore_errors: yes - name: Backup the old app to /geelyapp/tomcat-{{ project_name }}/oldfile folder shell: removes={{ tomcat_home }}/webapps/{{ project_name }}.war mv {{ tomcat_home }}/webapps/{{ project_name }}.war {{ tomcat_home }}/oldfile - name: delete the old project. file: path={{ tomcat_home }}/webapps/{{ project_name }}* state=absent - name: Clean tomcat cache. file: path={{ tomcat_home }}/work/Catalina state=absent ignore_errors: yes - name: deploy the new. shell: mv {{ tomcat_home }}/newfile/{{ project_name }}.war {{ tomcat_home }}/webapps/ - name: Start tomcat service. shell: nohup {{ tomcat_home }}/bin/startup.sh & register: command_result failed_when: "'not' in command_result.stderr"
总调用脚本:
--- - include: tomcat/tasks/tomcat-install.yml tags: install - include: tomcat/tasks/tomcat-stop.yml tags: [ stop, restart ] - include: tomcat/tasks/tomcat-start.yml tags: [ start, restart ] - include: tomcat/tasks/tomcat-deployapp.yml tags: deploy
使用说明:
必填项
host: 10.86.87.112 #目标主机
tomact_home: /geelyapp/tomcat-mes #tomcat安装路径
url: http://10.86.87.142/evunsoft/tomcat.tar.gz #软件下载路径
可选项
java_home: /usr/local/java #可接收参数,自定义java路径,默认/usr/local/java
tomcat_close_port: 8005 #tomcat关闭端口,同一台服务器不能重复
tomcat_app_port: 8080 #tomcat启动端口,同一台服务器不能重复
jmx_port: 7091 #jmx监控端口,同一台服务器不能重复
jvmxms: 4096 #可接收参数,JVM初始堆内存,默认1/2主机内存
jvmxmx: 4096 #可接收参数,JVM最大堆内存,默认1/2主机内存
jvmxmn: 1024 #可接收参数,年轻代使用内存,默认使用主机内存1/4
环境变量模板:
维护脚本模板:
server.xml模板: