# Conditional based on a set of strings contained in the hostname

**URL:** <https://forum.ansible.com/t/conditional-based-on-a-set-of-strings-contained-in-the-hostname/13756>\
**Category:** Ansible Project\
**Created:** [October 23, 2013, 11:01pm UTC](https://forum.ansible.com/t/conditional-based-on-a-set-of-strings-contained-in-the-hostname/13756 "2013-10-23T23:01:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Brossard](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@David\_Brossard](https://forum.ansible.com/u/David_Brossard)\
**Post date:** [October 23, 2013, 11:01pm UTC](https://forum.ansible.com/t/conditional-based-on-a-set-of-strings-contained-in-the-hostname/13756/1 "2013-10-23T23:01:14Z")

</div>

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](http://String1.mycompany.com) but not [String2.mycompany.com](http://String2.mycompany.com) or [Different.mycompany.com](http://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

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [October 24, 2013, 4:21pm UTC](https://forum.ansible.com/t/conditional-based-on-a-set-of-strings-contained-in-the-hostname/13756/2 "2013-10-24T16:21:26Z")

</div>

All of the when\_\* checks are going to be deprecated going forward. Just use the (much easier) “when:” syntax:

when: “String1” in ansible\_hostname

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [October 24, 2013, 5:00pm UTC](https://forum.ansible.com/t/conditional-based-on-a-set-of-strings-contained-in-the-hostname/13756/3 "2013-10-24T17:00:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![David\_Brossard](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@David\_Brossard](https://forum.ansible.com/u/David_Brossard)\
**Post date:** [October 24, 2013, 9:58pm UTC](https://forum.ansible.com/t/conditional-based-on-a-set-of-strings-contained-in-the-hostname/13756/4 "2013-10-24T21:58:48Z")

</div>

Thanks! That is exactly what I was looking for. The “or” operand works with when:
