# 'when' not working with 'with\_items' ?

**URL:** <https://forum.ansible.com/t/when-not-working-with-with-items/15307>\
**Category:** Ansible Project\
**Created:** [May 22, 2014, 7:06pm UTC](https://forum.ansible.com/t/when-not-working-with-with-items/15307 "2014-05-22T19:06:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evie\_Kassela](https://avatars.discourse-cdn.com/v4/letter/e/7feea3/32.png) [@Evie\_Kassela](https://forum.ansible.com/u/Evie_Kassela)\
**Post date:** [May 22, 2014, 7:06pm UTC](https://forum.ansible.com/t/when-not-working-with-with-items/15307/1 "2014-05-22T19:06:41Z")

</div>

I have this task in a play for group ‘dbservers’:

name: test  
debug: msg=‘{{ is\_master }}’  
sudo: true  
sudo\_user: postgres  
when: is\_master  
with\_items: groups[‘dbservers’][1::]

I wrote this because I wanted only master ( which is groups[‘dbservers’][0] with hostname ‘pgserver-1’ ) to show the message for every slave.

is\_master is evaluated before based on the hostname.

With 1 master and 1 slave it doesn’t work:

TASK: [database | test] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
\<…\> ESTABLISH CONNECTION FOR USER: root  
\<…\> ESTABLISH CONNECTION FOR USER: root  
ok: [pgserver-2] =\> (item=pgserver-2) =\> {  
“item”: “pgserver-2”,  
“msg”: “False”  
}  
ok: [pgserver-1] =\> (item=pgserver-2) =\> {  
“item”: “pgserver-2”,  
“msg”: “True”  
}

The first message shouldn’t be shown but be skipped. It’s like ‘when’ isn’t working but is\_master is correctly evaluated. what am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 23, 2014, 12:10pm UTC](https://forum.ansible.com/t/when-not-working-with-with-items/15307/2 "2014-05-23T12:10:36Z")

</div>

when works fine with with\_items.

It is evaluated once per item.

This syntax “with\_items: groups[‘dbservers’][1::]” is however, entirely invalid.

---

<div class="post-metadata">

**Author:** ![Evie\_Kassela](https://avatars.discourse-cdn.com/v4/letter/e/7feea3/32.png) [@Evie\_Kassela](https://forum.ansible.com/u/Evie_Kassela)\
**Post date:** [May 24, 2014, 4:12pm UTC](https://forum.ansible.com/t/when-not-working-with-with-items/15307/3 "2014-05-24T16:12:46Z")

</div>

Curiously this syntax seems to be working. Should I change it anyway?

The problem finally seems to be that ‘when’ doesn’t get evaluated…This is how is\_master is set:  
is\_master: “{{‘-1’ in inventory\_hostname }}”  
and gets a value of ‘True’ of ‘False’…Does it need to be ‘true’ or ‘false’ to work?  
It worked with this:  
when: is\_master == ‘True’  
but i don’t understand why, this code was working a few days ago and now it’s not…

Thanks

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 25, 2014, 8:56pm UTC](https://forum.ansible.com/t/when-not-working-with-with-items/15307/4 "2014-05-25T20:56:00Z")

</div>

" is\_master: “{{‘-1’ in inventory\_hostname }}”

is\_master: “-1 in inventory\_hostname”

when: “{{ is\_master }}”

That should cause your extra level of indirection.
