I’d also be super interested in some way to do this. To me this is a very sane use case, and the Ansible appears to allow the use of until: along with with_items:, but appears to just ignore the until statement. If this is not supported ideally there would be some sort of syntax issue.
Would the Ansible team be willing to take a pull request to allow until evaluations in the context of a with_items loop?
For more clarity here is an example play. Lets say we want to send a update to a single web server out of a set, and you want to stop after the first successful call. Maybe the calls are not idempotent, maybe you don’t want to add extra load…
here is a test play with until and with_items, and until apparently not being evaluated.
gather_facts: no
tasks:
action: debug msg=“cluster host {{ item }}”
action: uri timeout=2 url={{ item }}
action: shell curl --max-time 1 {{ item }} >/dev/null
with_items: “{{input_hosts|shuffle}}”
register: put_status
until: put_status.rc == 0
here is the output executing with an input array of URLs, you can see the first URL works yet it keeps going
ansible-playbook -vv -i …/hosts --sudo test2.yml --extra-vars=‘{“input_hosts”: [“https://www.google.com”, “https://www.snargoblarg.com”]}’
PLAY [localhost] **************************************************************
TASK: [test] ******************************************************************
<127.0.0.1> REMOTE_MODULE command curl --max-time 1 https://www.google.com >/dev/null #USE_SHELL
changed: [127.0.0.1] => (item=https://www.google.com) => {“attempts”: 0, “changed”: true, “cmd”: “curl --max-time 1 https://www.google.com >/dev/null”, “delta”: “0:00:00.555617”, “end”: “2016-01-04 23:32:04.951993”, “item”: “https://www.google.com”, “rc”: 0, “start”: “2016-01-04 23:32:04.396376”, “stderr”: " % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0\r100 18908 0 18908 0 0 35322 0 --:–:-- --:–:-- --:–:-- 36152", “stdout”: “”, “warnings”: [“Consider using get_url module rather than running curl”]}
<127.0.0.1> REMOTE_MODULE command curl --max-time 1 https://www.snargoblarg.com >/dev/null #USE_SHELL
<127.0.0.1> REMOTE_MODULE command curl --max-time 1 https://www.snargoblarg.com >/dev/null #USE_SHELL
Result from run 1 is: {‘cmd’: ‘curl --max-time 1 https://www.snargoblarg.com >/dev/null’, ‘end’: ‘2016-01-04 23:32:11.354140’, ‘stdout’: u’‘, ‘changed’: True, ‘attempts’: 1, ‘start’: ‘2016-01-04 23:32:11.188499’, ‘delta’: ‘0:00:00.165641’, ‘stderr’: ’ % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0curl: (6) Could not resolve host: www.snargoblarg.com’, ‘rc’: 6, ‘warnings’: [‘Consider using get_url module rather than running curl’]}
<127.0.0.1> REMOTE_MODULE command curl --max-time 1 https://www.snargoblarg.com >/dev/null #USE_SHELL
Result from run 2 is: {‘cmd’: ‘curl --max-time 1 https://www.snargoblarg.com >/dev/null’, ‘end’: ‘2016-01-04 23:32:16.868708’, ‘stdout’: u’‘, ‘changed’: True, ‘attempts’: 2, ‘start’: ‘2016-01-04 23:32:16.703207’, ‘delta’: ‘0:00:00.165501’, ‘stderr’: ’ % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0curl: (6) Could not resolve host: www.snargoblarg.com’, ‘rc’: 6, ‘warnings’: [‘Consider using get_url module rather than running curl’]}
<127.0.0.1> REMOTE_MODULE command curl --max-time 1 https://www.snargoblarg.com >/dev/null #USE_SHELL
Result from run 3 is: {‘cmd’: ‘curl --max-time 1 https://www.snargoblarg.com >/dev/null’, ‘end’: ‘2016-01-04 23:32:22.253845’, ‘stdout’: u’‘, ‘changed’: True, ‘attempts’: 3, ‘start’: ‘2016-01-04 23:32:22.231865’, ‘delta’: ‘0:00:00.021980’, ‘stderr’: ’ % Total % Received % Xferd Average Speed Time Time Time Current\n Dload Upload Total Spent Left Speed\n\r 0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0curl: (6) Could not resolve host: www.snargoblarg.com’, ‘rc’: 6, ‘warnings’: [‘Consider using get_url module rather than running curl’]}
failed: [127.0.0.1] => (item=https://www.snargoblarg.com) => {“attempts”: 3, “changed”: true, “cmd”: “curl --max-time 1 https://www.snargoblarg.com >/dev/null”, “delta”: “0:00:00.021980”, “end”: “2016-01-04 23:32:22.253845”, “failed”: true, “item”: “https://www.snargoblarg.com”, “rc”: 6, “start”: “2016-01-04 23:32:22.231865”, “warnings”: [“Consider using get_url module rather than running curl”]}
stderr: % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:–:-- --:–:-- --:–:-- 0curl: (6) Could not resolve host: www.snargoblarg.com
msg: Task failed as maximum retries was encountered
FATAL: all hosts have already failed – aborting