# Multiple vars in with\_nested problem

**URL:** <https://forum.ansible.com/t/multiple-vars-in-with-nested-problem/21333>\
**Category:** Ansible Project\
**Tags:** fedora\
**Created:** [May 2, 2016, 6:35pm UTC](https://forum.ansible.com/t/multiple-vars-in-with-nested-problem/21333 "2016-05-02T18:35:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 2, 2016, 6:35pm UTC](https://forum.ansible.com/t/multiple-vars-in-with-nested-problem/21333/1 "2016-05-02T18:35:56Z")

</div>

Hi,

I'm trying out (Ansible 2.0.2.0 on Fedora 23). The snippet below is also on [http://pastie.org/10821272](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:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- usr: patrick  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;base: /home/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- usr: root  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;base: /  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- usr: mockbuild  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;base: /home/

mc\_templates:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- src: mc/ini.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dst: ini

tasks:

- name: mc-set-colors | task #1 check if user exists  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: /usr/bin/getent passwd {{ item.usr }} | /usr/bin/wc -l | tr -d ' '  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ mc\_users }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: "user\_exists"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: mc-set-colors

- name: mc-set-colors | task #2 make sure mc config dir exists  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ item.item.base }}{{ item.item.usr }}/.config/mc"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: "{{ item.item.usr }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: "{{ item.item.usr }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0755  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: directory  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;recurse: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ user\_exists.results }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item.stdout == "1"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: mc-set-colors

- name: mc-set-colors | task #3 install mc config template  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "mc/ini.j2"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ item.item.base }}{{ item.item.usr }}/.config/mc/ini"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: "{{ item.item.usr }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: "{{ item.item.usr }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0644  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;force: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backup: no  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ user\_exists.results }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item.stdout == "1"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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  
&nbsp;&nbsp;&nbsp;&nbsp;template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: "{{ item.1.src }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ item.item.base }}{{ item.item.usr }}/.config/mc/{{ item.1.dst }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;owner: "{{ item.item.usr }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: "{{ item.item.usr }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mode: 0644  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;force: yes  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;backup: no  
&nbsp;&nbsp;&nbsp;&nbsp;with\_nested:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ user\_exists.results }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ mc\_templates }}"  
&nbsp;&nbsp;&nbsp;&nbsp;when: item.stdout == "1"  
&nbsp;&nbsp;&nbsp;&nbsp;tags: mc-set-colors

Resulting error:

TASK [laptop : mc-set-colors | install mc config template]

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [May 3, 2016, 4:29am UTC](https://forum.ansible.com/t/multiple-vars-in-with-nested-problem/21333/2 "2016-05-03T04:29:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 3, 2016, 8:28am UTC](https://forum.ansible.com/t/multiple-vars-in-with-nested-problem/21333/3 "2016-05-03T08:28:44Z")

</div>

Hi Johannes,

Thank you for your feedback.

> > &nbsp;&nbsp;&nbsp;&nbsp;with\_nested:  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ user\_exists.results }}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- "{{ 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
