'when' not working with 'with_items' ?

41 views
Skip to first unread message

Evie Kassela

unread,
May 22, 2014, 3:06:41 PM5/22/14
to ansible...@googlegroups.com

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?

Michael DeHaan

unread,
May 23, 2014, 8:10:40 AM5/23/14
to ansible...@googlegroups.com
when works fine with with_items.

It is evaluated once per item.

This syntax "with_items: groups['dbservers'][1::]" is however, entirely invalid.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ba7edb61-9699-4199-ab0d-9e7f7303f165%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Evie Kassela

unread,
May 24, 2014, 12:12:46 PM5/24/14
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
May 25, 2014, 4:56:03 PM5/25/14
to ansible...@googlegroups.com
"     is_master: "{{'-1' in inventory_hostname }}"

is_master: "-1 in inventory_hostname"

when: "{{ is_master }}"

That should cause your extra level of indirection.





--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages