Issue with conditionals

24 views
Skip to first unread message

Eugene Romero

unread,
Jan 4, 2016, 12:59:00 PM1/4/16
to Ansible Project
Hi guys! I'm experiencing an interesting issue that I can't figure out.

Every host in my inventory file has an "alias" variable assigned to it.

Now, I'm trying to run a template play on some of the hosts:

- name: Hosts file
  template:
    src=hosts.j2   
    dest=/etc/hosts
  when: (alias != "leia") or (alias != "luke") or (alias != "vader")

If I turn those conditionals from != to ==, the play only runs on those 3 servers as expected. But if I run it like it is above, it runs on ALL hosts.

What am I dong wrong?

Thanks in advance!

Josh Smift

unread,
Jan 4, 2016, 1:07:14 PM1/4/16
to ansible...@googlegroups.com
ER> when: (alias != "leia") or (alias != "luke") or (alias != "vader")
ER>
ER> If I turn those conditionals from != to ==, the play only runs on
ER> those 3 servers as expected. But if I run it like it is above, it runs
ER> on ALL hosts.

No matter what your alias is, at least one of those conditions will be
true (in practice, two or three of them will be), and the OR means that if
one of them is true, the whole 'when' is true.

-Josh (j...@care.com)



This email is intended for the person(s) to whom it is addressed and may contain information that is PRIVILEGED or CONFIDENTIAL. Any unauthorized use, distribution, copying, or disclosure by any person other than the addressee(s) is strictly prohibited. If you have received this email in error, please notify the sender immediately by return email and delete the message and any attachments from your system.

Brian Coca

unread,
Jan 4, 2016, 1:17:05 PM1/4/16
to Ansible Project
try this:

when: alias not in ['leia', 'luke', 'vader'}

^ so when alias is neither of those, which i think is what you wanted.


--
Brian Coca

Eugene Romero

unread,
Jan 4, 2016, 2:17:04 PM1/4/16
to Ansible Project
@Josh Smift:

Ahh, that explanation makes total sense. I see now why this didn't work. Thanks for that!

@ Brian Coca:

That worked beautifully. Thanks a lot!
Reply all
Reply to author
Forward
0 new messages