我尝试做:
等待关机
我这样做了:
- name: "get containers"
become: yes
shell: docker ps -a | grep -v NAMES | awk '{print $NF}'
register: efluidnet_tp_containers_list
delegate_to: myserver
- name: "shutdown containers"
async: 3600
poll: 0
become: yes
docker_container:
name: "{{ item }}"
state: stopped
with_items:
- "{{ efluidnet_tp_containers_list.stdout_lines }}"
register: restart_aelnet
delegate_to: myserver
- debug:
msg: "{{ item['ansible_job_id'] }}"
with_items: "{{ restart_aelnet['results'] }}"
- name: 'waiting for shutdown'
async_status:
jid: "{{ item['ansible_job_id'] }}"
register: job_result
until: job_result.finished
retries: 30
with_items: "{{ restart_aelnet['results'] }}"
delegate_to: myserver
但是我的等待任务无法找到ansible_jod id:
TASK [get containers] *******************************************************************************************************************************************
Thursday 05 December 2019 08:17:55 +0100 (0:00:00.285) 0:00:00.366 *****
changed: [localhost -> None]
TASK [shutdown containers] **************************************************************************************************************************************
Thursday 05 December 2019 08:17:56 +0100 (0:00:00.937) 0:00:01.303 *****
changed: [localhost -> None] => (item=ael-13.20.200-PREPRO-AMN_V13)
changed: [localhost -> None] => (item=ael-13.20.200-PREPRO-SCH_V13)
changed: [localhost -> None] => (item=ael-13.20.200-PREPRO-SAR_V13)
changed: [localhost -> None] => (item=ael-13.20.200-PREPRO-ROM_V13)
changed: [localhost -> None] => (item=ael-13.20.200-PREPRO-SAL_V13)
TASK [debug] *****************************************************************************************************************************************************
Thursday 05 December 2019 08:18:00 +0100 (0:00:03.320) 0:00:04.624 *****
ok: [localhost] => (item={'ansible_loop_var': u'item', u'ansible_job_id': u'264488569945.120415', 'failed': False, u'started': 1, 'changed': True, 'item': u'ael-13.20.200-PREPRO-AMN_V13', u'finished': 0, u'results_file': u'/root/.ansible_async/264488569945.120415'}) => {
"msg": "264488569945.120415"
}
ok: [localhost] => (item={'ansible_loop_var': u'item', u'ansible_job_id': u'828045810515.120527', 'failed': False, u'started': 1, 'changed': True, 'item': u'ael-13.20.200-PREPRO-SCH_V13', u'finished': 0, u'results_file': u'/root/.ansible_async/828045810515.120527'}) => {
"msg": "828045810515.120527"
}
ok: [localhost] => (item={'ansible_loop_var': u'item', u'ansible_job_id': u'406259878781.120644', 'failed': False, u'started': 1, 'changed': True, 'item': u'ael-13.20.200-PREPRO-SAR_V13', u'finished': 0, u'results_file': u'/root/.ansible_async/406259878781.120644'}) => {
"msg": "406259878781.120644"
}
ok: [localhost] => (item={'ansible_loop_var': u'item', u'ansible_job_id': u'803234566504.120755', 'failed': False, u'started': 1, 'changed': True, 'item': u'ael-13.20.200-PREPRO-ROM_V13', u'finished': 0, u'results_file': u'/root/.ansible_async/803234566504.120755'}) => {
"msg": "803234566504.120755"
}
ok: [localhost] => (item={'ansible_loop_var': u'item', u'ansible_job_id': u'966123972732.120860', 'failed': False, u'started': 1, 'changed': True, 'item': u'ael-13.20.200-PREPRO-SAL_V13', u'finished': 0, u'results_file': u'/root/.ansible_async/966123972732.120860'}) => {
"msg": "966123972732.120860"
}
TASK [waiting for shutdown] **************************************************************************************************************************************
Thursday 05 December 2019 08:18:00 +0100 (0:00:00.085) 0:00:04.710 *****
failed: [localhost -> None] (item={'ansible_loop_var': u'item', u'ansible_job_id': u'264488569945.120415', 'failed': False, u'started': 1, 'changed': True, 'item': u'ael-13.20.200-PREPRO-AMN_V13', u'finished': 0, u'results_file': u'/root/.ansible_async/264488569945.120415'}) => {"ansible_job_id": "264488569945.120415", "ansible_loop_var": "item", "attempts": 1, "changed": false, "finished": 1, "item": {"ansible_job_id": "264488569945.120415", "ansible_loop_var": "item", "changed": true, "failed": false, "finished": 0, "item": "ael-13.20.200-PREPRO-AMN_V13", "results_file": "/root/.ansible_async/264488569945.120415", "started": 1}, "msg": "could not find job", "started": 1}
failed: [localhost -> None] (item={'ansible_loop_var': u'item', u'ansible_job_id': u'828045810515.120527', 'failed': False, u'started': 1, 'changed': True, 'item': u'ael-13.20.200-PREPRO-SCH_V13', u'finished': 0, u'results_file': u'/root/.ansible_async/828045810515.120527'}) => {"ansible_job_id": "828045810515.120527", "ansible_loop_var": "item", "attempts": 1, "changed": false, "finished": 1, "item": {"ansible_job_id": "828045810515.120527", "ansible_loop_var": "item", "changed": true, "failed": false, "finished": 0, "item": "ael-13.20.200-PREPRO-SCH_V13", "results_file": "/root/.ansible_async/828045810515.120527", "started": 1}, "msg": "could not find job", "started": 1}
failed: [localhost -> None] (item={'ansible_loop_var': u'item', u'ansible_job_id': u'406259878781.120644', 'failed': False, u'started': 1, 'changed': True, 'item': u'ael-13.20.200-PREPRO-SAR_V13', u'finished': 0, u'results_file': u'/root/.ansible_async/406259878781.120644'}) => {"ansible_job_id": "406259878781.120644", "ansible_loop_var": "item", "attempts": 1, "changed": false, "finished": 1, "item": {"ansible_job_id": "406259878781.120644", "ansible_loop_var": "item", "changed": true, "failed": false, "finished": 0, "item": "ael-13.20.200-PREPRO-SAR_V13", "results_file": "/root/.ansible_async/406259878781.120644", "started": 1}, "msg": "could not find job", "started": 1}
failed: [localhost -> None] (item={'ansible_loop_var': u'item', u'ansible_job_id': u'803234566504.120755', 'failed': False, u'started': 1, 'changed': True, 'item': u'ael-13.20.200-PREPRO-ROM_V13', u'finished': 0, u'results_file': u'/root/.ansible_async/803234566504.120755'}) => {"ansible_job_id": "803234566504.120755", "ansible_loop_var": "item", "attempts": 1, "changed": false, "finished": 1, "item": {"ansible_job_id": "803234566504.120755", "ansible_loop_var": "item", "changed": true, "failed": false, "finished": 0, "item": "ael-13.20.200-PREPRO-ROM_V13", "results_file": "/root/.ansible_async/803234566504.120755", "started": 1}, "msg": "could not find job", "started": 1}
failed: [localhost -> None] (item={'ansible_loop_var': u'item', u'ansible_job_id': u'966123972732.120860', 'failed': False, u'started': 1, 'changed': True, 'item': u'ael-13.20.200-PREPRO-SAL_V13', u'finished': 0, u'results_file': u'/root/.ansible_async/966123972732.120860'}) => {"ansible_job_id": "966123972732.120860", "ansible_loop_var": "item", "attempts": 1, "changed": false, "finished": 1, "item": {"ansible_job_id": "966123972732.120860", "ansible_loop_var": "item", "changed": true, "failed": false, "finished": 0, "item": "ael-13.20.200-PREPRO-SAL_V13", "results_file": "/root/.ansible_async/966123972732.120860", "started": 1}, "msg": "could not find job", "started": 1}
PLAY RECAP *******************************************************************************************************************************************************
localhost : ok=4 changed=2 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
我设法通过一个简单的例子来运行这种机制:
- name: 'waiting for a long tasks'
shell: /bin/sleep 15
async: 1000
poll: 0
register: yum_sleeper
delegate_to: myserver
- debug:
msg: "{{ yum_sleeper }}"
delegate_to: myserver
- name: 'get results'
async_status:
jid: "{{ yum_sleeper.ansible_job_id }}"
register: job_result
until: job_result.finished
retries: 30
delegate_to: myserver
但是在我的实际用例中,我有一个“结果”属性,这可能是有问题的...
有人可以帮助我吗?
谢谢
答案 0 :(得分:0)
我认为问题出在 become
。要检查 async_status,用户需要与运行异步作业的用户相同。
- name: 'waiting for shutdown'
become: yes
async_status:
jid: "{{ item['ansible_job_id'] }}"
register: job_result
until: job_result.finished
retries: 30
with_items: "{{ restart_aelnet['results'] }}"
delegate_to: myserver