- hosts: all
vars:
ansible_ssh_user: runnuser
#配置文件
configsyaml:
#上传的本地包路径
artifacts_target:
#jar包名
finalName:
#服务器jar包路径
app_path:
gather_facts: "NO"
tasks:
- name: fetch process id
command: pgrep -a {{ finalName }}
ignore_errors: "YES"
register: pid_list
- name: kill process
command: kill {{ item.split(" ")[0] }}
loop: "{{ pid_list.stdout_lines }}"
when: pid_list.stdout|length>0
- name: wait process is closed
wait_for:
path: /proc/{{ item.split(" ")[0] }}/status
state: absent
loop: "{{ pid_list.stdout_lines }}"
when: pid_list.stdout|length>0
- name: upload file
copy:
dest: "{{ app_path }}"
src: "{{ item }}"
backup: "YES"
mode: "0700"
loop:
- {{ configsyaml }}
- {{ artifacts_target }}/{{ finalName }}
- name: mkdir log
file:
path: "{{ app_path }}/log"
state: directory
- name: launch app
command: sh -c "nohup {{ app_path }}/{{ finalName }} &>nohup.out &"
async: "300"
args:
chdir: "{{ app_path }}"
1.服务器添加免密授信登录
2.hosts写入服务器IP
ansible-play -i hosts deploy.yml