|
[root@web1 ~]# cat /etc/ansible/reloop.yml
---
- hosts: webservers
remote_user: root
tasks:
- shell: echo "{{ item }}"
with_items:
- one
- two
register: echo
- debug: msg="{{ echo }}"
- name: Fail if return code is not 0
fail: msg=" The command {{ item.cmd }} has a 0 return code"
when: item.rc != 0
with_items: "{{ echo.results }}"
[root@web1 ~]# ansible-playbook /etc/ansible/reloop.yml
PLAY [webservers] *************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.1.65]
TASK: [shell echo "{{ item }}"] ***********************************************
changed: [192.168.1.65] => (item=one)
changed: [192.168.1.65] => (item=two)
TASK: [debug msg="{{ echo }}"] ************************************************
ok: [192.168.1.65] => {
"msg": "{'msg': 'All items completed', 'changed': True, 'results': [{u'stdout': u'one', u'changed': True, u'end': u'2015-08-05 11:30:42.560652', u'start': u'2015-08-05 11:30:42.549056', u'cmd': u'echo \"one\"', u'rc': 0, 'item': 'one', u'stderr': u'', u'delta': u'0:00:00.011596', 'invocation': {'module_name': u'shell', 'module_args': u'echo \"one\"'}, 'stdout_lines': [u'one'], u'warnings': []}, {u'stdout': u'two', u'changed': True, u'end': u'2015-08-05 11:30:44.105387', u'start': u'2015-08-05 11:30:44.093500', u'cmd': u'echo \"two\"', u'rc': 0, 'item': 'two', u'stderr': u'', u'delta': u'0:00:00.011887', 'invocation': {'module_name': u'shell', 'module_args': u'echo \"two\"'}, 'stdout_lines': [u'two'], u'warnings': []}]}"
}
TASK: [Fail if return code is not 0] ******************************************
skipping: [192.168.1.65] => (item={u'cmd': u'echo "one"', u'end': u'2015-08-05 11:30:42.560652', u'stderr': u'', u'stdout': u'one', u'changed': True, u'rc': 0, 'item': 'one', u'warnings': [], u'delta': u'0:00:00.011596', 'invocation': {'module_name': u'shell', 'module_args': u'echo "one"'}, 'stdout_lines': [u'one'], u'start': u'2015-08-05 11:30:42.549056'})
skipping: [192.168.1.65] => (item={u'cmd': u'echo "two"', u'end': u'2015-08-05 11:30:44.105387', u'stderr': u'', u'stdout': u'two', u'changed': True, u'rc': 0, 'item': 'two', u'warnings': [], u'delta': u'0:00:00.011887', 'invocation': {'module_name': u'shell', 'module_args': u'echo "two"'}, 'stdout_lines': [u'two'], u'start': u'2015-08-05 11:30:44.093500'})
PLAY RECAP ********************************************************************
192.168.1.65 : ok=4 changed=1 unreachable=0 failed=0 |
|