**背景:**生产环境中偶然遇到一个问题,在使用Playbook调用supervisor进行stop进程时,虽然supervisor指令成功执行,但进程并没有结束。然后准备使用ansible原生的条件判断,但是目前并没有找到合适的。所以呢就先用笨方法自己手动实现类似于while的判断循环结构。另外引发的思考:1.程序在成功启动的情况下什么操作会导致程序的瞬间崩溃。2.运行状态下移除程序源文件对程序有何影响。3.我需要一本书《程序是如何跑起来的》哈哈。。下面写个playbook模拟下使用Ansible如何等待后台进程执行完毕
- 概览
目录结构
1 2 3 4 5 6 7 8 9
├── ansible.cfg ├── hosts ├── roles │ └── test_wait │ ├── tasks │ │ └── main.yml │ └── templates │ └── check_running_file.sh.j2 └── test_wait.yml
摘要:手动在各个机器上建/tmp/helloworld这个文件,然后执行playbook,然后手动删除这个文件,看到只有这个文件消失时才成功执行下面带when判断的指令,目标达成
- 检测脚本
1 2 3 4 5 6 7 8 9 10 11 12
[root@hosts test_ansible]# cat roles/test_wait/templates/check_running_file.sh.j2 time_out=0 dest_file='/tmp/helloworld' while [ -f "${dest_file}" ];do sleep 1 let time_out=${time_out}+1 if [ ${time_out} -ge 30 ];then echo "######Stop gamex Error Time out is ${time_out} Please Check it!!!#####" exit 0 fi done exit 0
- Playbook
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[root@hosts test_ansible]# cat roles/test_wait/templates/check_running_file.sh.j2 time_out=0 dest_file='/tmp/helloworld' while [ -f "${dest_file}" ];do sleep 1 let time_out=${time_out}+1 if [ ${time_out} -ge 30 ];then echo "######Stop gamex Error Time out is ${time_out} Please Check it!!!#####" exit 0 fi done exit 0 [root@hosts test_ansible]# cat test_wait.yml - hosts: test remote_user: ec2-user gather_facts: True become: yes roles: - test_wait