如何检查 PID 在 ansible 中是否为 运行,如果不是则任务失败?
How to check if PID is running in ansible and if not then fail the task?
我有 ansible 剧本,它可以做 3 件事:
a) 启动一个 flask 服务器
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'
哪个 returns PID 可以说...
8212
如何在 ansible playbook 中执行步骤 (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
我有 ansible 剧本,它可以做 3 件事:
a) 启动一个 flask 服务器
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'
哪个 returns PID 可以说...
8212
如何在 ansible playbook 中执行步骤 (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