ansible 拷贝文件并重启服务
Ansible 安装
[adservers]
172.20.0.70
客户端 建立一个脚本
[root@adserver4 sh]# cat /data/sh/adserver_update.sh
#!/bin/bash
pidno=`ps aux|grep adserver-beta|grep -v "grep"|awk '{print $2}'`
kill -9 $pidno
if [ $? -ne 0 ]; then
echo "old adserver kill failed"
exit 1
fi
sleep 5
adserverpid=`ps aux|grep adserver-beta|grep -v "grep"|awk '{print $2}'`
if [ "$adserverpid" ];then
echo "new version adserver is running now"
fi
建立另外一个脚本
[root@adserver4 sh]# cat /data/sh/packetanal_update.sh
#!/bin/bash
pidno=`ps aux|grep packetAnal|grep -v "grep"|awk '{print $2}'`
kill -9 $pidno
if [ $? -ne 0 ]; then
echo "old packetAnal kill failed"
exit 1
fi
sleep 5
packetpid=`ps aux|grep packetAnal|grep -v "grep"|awk '{print $2}'`
if [ "$packetpid" ];then
echo "new version packetAnal is running now"
fi
配置可执行权限
chmod +x /data/sh/*.sh
服务器上配置 playbook 的yml文件
[root@test-server28 ansible]# cat /etc/ansible/adserver_update.yml
---
- name: handlers adserver
hosts: adservers
user: root
tasks:
- name: copy file
copy: src=/etc/ansible/adserver-beta dest=/data/website/ owner=root group=root mode=0755
notify: adserver handlers
handlers:
- name: adserver handlers
shell: /bin/bash /data/sh/adserver_update.sh
[root@test-server28 ansible]# cat /etc/ansible/packetanal_update.yml
---
- name: handlers adserver
hosts: adservers
user: root
tasks:
- name: copy file
copy: src=/etc/ansible/packetAnal dest=/data/website/ owner=root group=root mode=0755
notify: packetAnal handlers
handlers:
- name: packetAnal handlers
shell: /bin/bash /data/sh/packetanal_update.sh
在客户端上删除 adserver, packetanal文件
执行ansible 分发进程
[root@test-server28 ansible]# ansible-playbook /etc/ansible/packetanal_update.yml
PLAY [handlers adserver] **************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [172.20.0.70]
TASK [copy file] **********************************************************************************************************************************************************
changed: [172.20.0.70]
RUNNING HANDLER [packetAnal handlers] *************************************************************************************************************************************
changed: [172.20.0.70]
PLAY RECAP ****************************************************************************************************************************************************************
172.20.0.70 : ok=3 changed=2 unreachable=0 failed=0
显示进程文件分发成功, 进程重新启动
[root@test-server28 ansible]# ansible-playbook /etc/ansible/adserver_update.yml
PLAY [handlers adserver] **************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [172.20.0.70]
TASK [copy file] **********************************************************************************************************************************************************
changed: [172.20.0.70]
RUNNING HANDLER [adserver handlers] ***************************************************************************************************************************************
changed: [172.20.0.70]
PLAY RECAP ****************************************************************************************************************************************************************
172.20.0.70 : ok=3 changed=2 unreachable=0 failed=0