Ansible发布jar包

598次阅读
没有评论

共计 2524 个字符,预计需要花费 7 分钟才能阅读完成。

Ansible发布jar包

记录下用ansible对java项目的发包,此处项目是springboot,只需要简单的java -jar xxxx.jar即可运行

看一下对接到jenkins中的大致playbook

---
- hosts: all
  vars:
    project_name: ""
    deploy_dir: "/usr/local/src"
    jar_path: ""
    jar_name_prefix: ""
    jar_name: ""
  tasks:
    - name: get pid
      shell: ps -ef | grep -v grep | grep {{ jar_name_prefix }} | awk '{print $2}'
      register: running_processes
    - name: kill running processes
      shell: kill {{ item }}
      with_items: "{{ running_processes.stdout_lines }}"
    - wait_for:
        path: "/proc/{{ item }}/status"
        state: absent
        timeout: 60
      with_items: "{{ running_processes.stdout_lines }}"
      ignore_errors: yes
      register: killed_processes
    - name: force kill stuck processes
      shell: "kill -9 {{ item }}"
      with_items: "{{ killed_processes.results | select('failed') | map(attribute='item') | list }}"
    - name: upload
      copy:
        src: "gs-spring-boot-0.1.0.jar"
        dest: "{{ deploy_dir }}/"
    - name: start
      shell: "cd {{ deploy_dir }} && nohup java -jar {{ jar_name }} --server.port=8081  >/dev/null 2>&1 &"

对应的运行命令

cd deploy && ansible-playbook -i hosts task.yaml -e "project_name=${project_name}" "jar_path=${jar_path}" "jar_name=${jar_name}"

对应的输出类似如下


PLAY [all] *******************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************
ok: [192.168.44.140]

TASK [get pid] ***************************************************************************************************************************************
changed: [192.168.44.140]

TASK [kill running processes] ************************************************************************************************************************
changed: [192.168.44.140] => (item=3362)

TASK [wait_for] **************************************************************************************************************************************
ok: [192.168.44.140] => (item=3362)

TASK [force kill stuck processes] ********************************************************************************************************************

TASK [upload] ****************************************************************************************************************************************
ok: [192.168.44.140]

TASK [start] *****************************************************************************************************************************************
changed: [192.168.44.140]

PLAY RECAP *******************************************************************************************************************************************
192.168.44.140             : ok=6    changed=3    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

正文完
 
xadocker
版权声明:本站原创文章,由 xadocker 2020-01-05发表,共计2524字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)