# using register stdout\_lines in with\_items loop

**URL:** <https://forum.ansible.com/t/using-register-stdout-lines-in-with-items-loop/20649>\
**Category:** Ansible Project\
**Created:** [February 22, 2016, 3:19pm UTC](https://forum.ansible.com/t/using-register-stdout-lines-in-with-items-loop/20649 "2016-02-22T15:19:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason3](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Jason3](https://forum.ansible.com/u/Jason3)\
**Post date:** [February 22, 2016, 3:19pm UTC](https://forum.ansible.com/t/using-register-stdout-lines-in-with-items-loop/20649/1 "2016-02-22T15:19:47Z")

</div>

Hello All,

I’m ultimately attempting to pull a list of files with wildcarded paths and pass the results into the replace module so it can cycle through them all. However, starting with a more simple example, i’m having issues getting the list of files to print properly in even a simple test case.

Pastebin code:  
[http://pastebin.com/zFRzucat](http://pastebin.com/zFRzucat)

I’m looking for this:

test.yml playbook

- hosts: all  
tasks:

- name: gather list of files  
shell: ls {{ item }}  
register: files  
with\_items:

- /app/psoft/test/\*/list.txt

- /app/psoft/test/\*/context.xml

- name: use shell to print list of file paths  
shell: “echo {{ item }}”  
with\_items: “{{files.stdout\_lines}}”

to print

/app/psoft/test/12.1.2.00/list.txt  
/app/psoft/test/12.1.3.00/context.xml

However currently the result is:

TASK [gather list of files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [net12204] =\> (item=/app/psoft/test/_/list.txt)  
changed: [net12204] =\> (item=/app/psoft/test/_/context.xml)

TASK [use shell to print list of file paths] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
[DEPRECATION WARNING]: Skipping task due to undefined attribute, in the future this will be a fatal error… This feature will  
be removed in a future release. Deprecation warnings can be disabled by setting deprecation\_warnings=False in ansible.cfg.

Am I misusing or not understanding [http://docs.ansible.com/ansible/playbooks\_loops.html#iterating-over-the-results-of-a-program-execution](http://docs.ansible.com/ansible/playbooks_loops.html#iterating-over-the-results-of-a-program-execution) properly?

Thanks!  
Jason

---

<div class="post-metadata">

**Author:** ![Jason3](https://avatars.discourse-cdn.com/v4/letter/j/f9ae1b/32.png) [@Jason3](https://forum.ansible.com/u/Jason3)\
**Post date:** [February 23, 2016, 6:51pm UTC](https://forum.ansible.com/t/using-register-stdout-lines-in-with-items-loop/20649/2 "2016-02-23T18:51:36Z")

</div>

So after some testing and reading i’m gonna attempt to answer my own question, and pose another one.

The documentation I cited for iterating the results of a program execution didn’t involve the use of with\_items in the original program execution. Having multiple results in a register variable doesn’t seem to play nice with ‘with\_items’.

I decided to work around the issue by having multiple tasks to gather the file names and iterate over each result in the replace module.

As a corollary to that, when using with\_items and setting a failed\_when condition, you can’t seem to use the ‘rc’ property of the register variable in a task because the rc code only exists if the task fails. I’m thinking there must be a way to say fail\_when: the register.rc exists, but haven’t figured that part out yet.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 23, 2016, 7:04pm UTC](https://forum.ansible.com/t/using-register-stdout-lines-in-with-items-loop/20649/3 "2016-02-23T19:04:15Z")

</div>

The “problem” is in how ansible registers results when using with\_items. It nests all of the results inside of a `results` key

Your second task could have been written to use:

- name: use shell to print list of file paths  
shell: “echo {{ item }}”  
with\_items: “{{ files.results|map(attribute=‘stdout\_lines’)|list }}”

That would do it for you. That pulls the `stdout_lines` attribute out into a list of `stdout_lines`, then with\_items natively handles the flattening, allowing you to loop over each individual item in the `ls` output.

There is some documentation about using register with a loop, which you can find at [http://docs.ansible.com/ansible/playbooks\_loops.html#using-register-with-a-loop](http://docs.ansible.com/ansible/playbooks_loops.html#using-register-with-a-loop)

---

<div class="post-metadata">

**Author:** ![Adam](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Adam](https://forum.ansible.com/u/Adam)\
**Post date:** [May 30, 2019, 1:12am UTC](https://forum.ansible.com/t/using-register-stdout-lines-in-with-items-loop/20649/4 "2019-05-30T01:12:22Z")

</div>

Matt -

I don’t know how you figured out how to do this (I would love to know), but it should DEFINITELY be in the documentation. It’s not pretty, but it works and it solved my issue. It’s definitely easier than breaking it into multiple tasks or having to populate a list manually.

```auto
- name: get list of directories
  shell: "egrep '^archive.directory|^outgoing.directory|^incoming.directory' {{conf_dir}}/{{item.1.config}}|cut -d '=' -f2|sort|uniq"
  with_subelements:
    - "{{loggers}}"
    - configs
  register: directoriesToBuild

- name: debug directory output
  debug:
    msg: "{{directoriesToBuild.results|map(attribute='stdout_lines')|list}}"

- name: build found directories
  file:
    state: directory
    path: "{{item}}"
  with_items:
    - "{{directoriesToBuild.results|map(attribute='stdout_lines')|list}}"

```

-Adam

---

<div class="post-metadata">

**Author:** ![Adam](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Adam](https://forum.ansible.com/u/Adam)\
**Post date:** [May 30, 2019, 1:27am UTC](https://forum.ansible.com/t/using-register-stdout-lines-in-with-items-loop/20649/5 "2019-05-30T01:27:43Z")

</div>

I was over zealous & too hopeful… this doesn’t actually work for me. It looks like it treats the whole list of directories as one big directory. I’m not entirely sure why yet.

---

<div class="post-metadata">

**Author:** ![Adam](https://avatars.discourse-cdn.com/v4/letter/a/4af34b/32.png) [@Adam](https://forum.ansible.com/u/Adam)\
**Post date:** [May 30, 2019, 1:19pm UTC](https://forum.ansible.com/t/using-register-stdout-lines-in-with-items-loop/20649/6 "2019-05-30T13:19:35Z")

</div>

After spending several hours trying different combinations of tasks, filters, combinations of filters, etc, I decided I spent too much time trying to do it all in memory. Here was my final solution that works:

```auto
- name: make sure temp file doesn't exist
  file:
    state: absent
    path: "{{appdataRoot}}/directoriesToBuild.tmp"

- name: get list of directories to build
  shell: "egrep '^archive.directory|^outgoing.directory|^incoming.directory' {{conf_dir}}/{{item.1.config}}|cut -d '=' -f2|sort|uniq >> {{appdataRoot}}/directoriesToBuild.tmp"
  with_subelements:
    - "{{loggers}}"
    - configs

- name: put list of directories into a variable
  shell: "cat {{appdataRoot}}/directoriesToBuild.tmp"
  register: directoriesToBuild

- name: build found directories
  file:
    state: directory
    path: "{{item}}"
  with_items:
    - "{{directoriesToBuild.stdout_lines}}"

```

---

<div class="post-metadata">

**Author:** ![David\_Gorsett](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@David\_Gorsett](https://forum.ansible.com/u/David_Gorsett)\
**Post date:** [December 22, 2022, 5:51pm UTC](https://forum.ansible.com/t/using-register-stdout-lines-in-with-items-loop/20649/7 "2022-12-22T17:51:04Z")

</div>

This page was the starting point of me figuring out how to print the ‘results[\*].stdout\_lines’ of a registered return value. So, I thought I ought to share what I found to work, as it’s quite short and re-usable with any return value that produces a list. It also provides flexibility to add more fields to the output.

If ‘stdout\_lines’ is the only variable in the select, the resulting array will be all 1 liners, rather than grouped by result. ‘item’ within each result in the list displays the item name provided when the list was produced.

- name: Show ‘item’ & ‘stdout\_lines’ per result  
debug: var=item  
loop: “{{ result | json\_query(‘results[\*].{item: item, stdout\_lines: stdout\_lines}’) }}”

- name: Only show ‘stdout\_lines’ per result

debug: var=item.stdout\_lines  
loop: “{{ result | json\_query(‘results[\*].{item: item, stdout\_lines: stdout\_lines}’) }}”

- name: Use shell to print ‘stdout\_lines’ per result  
shell: “echo {{ item.stdout\_lines }}”  
loop: “{{ result | json\_query(‘results[\*].{item: item, stdout\_lines: stdout\_lines}’) }}”

- name: Use shell to print flattened ‘stdout\_lines’  
shell: “echo {{ item }}”  
loop: “{{ result | json\_query(‘results[\*].{stdout\_lines: stdout\_lines}’) }}”

Hopefully someone finds this useful.
