ansible在远程机器将程序起在后台,ansible后台


用&将程序起在后台,但是有部分程序却不生效,

- name: 启动8001
  shell: "redis-server redis_6379.conf 2>&1 | cronolog test_%Y%m%d%H%M%S.log &"
  async: 10  //最长等待10秒返回
  poll: 0    //值为0表示无需等待该任务返回

Ansible 有时候要执行等待时间很长的操作, 这个操作可能要持续很长时间, 设置超过ssh的timeout. 这时候你可以在step中指定async 和 poll 来实现异步操作

async 表示这个step的最长等待时长, 如果设置为0, 表示一直等待下去直到动作完成.

poll 表示检查step操作结果的间隔时长.

例1:

---
- name: Test
  hosts: localhost
  tasks:
    - name: wair for
      shell: sleep 16
      async: 10
      poll: 2

结果:
TASK: [wair for] ************************************************************** 
ok: [localhost]
<job 207388424975.101038> polling, 8s remaining
ok: [localhost]
<job 207388424975.101038> polling, 6s remaining
ok: [localhost]
<job 207388424975.101038> polling, 4s remaining
ok: [localhost]
<job 207388424975.101038> polling, 2s remaining
ok: [localhost]
<job 207388424975.101038> polling, 0s remaining
<job 207388424975.101038> FAILED on localhost

这个step失败, 因为操作时间超过了最大等待时长


例2:

---
- name: Test
  hosts: localhost
  tasks:
    - name: wair for
      shell: sleep 16
      async: 10
      poll: 0

结果:
TASK: [wair for] ************************************************************** 
<job 621720484791.102116> finished on localhost

PLAY RECAP ********************************************************************

poll 设置为0, 表示不用等待执行结果, 该step执行成功
例3:

---
- name: Test
  hosts: localhost
  tasks:
    - name: wair for
      shell: sleep 16
      async: 0
      poll: 10

结果:
# time ansible-playbook xiama.yml 
TASK: [wair for] ************************************************************** 
changed: [localhost]

PLAY RECAP ******************************************************************** 
localhost                  : ok=2    changed=1    unreachable=0    failed=0   


real    0m16.693s

async设置为0, 会一直等待直到该操作完成.

相关内容

    暂无相关文章