Using loops and lookup

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

Right… solved it :slight_smile:
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.

So the final result :smiley:
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)