# conditional.  when variable in variable

**URL:** <https://forum.ansible.com/t/conditional-when-variable-in-variable/28039>\
**Category:** Ansible Project\
**Created:** [July 12, 2018, 7:52pm UTC](https://forum.ansible.com/t/conditional-when-variable-in-variable/28039 "2018-07-12T19:52:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [July 12, 2018, 7:52pm UTC](https://forum.ansible.com/t/conditional-when-variable-in-variable/28039/1 "2018-07-12T19:52:21Z")

</div>

I wish to use a when statement that takes a variable passed in at command line (user), check it against a variable created with getent: (database: passwd). I am doing something wrong. I think the issue is two fold. 1. I think that I am not using my variables right in the when statement. 2. In this context, would I need to be pointing to local\_users.ansible\_facts.getent\_passwd? or something similar?:

`

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [July 12, 2018, 8:00pm UTC](https://forum.ansible.com/t/conditional-when-variable-in-variable/28039/2 "2018-07-12T20:00:30Z")

</div>

You only need the user task to do this.  
Ansible will remove the user if it exist, if the user doesn't exist it will just carry on.

So you only need this

&nbsp;&nbsp;&nbsp;- name: Purge user  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;user:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: "{{ user }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: absent  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;remove: yes

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [July 12, 2018, 8:03pm UTC](https://forum.ansible.com/t/conditional-when-variable-in-variable/28039/3 "2018-07-12T20:03:41Z")

</div>

I thought about that, don’t know why I didn’t just go with it. Out of curiosity though, what is wrong with my when statement (in case I use it in the future in a different scenario, I want to be able to reference this forum)?

---

<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:** [July 12, 2018, 8:06pm UTC](https://forum.ansible.com/t/conditional-when-variable-in-variable/28039/4 "2018-07-12T20:06:35Z")

</div>

the when is mostly fine, you are just making a bad assumption on the  
structure of local\_users:

when: user in local\_users['ansible\_facts']['getent\_passwd']

would be the 'corrected' version

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [July 12, 2018, 8:23pm UTC](https://forum.ansible.com/t/conditional-when-variable-in-variable/28039/5 "2018-07-12T20:23:05Z")

</div>

> when: user in local\_users[‘ansible\_facts’][‘getent\_passwd’]

Thank you Brian. Why local\_users[‘ansible\_facts’][‘getent\_passwd’] vs local\_users.ansible\_facts.getent\_passwd. Is that a preference? or is there reasoning behind it?

---

<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:** [July 12, 2018, 8:24pm UTC](https://forum.ansible.com/t/conditional-when-variable-in-variable/28039/6 "2018-07-12T20:24:14Z")

</div>

the first always works, the second is a 'jinja2' specific trick to  
allow dot notation, but it doesn't always work, mostly when you use a  
- in a name.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [July 12, 2018, 8:27pm UTC](https://forum.ansible.com/t/conditional-when-variable-in-variable/28039/7 "2018-07-12T20:27:57Z")

</div>

I see. Thank you!
