Skip to content

Instantly share code, notes, and snippets.

@odyssey4me
Last active August 25, 2017 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odyssey4me/5256717f40fd5232230d895b6773a8ca to your computer and use it in GitHub Desktop.
Save odyssey4me/5256717f40fd5232230d895b6773a8ca to your computer and use it in GitHub Desktop.
ansible async job with retries - unfortunately using async with retries doesn't work
---
- hosts: localhost
gather_facts: false
tasks:
- name: Test single async task
shell: "sleep 2; false"
register: _async_test1
async: 1800
poll: 0
retries: 3
delay: 1
until: _async_test1 | success
- name: Test with_items async tasks
shell: "{{ item }}"
with_items:
- "sleep 1; false"
- "sleep 10"
- "sleep 15"
register: _async_test2
async: 1800
poll: 0
retries: 3
delay: 1
until: _async_test2 | success
- name: Wait for async task to complete
async_status:
jid: "{{ item['ansible_job_id'] }}"
register: _async_test_jobs
until: _async_test_jobs['finished'] | bool
delay: 5
retries: 5
with_items: "{{ [_async_test1] + _async_test2['results'] }}"
# ansible-playbook -i localhost test.yml -vvv
No config file found; using defaults
[WARNING]: Host file not found: localhost
[WARNING]: provided hosts list is empty, only localhost is available
PLAYBOOK: test.yml *************************************************************
1 plays in test.yml
PLAY [localhost] ***************************************************************
TASK [Test single async task] **************************************************
task path: /opt/rpc-openstack/test.yml:6
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1503676114.24-119378740424375 `" && echo ansible-tmp-1503676114.24-119378740424375="` echo $HOME/.ansible/tmp/ansible-tmp-1503676114.24-119378740424375 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpehrIKP TO /root/.ansible/tmp/ansible-tmp-1503676114.24-119378740424375/command
<127.0.0.1> PUT /tmp/tmpB0RzFH TO /root/.ansible/tmp/ansible-tmp-1503676114.24-119378740424375/async_wrapper
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1503676114.24-119378740424375/ /root/.ansible/tmp/ansible-tmp-1503676114.24-119378740424375/command /root/.ansible/tmp/ansible-tmp-1503676114.24-119378740424375/async_wrapper && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /root/.ansible/tmp/ansible-tmp-1503676114.24-119378740424375/async_wrapper 478187537679 1800 /root/.ansible/tmp/ansible-tmp-1503676114.24-119378740424375/command && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1503676114.24-119378740424375/ > /dev/null 2>&1 && sleep 0'
ok: [localhost] => {"ansible_job_id": "478187537679.9061", "changed": false, "finished": 0, "results_file": "/root/.ansible_async/478187537679.9061", "started": 1}
TASK [Test with_items async tasks] *********************************************
task path: /opt/rpc-openstack/test.yml:15
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1503676115.48-130464008568417 `" && echo ansible-tmp-1503676115.48-130464008568417="` echo $HOME/.ansible/tmp/ansible-tmp-1503676115.48-130464008568417 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpRU1Kb3 TO /root/.ansible/tmp/ansible-tmp-1503676115.48-130464008568417/command
<127.0.0.1> PUT /tmp/tmpbr5vjy TO /root/.ansible/tmp/ansible-tmp-1503676115.48-130464008568417/async_wrapper
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1503676115.48-130464008568417/ /root/.ansible/tmp/ansible-tmp-1503676115.48-130464008568417/command /root/.ansible/tmp/ansible-tmp-1503676115.48-130464008568417/async_wrapper && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /root/.ansible/tmp/ansible-tmp-1503676115.48-130464008568417/async_wrapper 800377814562 1800 /root/.ansible/tmp/ansible-tmp-1503676115.48-130464008568417/command && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1503676115.48-130464008568417/ > /dev/null 2>&1 && sleep 0'
ok: [localhost] => (item=sleep 1; false) => {"ansible_job_id": "800377814562.9091", "finished": 0, "item": "sleep 1; false", "results_file": "/root/.ansible_async/800377814562.9091", "started": 1}
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1503676116.55-268052030436234 `" && echo ansible-tmp-1503676116.55-268052030436234="` echo $HOME/.ansible/tmp/ansible-tmp-1503676116.55-268052030436234 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpGWd0Fr TO /root/.ansible/tmp/ansible-tmp-1503676116.55-268052030436234/command
<127.0.0.1> PUT /tmp/tmpwyl7gQ TO /root/.ansible/tmp/ansible-tmp-1503676116.55-268052030436234/async_wrapper
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1503676116.55-268052030436234/ /root/.ansible/tmp/ansible-tmp-1503676116.55-268052030436234/command /root/.ansible/tmp/ansible-tmp-1503676116.55-268052030436234/async_wrapper && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /root/.ansible/tmp/ansible-tmp-1503676116.55-268052030436234/async_wrapper 460716033143 1800 /root/.ansible/tmp/ansible-tmp-1503676116.55-268052030436234/command && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1503676116.55-268052030436234/ > /dev/null 2>&1 && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1503676117.61-81094327870798 `" && echo ansible-tmp-1503676117.61-81094327870798="` echo $HOME/.ansible/tmp/ansible-tmp-1503676117.61-81094327870798 `" ) && sleep 0'
ok: [localhost] => (item=sleep 10) => {"ansible_job_id": "460716033143.9119", "finished": 0, "item": "sleep 10", "results_file": "/root/.ansible_async/460716033143.9119", "started": 1}
<127.0.0.1> PUT /tmp/tmpih0KOR TO /root/.ansible/tmp/ansible-tmp-1503676117.61-81094327870798/command
<127.0.0.1> PUT /tmp/tmp_E7k0v TO /root/.ansible/tmp/ansible-tmp-1503676117.61-81094327870798/async_wrapper
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1503676117.61-81094327870798/ /root/.ansible/tmp/ansible-tmp-1503676117.61-81094327870798/command /root/.ansible/tmp/ansible-tmp-1503676117.61-81094327870798/async_wrapper && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /root/.ansible/tmp/ansible-tmp-1503676117.61-81094327870798/async_wrapper 447657466900 1800 /root/.ansible/tmp/ansible-tmp-1503676117.61-81094327870798/command && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-tmp-1503676117.61-81094327870798/ > /dev/null 2>&1 && sleep 0'
ok: [localhost] => (item=sleep 15) => {"ansible_job_id": "447657466900.9146", "finished": 0, "item": "sleep 15", "results_file": "/root/.ansible_async/447657466900.9146", "started": 1}
TASK [Wait for async task to complete] *****************************************
task path: /opt/rpc-openstack/test.yml:28
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: root
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1503676118.8-120343439338991 `" && echo ansible-tmp-1503676118.8-120343439338991="` echo $HOME/.ansible/tmp/ansible-tmp-1503676118.8-120343439338991 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpD0k_wC TO /root/.ansible/tmp/ansible-tmp-1503676118.8-120343439338991/async_status
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1503676118.8-120343439338991/ /root/.ansible/tmp/ansible-tmp-1503676118.8-120343439338991/async_status && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /opt/ansible-runtime/bin/python2 /root/.ansible/tmp/ansible-tmp-1503676118.8-120343439338991/async_status; rm -rf "/root/.ansible/tmp/ansible-tmp-1503676118.8-120343439338991/" > /dev/null 2>&1 && sleep 0'
failed: [localhost] (item={u'started': 1, u'finished': 0, u'results_file': u'/root/.ansible_async/478187537679.9061', u'ansible_job_id': u'478187537679.9061', u'changed': False}) => {"ansible_job_id": "478187537679.9061", "changed": true, "cmd": "sleep 2; false", "delta": "0:00:02.003254", "end": "2017-08-25 15:48:36.445959", "failed": true, "finished": 1, "invocation": {"module_args": {"_raw_params": "sleep 2; false", "_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "async_status"}, "item": {"ansible_job_id": "478187537679.9061", "changed": false, "finished": 0, "results_file": "/root/.ansible_async/478187537679.9061", "started": 1}, "rc": 1, "start": "2017-08-25 15:48:34.442705", "stderr": "", "stdout": "", "stdout_lines": [], "warnings": []}
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1503676118.91-116150016451855 `" && echo ansible-tmp-1503676118.91-116150016451855="` echo $HOME/.ansible/tmp/ansible-tmp-1503676118.91-116150016451855 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpUVjRkE TO /root/.ansible/tmp/ansible-tmp-1503676118.91-116150016451855/async_status
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1503676118.91-116150016451855/ /root/.ansible/tmp/ansible-tmp-1503676118.91-116150016451855/async_status && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /opt/ansible-runtime/bin/python2 /root/.ansible/tmp/ansible-tmp-1503676118.91-116150016451855/async_status; rm -rf "/root/.ansible/tmp/ansible-tmp-1503676118.91-116150016451855/" > /dev/null 2>&1 && sleep 0'
failed: [localhost] (item={u'_ansible_parsed': True, u'_ansible_no_log': False, u'ansible_job_id': u'800377814562.9091', u'started': 1, u'_ansible_item_result': True, u'item': u'sleep 1; false', u'finished': 0, u'results_file': u'/root/.ansible_async/800377814562.9091'}) => {"ansible_job_id": "800377814562.9091", "changed": true, "cmd": "sleep 1; false", "delta": "0:00:01.003262", "end": "2017-08-25 15:48:36.612537", "failed": true, "finished": 1, "invocation": {"module_args": {"_raw_params": "sleep 1; false", "_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "async_status"}, "item": {"ansible_job_id": "800377814562.9091", "finished": 0, "item": "sleep 1; false", "results_file": "/root/.ansible_async/800377814562.9091", "started": 1}, "rc": 1, "start": "2017-08-25 15:48:35.609275", "stderr": "", "stdout": "", "stdout_lines": [], "warnings": []}
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1503676119.03-71793263820754 `" && echo ansible-tmp-1503676119.03-71793263820754="` echo $HOME/.ansible/tmp/ansible-tmp-1503676119.03-71793263820754 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpxN2rrU TO /root/.ansible/tmp/ansible-tmp-1503676119.03-71793263820754/async_status
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1503676119.03-71793263820754/ /root/.ansible/tmp/ansible-tmp-1503676119.03-71793263820754/async_status && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /opt/ansible-runtime/bin/python2 /root/.ansible/tmp/ansible-tmp-1503676119.03-71793263820754/async_status; rm -rf "/root/.ansible/tmp/ansible-tmp-1503676119.03-71793263820754/" > /dev/null 2>&1 && sleep 0'
FAILED - RETRYING: TASK: Wait for async task to complete (5 retries left).Result was: {"ansible_job_id": "460716033143.9119", "attempts": 1, "changed": false, "finished": 0, "invocation": {"module_args": {"jid": "460716033143.9119", "mode": "status"}, "module_name": "async_status"}, "retries": 6, "started": 1}
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1503676124.17-110315715091821 `" && echo ansible-tmp-1503676124.17-110315715091821="` echo $HOME/.ansible/tmp/ansible-tmp-1503676124.17-110315715091821 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpeX813l TO /root/.ansible/tmp/ansible-tmp-1503676124.17-110315715091821/async_status
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1503676124.17-110315715091821/ /root/.ansible/tmp/ansible-tmp-1503676124.17-110315715091821/async_status && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /opt/ansible-runtime/bin/python2 /root/.ansible/tmp/ansible-tmp-1503676124.17-110315715091821/async_status; rm -rf "/root/.ansible/tmp/ansible-tmp-1503676124.17-110315715091821/" > /dev/null 2>&1 && sleep 0'
FAILED - RETRYING: TASK: Wait for async task to complete (4 retries left).Result was: {"ansible_job_id": "460716033143.9119", "attempts": 2, "changed": false, "finished": 0, "invocation": {"module_args": {"jid": "460716033143.9119", "mode": "status"}, "module_name": "async_status"}, "retries": 6, "started": 1}
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1503676129.28-104160263332249 `" && echo ansible-tmp-1503676129.28-104160263332249="` echo $HOME/.ansible/tmp/ansible-tmp-1503676129.28-104160263332249 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmp0e_Qwe TO /root/.ansible/tmp/ansible-tmp-1503676129.28-104160263332249/async_status
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1503676129.28-104160263332249/ /root/.ansible/tmp/ansible-tmp-1503676129.28-104160263332249/async_status && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /opt/ansible-runtime/bin/python2 /root/.ansible/tmp/ansible-tmp-1503676129.28-104160263332249/async_status; rm -rf "/root/.ansible/tmp/ansible-tmp-1503676129.28-104160263332249/" > /dev/null 2>&1 && sleep 0'
changed: [localhost] => (item={u'_ansible_parsed': True, u'_ansible_no_log': False, u'ansible_job_id': u'460716033143.9119', u'started': 1, u'_ansible_item_result': True, u'item': u'sleep 10', u'finished': 0, u'results_file': u'/root/.ansible_async/460716033143.9119'}) => {"ansible_job_id": "460716033143.9119", "changed": true, "cmd": "sleep 10", "delta": "0:00:10.003011", "end": "2017-08-25 15:48:46.668284", "finished": 1, "invocation": {"module_args": {"_raw_params": "sleep 10", "_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "async_status"}, "item": {"ansible_job_id": "460716033143.9119", "finished": 0, "item": "sleep 10", "results_file": "/root/.ansible_async/460716033143.9119", "started": 1}, "rc": 0, "start": "2017-08-25 15:48:36.665273", "stderr": "", "stdout": "", "stdout_lines": [], "warnings": []}
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1503676129.41-121283399554820 `" && echo ansible-tmp-1503676129.41-121283399554820="` echo $HOME/.ansible/tmp/ansible-tmp-1503676129.41-121283399554820 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpkaDsv9 TO /root/.ansible/tmp/ansible-tmp-1503676129.41-121283399554820/async_status
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1503676129.41-121283399554820/ /root/.ansible/tmp/ansible-tmp-1503676129.41-121283399554820/async_status && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /opt/ansible-runtime/bin/python2 /root/.ansible/tmp/ansible-tmp-1503676129.41-121283399554820/async_status; rm -rf "/root/.ansible/tmp/ansible-tmp-1503676129.41-121283399554820/" > /dev/null 2>&1 && sleep 0'
FAILED - RETRYING: TASK: Wait for async task to complete (5 retries left).Result was: {"ansible_job_id": "447657466900.9146", "attempts": 1, "changed": false, "finished": 0, "invocation": {"module_args": {"jid": "447657466900.9146", "mode": "status"}, "module_name": "async_status"}, "retries": 6, "started": 1}
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1503676134.52-38149466627669 `" && echo ansible-tmp-1503676134.52-38149466627669="` echo $HOME/.ansible/tmp/ansible-tmp-1503676134.52-38149466627669 `" ) && sleep 0'
<127.0.0.1> PUT /tmp/tmpug8TB6 TO /root/.ansible/tmp/ansible-tmp-1503676134.52-38149466627669/async_status
<127.0.0.1> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1503676134.52-38149466627669/ /root/.ansible/tmp/ansible-tmp-1503676134.52-38149466627669/async_status && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 /opt/ansible-runtime/bin/python2 /root/.ansible/tmp/ansible-tmp-1503676134.52-38149466627669/async_status; rm -rf "/root/.ansible/tmp/ansible-tmp-1503676134.52-38149466627669/" > /dev/null 2>&1 && sleep 0'
changed: [localhost] => (item={u'_ansible_parsed': True, u'_ansible_no_log': False, u'ansible_job_id': u'447657466900.9146', u'started': 1, u'_ansible_item_result': True, u'item': u'sleep 15', u'finished': 0, u'results_file': u'/root/.ansible_async/447657466900.9146'}) => {"ansible_job_id": "447657466900.9146", "changed": true, "cmd": "sleep 15", "delta": "0:00:15.003303", "end": "2017-08-25 15:48:52.723740", "finished": 1, "invocation": {"module_args": {"_raw_params": "sleep 15", "_uses_shell": true, "chdir": null, "creates": null, "executable": null, "removes": null, "warn": true}, "module_name": "async_status"}, "item": {"ansible_job_id": "447657466900.9146", "finished": 0, "item": "sleep 15", "results_file": "/root/.ansible_async/447657466900.9146", "started": 1}, "rc": 0, "start": "2017-08-25 15:48:37.720437", "stderr": "", "stdout": "", "stdout_lines": [], "warnings": []}
NO MORE HOSTS LEFT *************************************************************
to retry, use: --limit @/opt/rpc-openstack/test.retry
PLAY RECAP *********************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment