# Nested loop.  Write var to file before moving on to next {{item}}

**URL:** <https://forum.ansible.com/t/nested-loop-write-var-to-file-before-moving-on-to-next-item/25645>\
**Category:** Ansible Project\
**Created:** [September 29, 2017, 9:56pm UTC](https://forum.ansible.com/t/nested-loop-write-var-to-file-before-moving-on-to-next-item/25645 "2017-09-29T21:56:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [September 29, 2017, 9:56pm UTC](https://forum.ansible.com/t/nested-loop-write-var-to-file-before-moving-on-to-next-item/25645/1 "2017-09-29T21:56:55Z")

</div>

The following playbook is close to what I need, but I need to write the content of sudoers.stdout\_lines to a file (append) before moving on to the next item. Is it possible to nest these two pieces? or rewrite it to do what I need?

`

- name: Gather sudo users  
shell: “/bin/cat /etc/{{item}} | grep -ve ^$ -e ^# -e ^Defaults”  
register: sudoers  
with\_items:

- sudoers

- sudoers.d/\*

- name: Write sudo users  
lineinfile:  
dest: “/tmp/{{ansible\_nodename}}.sudoers”  
line: “{{sudoers.stdout\_lines}}”  
insertafter: EOF  
create: yes  
state: present  
delegate\_to: localhost

`

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [September 29, 2017, 10:12pm UTC](https://forum.ansible.com/t/nested-loop-write-var-to-file-before-moving-on-to-next-item/25645/2 "2017-09-29T22:12:44Z")

</div>

`Interesting..... debug shows that the variable is being appended to. I can't seem to reference anything under the var (sudoers.stdout_lines for instance). Is it possible to grab all instances of stdout_lines with this var's output? I've been trying without luck . . .`

` `  
ok: [ansibletest-oel6] =\> {  
“sudoers”: {  
“changed”: true,  
“msg”: “All items completed”,  
“results”: [  
{  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: true,  
“cmd”: “/bin/cat /etc/sudoers | grep -ve ^$ -e ^# -e ^Defaults”,  
“delta”: “0:00:00.004715”,  
“end”: “2017-09-29 16:08:26.616661”,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “/bin/cat /etc/sudoers | grep -ve ^$ -e ^# -e ^Defaults”,  
“\_uses\_shell”: true,  
“chdir”: null,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“warn”: true  
}  
},  
“item”: “sudoers”,  
“rc”: 0,  
“start”: “2017-09-29 16:08:26.611946”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “root\tALL=(ALL) \tALL”,  
“stdout\_lines”: [  
“root\tALL=(ALL) \tALL”  
]  
},  
{  
“\_ansible\_item\_result”: true,  
“\_ansible\_no\_log”: false,  
“\_ansible\_parsed”: true,  
“changed”: true,  
“cmd”: “/bin/cat /etc/sudoers.d/\* | grep -ve ^$ -e ^# -e ^Defaults”,  
“delta”: “0:00:00.005332”,  
“end”: “2017-09-29 16:08:26.782472”,  
“invocation”: {  
“module\_args”: {  
“\_raw\_params”: “/bin/cat /etc/sudoers.d/\* | grep -ve ^$ -e ^# -e ^Defaults”,  
“\_uses\_shell”: true,  
“chdir”: null,  
“creates”: null,  
“executable”: null,  
“removes”: null,  
“warn”: true  
}  
},  
“item”: “sudoers.d/\*”,  
“rc”: 0,  
“start”: “2017-09-29 16:08:26.777140”,  
“stderr”: “”,  
“stderr\_lines”: ,  
“stdout”: “gomer ALL=(ALL) NOPASSWD: ALL\npyle ALL=(ALL) NOPASSWD: ALL”,  
“stdout\_lines”: [  
“gomer ALL=(ALL) NOPASSWD: ALL”,  
“pyle ALL=(ALL) NOPASSWD: ALL”  
]  
}  
]  
}  
}

` `

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [September 29, 2017, 10:33pm UTC](https://forum.ansible.com/t/nested-loop-write-var-to-file-before-moving-on-to-next-item/25645/3 "2017-09-29T22:33:32Z")

</div>

I found this: [https://groups.google.com/forum/#!msg/ansible-project/QUugEQRZT-g/bYOjM\_k3BQAJ](https://groups.google.com/forum/#!msg/ansible-project/QUugEQRZT-g/bYOjM_k3BQAJ)

If I change my line to this:

`line: "{{sudoers.results|map(attribute='stdout_lines')|list}}"`

Then I get the following results in my file:

`[['root\tALL=(ALL) \tALL'], ['gomer ALL=(ALL) NOPASSWD: ALL', 'pyle ALL=(ALL) NOPASSWD: ALL']]`

Is there a way to further refine the output to look like the following?

`root ALL=(ALL) ALL gomer ALL=(ALL) NOPASSWD: ALL pyle ALL=(ALL) NOPASSWD: ALL`

---

<div class="post-metadata">

**Author:** ![Zimidar\_Boy](https://avatars.discourse-cdn.com/v4/letter/z/0ea827/32.png) [@Zimidar\_Boy](https://forum.ansible.com/u/Zimidar_Boy)\
**Post date:** [September 30, 2017, 2:52am UTC](https://forum.ansible.com/t/nested-loop-write-var-to-file-before-moving-on-to-next-item/25645/4 "2017-09-30T02:52:12Z")

</div>

I don’t know much but maybe this can help. [http://docs.ansible.com/ansible/latest/playbooks\_filters.html](http://docs.ansible.com/ansible/latest/playbooks_filters.html)

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [October 2, 2017, 3:33pm UTC](https://forum.ansible.com/t/nested-loop-write-var-to-file-before-moving-on-to-next-item/25645/5 "2017-10-02T15:33:06Z")

</div>

That did come in helpful, thank you. It helped me to format the output nicely.
