Ansible 条件 - 通配符匹配字符串

emm*_*dee 11 ansible ansible-playbook

我在 Ansible 任务中有以下条件:

when: ec2_tag_Name == 'testhost01'
Run Code Online (Sandbox Code Playgroud)

它工作正常,但是我想在ec2_tag_Name场上匹配通配符。

所以像这样

when: ec2_tag_Name == 'testhost*'
Run Code Online (Sandbox Code Playgroud)

目标是匹配诸如testhostx testhost12 testhostABCetc 之类的任何内容testhost,仅匹配字符串开头的任何内容。

这可能吗?似乎无法让它工作。

Ger*_*der 12

测试字符串

要将字符串与子字符串或正则表达式匹配,请使用“匹配”或“搜索”过滤器

在你的情况下:

when: ec2_tag_Name is match("testhost.*")
Run Code Online (Sandbox Code Playgroud)

  • 我还发现 `when: 'testhost' in ec2_tag_Name` 可以做类似的事情,它不限制它位于字符串的开头 - 所以你的方法绝对是正确的方法。谢谢 (2认同)

小智 5

这也有效。

when: "ec2_tag_Name.startswith('testhost')"
Run Code Online (Sandbox Code Playgroud)

您可以组合逻辑运算符以及