共计 2524 个字符,预计需要花费 7 分钟才能阅读完成。
记录下用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
正文完