Looking for help with a problem I've been struggling with for a few hours. I want to iterate over a list, run a command, register the output for each command and then iterate with debug over each unique registers {{ someregister }}.stdout
For example, the following code will spit out "msg": "1" and "msg": "2"
--- - hosts: localhost gather_facts: false vars: numbers: - name: "first" int: "1" - name: "second" int: "2" tasks: - name: Register output command: "/bin/echo {{ item.int }}" register: result with_items: "{{ numbers }}" - debug: msg={{ item.stdout }} with_items: "{{ result.results }}" If however, I try and capture the output of a command in a register variable that is named using with_list, I am having trouble accessing the list or the elements within it. For example, altering the code slightly to:
--- - hosts: localhost gather_facts: false vars: numbers: - name: "first" int: "1" - name: "second" int: "2" tasks: - name: Register output command: "/bin/echo {{ item.int }}" register: "{{ item.name }}" with_items: "{{ numbers }}" - debug: var={{ item.name.stdout }} with_items: "{{ numbers }}" Gives me:
TASK [debug] > ******************************************************************* fatal: [localhost]: FAILED! => {"failed": true, "msg": "'unicode > object' has no attribute 'stdout'"} Is it not possible to dynamically name the register the output of a command which can then be called later on in the play? I would like each iteration of the command and its subsequent register name to be accessed uniquely, e.g, given the last example I would expect there to be variables registered called "first" and "second" but there aren't.
Taking away the with_items from the debug stanza, and just explicitly defining the var or message using first.stdout returns "undefined".
Ansible version is 2.0.2.0 on Centos 7_2.
Thanks in advance.
Looping over Filetrees. with_filetree recursively matches all files in a directory tree, enabling you to template a complete tree of files on a target system while retaining permissions and ownership.
In Ansible, you can run any shell command on your Ansible hosts, the hosts you will be configuring with Ansible. These shell commands may have outputs. By default, the output is ignored. If you want to store the output in a variable and use it later, then you can use the Ansible register module.
with_dict: - "{{ zones_hash }}" declares a list with a dict as the first index, and Ansible rightfully complains since it expects a dict. The solution kfreezy mentioned works since it actually gives a dictionary to with_dict and not a list: with_dict: "{{ zones_hash }}" Follow this answer to receive notifications.
Ansible's syntax also supports the idea of nested looping. Nested loops in many ways are similar in nature to a set of arrays that would be iterated over using the with_nested operator. Nested loops provide us with a succinct way of iterating over multiple lists within a single task.
OK so I found a post on stackoverflow that helped me better understand what is going on here and how to access the elements in result.results.
The resultant code I ended up with was:
--- - hosts: localhost gather_facts: false vars: numbers: - name: "first" int: "1" - name: "second" int: "2" tasks: - name: Register output command: "/bin/echo {{ item.int }}" register: echo_out with_items: "{{ numbers }}" - debug: msg="item.item={{item.item.name}}, item.stdout={{item.stdout}}" with_items: "{{ echo_out.results }}" Which gave me the desired result:
"msg": "item.item=first, item.stdout=1" "msg": "item.item=second, item.stdout=2"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With