Multiple vars in with_nested problem

43 views
Skip to first unread message

Patrick Laimbock

unread,
May 2, 2016, 2:59:15 PM5/2/16
to ansible...@googlegroups.com
Hi,

I'm trying out (Ansible 2.0.2.0 on Fedora 23). The snippet below is also
on http://pastie.org/10821272

Goal: expand task #3 below so it installs for existing users all the
files listed in the variable mc_templates.

Question: I tinkered but couldn't make it work (snippet + error at the
end) so how do I do that? Is with_nested the right one to use? How do I
reference items in user_exists.results and mc_templates?

vars:
mc_users:
- usr: patrick
base: /home/
- usr: root
base: /
- usr: mockbuild
base: /home/

mc_templates:
- src: mc/ini.j2
dst: ini

tasks:

- name: mc-set-colors | task #1 check if user exists
shell: /usr/bin/getent passwd {{ item.usr }} | /usr/bin/wc -l | tr
-d ' '
with_items:
- "{{ mc_users }}"
register: "user_exists"
tags: mc-set-colors

- name: mc-set-colors | task #2 make sure mc config dir exists
file:
dest: "{{ item.item.base }}{{ item.item.usr }}/.config/mc"
owner: "{{ item.item.usr }}"
group: "{{ item.item.usr }}"
mode: 0755
state: directory
recurse: yes
with_items:
- "{{ user_exists.results }}"
when: item.stdout == "1"
tags: mc-set-colors

- name: mc-set-colors | task #3 install mc config template
template:
src: "mc/ini.j2"
dest: "{{ item.item.base }}{{ item.item.usr }}/.config/mc/ini"
owner: "{{ item.item.usr }}"
group: "{{ item.item.usr }}"
mode: 0644
force: yes
backup: no
with_items:
- "{{ user_exists.results }}"
when: item.stdout == "1"
tags: mc-set-colors

^^^ task #1, #2 and #3 work fine. I tried the change task #3 below but
that results in an error. Any ideas?

- name: mc-set-colors | install mc config template
template:
src: "{{ item.1.src }}"
dest: "{{ item.item.base }}{{ item.item.usr }}/.config/mc/{{
item.1.dst }}"
owner: "{{ item.item.usr }}"
group: "{{ item.item.usr }}"
mode: 0644
force: yes
backup: no
with_nested:
- "{{ user_exists.results }}"
- "{{ mc_templates }}"
when: item.stdout == "1"
tags: mc-set-colors


Resulting error:

TASK [laptop : mc-set-colors | install mc config template]
*********************
fatal: [laptop]: FAILED! => {"failed": true, "msg": "'list object' has
no attribute 'item'"}


Thanks for any pointers.

Cheers,
Patrick

Johannes Kastl

unread,
May 3, 2016, 3:21:20 AM5/3/16
to ansible...@googlegroups.com
On 02.05.16 20:35 Patrick Laimbock wrote:

> with_nested:
> - "{{ user_exists.results }}"
> - "{{ mc_templates }}"

Please correct me if I am mistaken, I am no expert on this. Maybe
Brian can chime in.

I think you are creating a list here, which has two list elements. And
with_nested only works with dicts.

Try to join these two lists together (maybe use {{ foo + bar }} or
something similar) as argument for with_nested.

Johannes

signature.asc

Patrick Laimbock

unread,
May 3, 2016, 4:28:51 AM5/3/16
to ansible...@googlegroups.com
Hi Johannes,

Thank you for your feedback.

On 03-05-16 06:29, Johannes Kastl wrote:
> On 02.05.16 20:35 Patrick Laimbock wrote:
>
>> with_nested:
>> - "{{ user_exists.results }}"
>> - "{{ mc_templates }}"
>
> Please correct me if I am mistaken, I am no expert on this. Maybe
> Brian can chime in.

That would be nice :)

> I think you are creating a list here, which has two list elements. And
> with_nested only works with dicts.

Got it, thanks. Taking a step back, if there is a simpler way to do this
then I'm all ears. The goal is: install one or more files in a directory
in the home of a user (skip if user does not exist).

> Try to join these two lists together (maybe use {{ foo + bar }} or
> something similar) as argument for with_nested.

I'll give that a try.

Thanks,
Patrick

Reply all
Reply to author
Forward
0 new messages