Skip to content

Instantly share code, notes, and snippets.

@robrant
Created August 10, 2017 06:55
Show Gist options
  • Save robrant/6f4c35e5ce6bd6baba758a20ab02bb53 to your computer and use it in GitHub Desktop.
Save robrant/6f4c35e5ce6bd6baba758a20ab02bb53 to your computer and use it in GitHub Desktop.
Showing how Ansible `register` nicely handles `with_items` loops.
# 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.
# 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