Conditional based on a set of strings contained in the hostname

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

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

when: “String1” in ansible_hostname

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

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