条件性或字符串上的比较在块中不起作用



我有一个带块的剧本,我在其中对这样的块进行字符串比较;hello-test.com";,换句话说,应该跳过整个块。

- name: Do cool stuff
block:

- name: Some tasks
set_fact: feeder="feeder.stuff.com"

when:  ("'hello-prod.com' in inventory_hostname")

如果我像上面那样运行它,它会跳过它。但是,如果我像下面那样用或运行它,即使inventory_hostname与任何一个比较都不匹配,它也不会跳过它

- 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")

我从来没有在when语句中使用过或,我缺少什么?

您的when:子句实际上在使用引号,我甚至很惊讶它没有引发错误。正确的语法是:

when: "'hello-prod.com' in inventory_hostname or 'hello-cool-prod.com' in inventory_hostname"

同时,您可以通过使用以下来缩短此条件并去掉封闭的双引号

when: inventory_hostname is regex('hello(-cool)?-prod.com')

最新更新