'Conditional or on string comparison not working in block
I have a playbook with a block where I am doing a string comparison on a block like so. Imagine my inventory_hostname is "hello-test.com", in other words this entire block should be skipped.
- name: Do cool stuff
block:
- name: Some tasks
set_fact: feeder="feeder.stuff.com"
when: ("'hello-prod.com' in inventory_hostname")
If I run it like above, it will skip it. However, If I run it with an or, like below it does not skip it even though inventory_hostname does not match either comparison
- name: Do cool stuff
block:
- name: Some tasks
set_fact: feeder="feeder.stuff.com"
when: ("'hello-prod.com' in inventory_hostname") or ("'hello-cool-prod.com' in inventory_hostname")
I've never had to use an or before in a when statement, what am I missing?
Solution 1:[1]
Your when: clause is actually messing around with quotes and I'm even surprised it does not fire an error. The correct syntax would be:
when: "'hello-prod.com' in inventory_hostname or 'hello-cool-prod.com' in inventory_hostname"
Meanwhile, you can shorten this condition and get rid off the enclosing double quotes by using the following
when: inventory_hostname is regex('hello(-cool)?-prod.com')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zeitounator |
