Conditional based on a set of strings contained in the hostname

4,490 views
Skip to first unread message

David Brossard

unread,
Oct 23, 2013, 7:01:14 PM10/23/13
to ansible...@googlegroups.com
I've been racking my brain on this one but I haven't been able to come up with an answer.

My Devs have named machines based on their task. I know you can set a conditional based on the hostname such as:

  - name: Testing
    shell: echo "String Found!"
    when_string: '"String1" in "${ansible_hostname}"'

which will match String1.mycompany.com but not String2.mycompany.com or Different.mycompany.com

However, I need to execute that same command on hosts with more than just String1, say String2 and Different also. I tried using "or" but that is not supported apparently with when_string: the same way it is with when:

This works:
when: somevar == 'String1' or somevar == 'String2' or somevar == 'Different'

But this does not:

when_string: '"String1" in "${ansible_hostname}"' or '"String2" in "${ansible_hostname}"' or '"Different" in "${ansible_hostname}"'

Anybody have an idea to get this working?

Thanks

James Cammarata

unread,
Oct 24, 2013, 12:21:26 PM10/24/13
to ansible...@googlegroups.com
All of the when_* checks are going to be deprecated going forward. Just use the (much easier) "when:" syntax:

when: "String1" in ansible_hostname





--
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.
For more options, visit https://groups.google.com/groups/opt_out.



--

James Cammarata <jcamm...@ansibleworks.com>
Sr. Software Engineer, AnsibleWorks, Inc.
http://www.ansibleworks.com/

James Cammarata

unread,
Oct 24, 2013, 1:00:18 PM10/24/13
to ansible...@googlegroups.com
I just realized I forgot to correct for a syntax error. With YAML, if the line starts with a quote (as mine does above), you'll need to quote the entire line:

when: '"String1" in ansible_hostname'

Alternatively, you can use the continuation character:

when: >
  "String1" in ansible_hostname

David Brossard

unread,
Oct 24, 2013, 5:58:48 PM10/24/13
to ansible...@googlegroups.com
Thanks! That is exactly what I was looking for. The "or" operand works with when:
Reply all
Reply to author
Forward
0 new messages