# Need help with conditionals and a dict (?) or variable...

**URL:** <https://forum.ansible.com/t/need-help-with-conditionals-and-a-dict-or-variable/20785>\
**Category:** Ansible Project\
**Created:** [March 6, 2016, 1:23pm UTC](https://forum.ansible.com/t/need-help-with-conditionals-and-a-dict-or-variable/20785 "2016-03-06T13:23:07Z")\
**Posts on this page:** 6\
**Page:** 1

<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:** [March 6, 2016, 1:23pm UTC](https://forum.ansible.com/t/need-help-with-conditionals-and-a-dict-or-variable/20785/1 "2016-03-06T13:23:07Z")

</div>

Hi everyone,

I need help getting a role to run. Especially the part where one host  
has a list of users defined, and the other host has not.

In the host\_vars for hostA I have this:

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 6, 2016, 1:51pm UTC](https://forum.ansible.com/t/need-help-with-conditionals-and-a-dict-or-variable/20785/2 "2016-03-06T13:51:53Z")

</div>

you made the default dict, it should be a list

with\_items: “{{ ssh\_users|default() }}”

ssh\_users is already a list so no problem there.

---

<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:** [March 6, 2016, 5:56pm UTC](https://forum.ansible.com/t/need-help-with-conditionals-and-a-dict-or-variable/20785/3 "2016-03-06T17:56:13Z")

</div>

Hi Brian,

thanks for the fast answer.

> you made the default dict, it should be a list
> 
> &nbsp;&nbsp;with\_items: "{{ ssh\_users|default() }}"
> 
> ssh\_users is already a list so no problem there.

Sorry, but I'm not sure I understand what you mean. Do you mean that  
my ssh\_users is a list and not a dict? Dict being somewhere along the  
lines of:

ssh\_users  
&nbsp;&nbsp;- user1: foo  
&nbsp;&nbsp;- user2: bar

Using 'default{()}' returns a dict, not a list, is that what you meant?

---

<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:** [March 6, 2016, 6:35pm UTC](https://forum.ansible.com/t/need-help-with-conditionals-and-a-dict-or-variable/20785/4 "2016-03-06T18:35:50Z")

</div>

I'll answer myself...

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 7, 2016, 6:31pm UTC](https://forum.ansible.com/t/need-help-with-conditionals-and-a-dict-or-variable/20785/5 "2016-03-07T18:31:47Z")

</div>

> - name: This runs if there is something ssh\_users  
> &nbsp;&nbsp;copy:  
> &nbsp;&nbsp;&nbsp;&nbsp;src='...'  
> &nbsp;&nbsp;&nbsp;&nbsp;dest='...'  
> &nbsp;&nbsp;with\_items: "{{ ssh\_users|default([1]) }}"  
> &nbsp;&nbsp;when: ssh\_users is defined  
> ################

​remove the when: it executes PER item, in the default you are forcing 1  
item and then skipping it.

​  
&nbsp;&nbsp;with\_items: "{{ ssh\_users|default() }}"

​ssh\_users is a list:

ssh\_users:  
&nbsp;&nbsp;- user1: foo  
&nbsp;&nbsp;- user2: bar

this is how it would be for a dict:

ssh\_users:  
&nbsp;&nbsp;user1: foo  
&nbsp;&nbsp;user2: bar  
​  
(no -, which signifies 'list item')

​^ this will skip the task due to the loop list being empty

​

---

<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:** [March 7, 2016, 7:14pm UTC](https://forum.ansible.com/t/need-help-with-conditionals-and-a-dict-or-variable/20785/6 "2016-03-07T19:14:12Z")

</div>

Hi Brian,

> > - name: This runs if there is something ssh\_users  
> > &nbsp;&nbsp;copy:  
> > &nbsp;&nbsp;&nbsp;&nbsp;src='...'  
> > &nbsp;&nbsp;&nbsp;&nbsp;dest='...'  
> > &nbsp;&nbsp;with\_items: "{{ ssh\_users|default([1]) }}"  
> > &nbsp;&nbsp;when: ssh\_users is defined  
> > ################
> 
> ​remove the when: it executes PER item, in the default you are forcing 1  
> item and then skipping it.

If I leave out the 'when'-statement (and use '...|default{}' without  
the 1), I do not get any hint in the ansible-playbook output whether  
this task is skipped or not.

> ​ssh\_users is a list:
> 
> ssh\_users:  
> &nbsp;&nbsp;- user1: foo  
> &nbsp;&nbsp;- user2: bar
> 
> this is how it would be for a dict:
> 
> ssh\_users:  
> &nbsp;&nbsp;user1: foo  
> &nbsp;&nbsp;user2: bar  
> ​  
> (no -, which signifies 'list item')

OK, now I got it. Thanks!

> ​^ this will skip the task due to the loop list being empty

I am not sure where this line belongs to, but I assume my example is  
not totally wrong and will not break with the next release, so Im good.

Thanks, Brian!

Johannes
