# Using loops and lookup

**URL:** <https://forum.ansible.com/t/using-loops-and-lookup/26389>\
**Category:** Ansible Project\
**Created:** [December 28, 2017, 10:02am UTC](https://forum.ansible.com/t/using-loops-and-lookup/26389 "2017-12-28T10:02:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thijn](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@thijn](https://forum.ansible.com/u/thijn)\
**Post date:** [December 28, 2017, 10:02am UTC](https://forum.ansible.com/t/using-loops-and-lookup/26389/1 "2017-12-28T10:02:00Z")

</div>

Hi,

| 

Trying to create files based on sourcefile, and of course when this construction works… i will use it on lots of things.

 |
|
| |

| 

Version: ansible 2.4.2.0  
I am trying to go through a source file ( in this case a list with users) and use every line i return in the file with the with\_items loop to create files based on the each line.

As you can see the return is “user1\nuser2\nuser3\nuser4”  
i want to split the lines, so i can use the value/string of each individual line.  
I am still fairly new with Ansible, so I am probably missing something fundamental.  
What am i missing? Why can’t i use stdout\_lines on a lookup return?  
“When is returned, Ansible always provides a list of strings, each containing one item per line from the original output.”

 |
|
| |

| |
|
| |

| 

_$cat /etc/remote\_users.txt_

 |
|
| |

| 

user1

 |
|
| |

| 

user2

 |
|
| |

| 

user3

 |
|
| |

| 

user4

 |
|
| |

| |
|
| |

| 

_$cat /opt/ansible/roles/testing/vars/main.yml_

 |
|
| |

| |

---

<div class="post-metadata">

**Author:** ![thijn](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@thijn](https://forum.ansible.com/u/thijn)\
**Post date:** [December 29, 2017, 9:42am UTC](https://forum.ansible.com/t/using-loops-and-lookup/26389/2 "2017-12-29T09:42:30Z")

</div>

Right… solved it 🙂  
The problem was that the lookup returned a string and not a list.  
so i changed the main.yml in vars from

users: “{{ lookup(‘file’, ‘/etc/remote\_users.txt’) }}”  
to  
users: “{{ lookup(‘file’, ‘/etc/remote\_users.txt’).split() }}”

and as there is no attribute in the list, i removed that well and just itereate of the list:  
with\_items: ‘{{users}}’  
and not  
with\_items: “{{ users.stdout\_lines }}”

problem solved.

---

<div class="post-metadata">

**Author:** ![thijn](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@thijn](https://forum.ansible.com/u/thijn)\
**Post date:** [December 29, 2017, 9:48am UTC](https://forum.ansible.com/t/using-loops-and-lookup/26389/3 "2017-12-29T09:48:39Z")

</div>

So the final result 😃  
TASK [testing : debug] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [minion2] =\> {  
“msg”: “the value of foo.txt is [u’user1’, u’user2’, u’user3’, u’user4’]”  
}

TASK [testing : test files] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [minion2] =\> (item=user1)  
changed: [minion2] =\> (item=user2)  
changed: [minion2] =\> (item=user3)  
changed: [minion2] =\> (item=user4)
