我发现通过通配符选择变量的唯一方法是循环所有变量并测试match。例如
tasks:
- debug:
var: item
loop: "{{ query('dict', hostvars[inventory_hostname]) }}"
when: item.key is match("^.*_python_.*$")
> ansible-playbook test.yml | grep \"key\":
"key": "ansible_selinux_python_present",
"key": "ansible_python_version",
有更有效的方法吗?
json_query([?key =='name'])和 lookup('vars','name')都不适用于通配符。
还有其他“启用通配符”的测试,过滤器...吗?
注意:regex_search在What is the syntax within the regex_search() to match against a variable?中讨论
答案 0 :(得分:2)
- debug:
msg: "{{ lookup('vars', item) }}"
loop: "{{ hostvars[inventory_hostname].keys() | select('match', '^.*_python_.*$') | list }}"
给予:
ok: [localhost] => (item=ansible_selinux_python_present) => {
"msg": false
}
ok: [localhost] => (item=ansible_python_version) => {
"msg": "2.7.10"
}