Using loops and lookup

50 views
Skip to first unread message

Thijn van der Schoot

unread,
Dec 28, 2017, 5:02:00 AM12/28/17
to Ansible Project
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 stdout 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
---
users: "{{ lookup('file', '/etc/remote_users.txt') }}"

$cat /opt/ansible/roles/testing/tasks/google.yml
---
- debug: msg="the value of source text is {{ users }}"

- name: test files
file:
path: /home/thijn/{{ item }}
state: touch
mode: u=rw,g=r,o=r"
with_items: "{{ users.stdout_lines }}"
#with_items: "{{ users.stdout.split() }}"
#with_items: "{{ users.stdout.split('/n') }}"

Expected to have the following files created:
/home/thijn/user1
/home/thijn/user2
/home/thijn/user3
/home/thijn/user4

Actual output:
TASK [testing : debug] **************************************************************************************************************************************************************************************************
ok: [minion2] => {
"msg": "the value of foo.txt is user1\nuser2\nuser3\nuser4"
}

TASK [testing : test files] *********************************************************************************************************************************************************************************************
fatal: [minion2]: FAILED! => {"failed": true, "msg": "'ansible.vars.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'stdout_lines'"}

PLAY RECAP **************************************************************************************************************************************************************************************************************
minion2 : ok=2 changed=0 unreachable=0 failed=1

Thijn van der Schoot

unread,
Dec 29, 2017, 4:42:30 AM12/29/17
to Ansible Project
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.


Thijn van der Schoot

unread,
Dec 29, 2017, 4:48:39 AM12/29/17
to Ansible Project
So the final result :D
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)



Reply all
Reply to author
Forward
0 new messages