# iterating over stdout\_lines list

**URL:** <https://forum.ansible.com/t/iterating-over-stdout-lines-list/27961>\
**Category:** Ansible Project\
**Created:** [July 5, 2018, 4:30pm UTC](https://forum.ansible.com/t/iterating-over-stdout-lines-list/27961 "2018-07-05T16:30:35Z")\
**Posts on this page:** 4\
**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:** [July 5, 2018, 4:30pm UTC](https://forum.ansible.com/t/iterating-over-stdout-lines-list/27961/1 "2018-07-05T16:30:35Z")

</div>

I want ansible to create a csv file for me. For example I need the output to be in this format:

Server, User, Approve, Remove  
serverA, user1,X,  
serverA, user2,X  
ServerA, user3,X  
ServerB, user1,X  
ServerC, user1,X  
ServerC, user2,X

In order to do this I need to iterate over a var I registered. ie:

`

---

<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:** [July 5, 2018, 4:58pm UTC](https://forum.ansible.com/t/iterating-over-stdout-lines-list/27961/2 "2018-07-05T16:58:11Z")

</div>

I have also been trying trying things similar to the following and receiving errors that the index is out of range:

`

- name: Create CSV file  
lineinfile:  
dest: “/tmp/prod\_admin\_users.csv”  
line: “{{ host }},{{ item }},X,”  
insertafter: EOF  
state: present  
delegate\_to: localhost  
with\_items:
- “{{ other\_users.split(‘\n’) }}”  
`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 5, 2018, 5:32pm UTC](https://forum.ansible.com/t/iterating-over-stdout-lines-list/27961/3 "2018-07-05T17:32:56Z")

</div>

Multiple task editing the same file seldom work.  
Instead I would use the copy module with Jinja template like this

- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;content: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Server,User,Approve,Remove,Reviewed by: YourNameHere  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for host in ansible\_play\_hosts %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for user in hostvars[host].other\_users.stdout\_lines %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ host }},{{ user }},X,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/prod\_admin\_users.csv  
&nbsp;&nbsp;&nbsp;delegate\_to: localhost  
&nbsp;&nbsp;&nbsp;run\_once: true

---

<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:** [July 5, 2018, 6:00pm UTC](https://forum.ansible.com/t/iterating-over-stdout-lines-list/27961/4 "2018-07-05T18:00:53Z")

</div>

Works perfectly! I never heard of Jinja before getting into Ansilble a year ago. I will have to go study up on jinja templating. I can see how your example works though. Thank you.
