我有一本可完成3件事的剧本:
a)启动烧瓶服务器
b)等待10秒钟启动
c)检查PID是否正在运行
- name: "execute script to start flask"
shell: nohup python main.py &
args:
chdir: /home/ubuntu/flask
executable: /bin/bash
- pause: seconds=10
- name: "verify that the server has started"
command: ?????
手动执行此操作以获取PID
pgrep -f /'main.py'
哪个返回PID可以说...
8212
我如何在ansible剧本中执行步骤(c),并确保是否未找到PID,任务将失败?
我不会暂停,它会不必要地降低您的剧本的播放速度。您可以简单地使用pids
Ansible-Module和until
Pids:https://docs.ansible.com/ansible/latest/modules/pids_module.html
示例每5秒检查一次重试10次,如果main.py
的Pid不可用,则剧本将失败。
---
- hosts: localhost
tasks:
- name: check the pid of main.py
pids:
name: main.py
register: pid
until: pid.pids | length > 0
delay: 5
retries: 10
- debug: var=pid