# Need help with nested variables to use in a "when:" directive inside of a role playbook

**URL:** <https://forum.ansible.com/t/need-help-with-nested-variables-to-use-in-a-when-directive-inside-of-a-role-playbook/20425>\
**Category:** Ansible Project\
**Created:** [February 2, 2016, 8:48pm UTC](https://forum.ansible.com/t/need-help-with-nested-variables-to-use-in-a-when-directive-inside-of-a-role-playbook/20425 "2016-02-02T20:48:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dayton\_Jones](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@Dayton\_Jones](https://forum.ansible.com/u/Dayton_Jones)\
**Post date:** [February 2, 2016, 8:48pm UTC](https://forum.ansible.com/t/need-help-with-nested-variables-to-use-in-a-when-directive-inside-of-a-role-playbook/20425/1 "2016-02-02T20:48:21Z")

</div>

I have a requirement to create directories only if the specified user(s) exist on the remote host… given my role defintion below, what is the proper syntax to use with “when” (or other method) to only create the directory only if that user exists on the host and skip the task if not present?

…/vars/main.yml:

> * * *
> 
> my\_user\_list:
> 
> - user1
> - user2

…/tasks/main.yml:

> - name: Check for existence of users  
> getent: database=passwd key={{item}} fail\_key=False  
> with\_items: my\_user\_list  
> register: passinfo
> - name: Ensure user directory exists (user1)  
> file:  
> path=/opt/user1  
> state=directory  
> owner=user1  
> group=user1  
> mode=0755  
> when: ??? filter to only run if user1 exists ???  
> ignore\_errors: yes
> - name: Ensure user directory exists (user2)  
> file:  
> path=/opt/user2  
> state=directory  
> owner=user2  
> group=user2  
> mode=0755  
> when: ??? filter to only run if user2 exists ???  
> ignore\_errors: yes

here is the “output” of the passinfo variable:

---

<div class="post-metadata">

**Author:** ![igorc](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/igorc/32/5491_2.png) [@igorc](https://forum.ansible.com/u/igorc)\
**Post date:** [February 4, 2016, 7:21am UTC](https://forum.ansible.com/t/need-help-with-nested-variables-to-use-in-a-when-directive-inside-of-a-role-playbook/20425/2 "2016-02-04T07:21:41Z")

</div>

Try this:

- name: Ensure user directory exists  
file:  
path=/opt/{{ item.item }}  
state=directory  
owner={{ item.item }}  
group={{ item.item }}  
mode=0755  
with\_items: passinfo.results

not tested though.
