Created
August 10, 2017 06:55
-
-
Save robrant/6f4c35e5ce6bd6baba758a20ab02bb53 to your computer and use it in GitHub Desktop.
Showing how Ansible `register` nicely handles `with_items` loops.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Simple playbook to understand and illustrate how nicely ansible handles registering output as part of a `with_items` loop. | |
# To call: | |
# $> ansible-playbook playbook.yml | |
--- | |
- hosts: localhost | |
tasks: | |
- stat: | |
path: "/tmp" | |
register: my_stat_output | |
- debug: | |
msg: "{{ my_stat_output }}" #<-- this is a single output dict | |
- hosts: localhost | |
tasks: | |
- stat: | |
path: "/{{ item }}" | |
register: my_stat_output | |
with_items: | |
- "tmp" | |
- "etc" | |
- debug: | |
msg: "{{ my_stat_output }}" #<-- this is a ***list*** of dicts. Win. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# First Stat - no loop | |
ok: [localhost] => { | |
"msg": { | |
"changed": false, | |
"stat": { | |
... | |
} | |
} | |
} | |
# ------------------------------------------ | |
# Second stat - with_items loop | |
ok: [localhost] => { | |
"msg": { | |
"changed": false, | |
"msg": "All items completed", | |
"results": [ | |
{ | |
"_ansible_item_result": true, | |
"_ansible_no_log": false, | |
"_ansible_parsed": true, | |
"changed": false, | |
"invocation": { | |
"module_args": { | |
"checksum_algorithm": "sha1", | |
"follow": false, | |
"get_attributes": true, | |
"get_checksum": true, | |
"get_md5": true, | |
"get_mime": true, | |
"path": "/tmp" | |
} | |
}, | |
"item": "tmp", | |
"stat": { | |
... | |
} | |
}, | |
{ | |
"_ansible_item_result": true, | |
"_ansible_no_log": false, | |
"_ansible_parsed": true, | |
"changed": false, | |
"invocation": { | |
"module_args": { | |
"checksum_algorithm": "sha1", | |
"follow": false, | |
"get_attributes": true, | |
"get_checksum": true, | |
"get_md5": true, | |
"get_mime": true, | |
"path": "/etc" | |
} | |
}, | |
"item": "etc", | |
"stat": { | |
... | |
} | |
} | |
] | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment