ansible tomcat部署 Ubuntu环境

系统环境:

# cat /etc/issue
Ubuntu 14.04.2 LTS \n \l

# uname -m
x86_64

# ll /bin/sh
lrwxrwxrwx 1 root root 4 Dec 14 05:04 /bin/sh -> bash*

ansible hosts配置:

# grep ubuntu -B 1 /etc/ansible/hosts 
[ubuntu]
52.80.18.24 ansible_ssh_private_key_file=/etc/ansible/ssh_keys/ll_nicai.pem ansible_ssh_user=ubuntu

ansible tomcat_jdk.yml  配置系统的基础环境,创建目录,配置jdk配置tomcat。

# cat tomcat_jdk.yml 
---
- name: Tomcat deployment
  hosts: ubuntu
  remote_user: ubuntu
  become: yes
  become_method: sudo

  vars:
    Tomcat_dir: /application/tomcat-7/
    package: jenkins.war
    object: /tomcat/jenkins.war
    dest: /tmp/update/jenkins.war
    tomcat_p: /data/tomcat/jenkins.war
    Webapps_cache: /application/tomcat-7/webapps/jenkins
work_cache: /application/tomcat-7/work/Catalina/localhost/* bucket: glodw-ansible region: cn-north-1 aws_access_key: AxxxxxxxZQ aws_secret_key: U5k0xxxxxxxxxxF1XHH tasks: - name: mkdir dir shell: mkdir -p {{ item }} with_items: - '/tmp/update/' - '/data/tomcat/' - '/data/backup/' - '/application/' - name: copy tomcat jdk and tar xf unarchive: src="{{ item.regexp }}" dest="{{ item.line }}" with_items: - { regexp: '/tmp/tools/jdk-8u144-linux-x64.tar.gz', line: '/usr/local' } - { regexp: '/tmp/tools/apache-tomcat-7.0.82.tar.gz', line: '/application' } - name: link file: src=/application/apache-tomcat-7.0.82 dest=/application/tomcat-7 state=link - name: add jdk path and java path befor in '# Better' lineinfile: dest: /application/tomcat-7/bin/startup.sh insertbefore: '^# Better' line: '{{item}}' with_items: - 'JAVA_HOME=/usr/local/jdk1.8.0_144' - 'PATH=$JAVA_HOME/bin:$PATH' - 'CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' - 'TOMCAT_HOME=/application/tomcat-7' - name: copy python pip copy: src=/tmp/tools/get-pip.py dest=/tmp/ - name: python install pip shell: python /tmp/get-pip.py - name: install boto #shell: pip install awscli --upgrade --user shell: pip install boto

ansibel  tomcat更新war包的yml,备份、更换war包,重启tomcat。

# cat tomcat.yml 
---
- name: Tomcat deployment
  hosts: ubuntu
  remote_user: ubuntu
  become: yes
  become_method: sudo

  vars:
    Tomcat_dir: /application/tomcat-7/
    tomcat_name: tomcat-7
    package: jenkins.war
    object: /tomcat/jenkins.war
    dest: /tmp/update/jenkins.war
    tomcat_p: /data/tomcat/jenkins.war
    Webapps_cache: /application/tomcat-7/webapps/jenkins
    work_cache: /application/tomcat-7/work/Catalina/localhost/*
    bucket: glodw-ansible
    region: cn-north-1
    aws_access_key: AKxxxxxxxxxxxxTZQ
    aws_secret_key: U5kxxxxxxxxxxxF1XHH

  tasks:
  - name: check package
    shell: if [[ -f "{{ dest }}" ]]; then /bin/true; else /bin/false; fi
    register: result
    ignore_errors: True

  - name: "download war package"
    s3:
      bucket: "{{ bucket }}"
      object: "{{ object }}"
      dest: "{{ dest }}"
      mode: get
      region: "{{ region }}"
      overwrite: true
      aws_access_key: "{{ aws_access_key }}"
      aws_secret_key: "{{ aws_secret_key }}"
    when: result|failed

  - name: check tomcat package
    shell: if [[ -f "{{ tomcat_p }}" ]]; then /bin/true; else /bin/false; fi
    register: resu
    ignore_errors: True

  - name: cp /tmp/update/packages to /data/tomcat/package
    shell: \cp -af /tmp/update/jenkins.war /data/tomcat/
    when: resu|failed


  - name: stop tocmat
    shell: chdir={{ Tomcat_dir }}bin nohup ./shutdown.sh &

  - name: check tomcat process
    shell: if [[ `ps axu|grep {{ tomcat_name }}|wc -l` -gt 1 ]];then /bin/false;else /bin/true;fi
    register: results
    ignore_errors: True

  - name: Forced end process
    shell: nohup kill -9 `ps aux |awk '/{{ tomcat_name }}/{print $2}'` &
    when: results|failed

  - name: Backup and update war packages
    shell: \cp -af  {{ item.source}} {{ item.desc}}
    with_items:
      - { source: '/data/tomcat/jenkins.war',desc: '/data/backup/jenkins.war-"{{ansible_date_time.date}}"-"{{ansible_date_time.time}}"'}
      - { source: '/tmp/update/jenkins.war',desc: '/data/tomcat/jenkins.war'}

  - name: Delete the cache
    shell: rm -rf {{ item }}
    with_items:
      - "{{ Webapps_cache }}"
      - "{{ work_cache }}"
      - "{{ dest }}"

  - name: start tomcat
    shell: chdir={{ Tomcat_dir }}/bin nohup ./startup.sh &

 

以上自己测试环境,仅供参考。如有异议也请留言。

 

posted @ 2017-12-14 13:15  Star-Hitian  阅读(161)  评论(0)    收藏  举报