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模板: