ansible - Ansible 变量通配符选择
问题描述
我发现通过通配符选择变量的唯一方法是循环所有变量并测试match。例如
tasks:
- debug:
var: item
loop: "{{ query('dict', hostvars[inventory_hostname]) }}"
when: item.key is match("^.*_python_.*$")
shell> ansible-playbook test.yml | grep key:
key: ansible_python_interpreter
key: ansible_python_version
key: ansible_selinux_python_present
- 有没有更有效的方法来做到这一点?
json_query ([?key=='name'])和lookup('vars', 'name')都不能使用通配符。
- 是否有任何其他“启用通配符”的测试,过滤器......?
注意:regex_search在 regex_search() 中匹配变量的语法是什么?
解决方案
- 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"
}
推荐阅读
- ios - 苹果登录后退出
- iis - iis url rewrite - 如何匹配以“?”开头的模式
- python - CSV 文件中列的顺序是否会影响我的神经网络的性能?
- android - recyclerview 仅在第二次单击 kotlin 中的另一个按钮时显示
- laravel - Laravel - 以删除模式形式提交按钮没有响应
- java - 简单的循环列表
- nginx - 在 nginx 中,如何允许任何字符串作为 http 方法?
- tfs - 我们如何修复 Azure DevOps SonarQube 错误:类路径包含多个 SLF4J 绑定
- javascript - 如何从多个对象中找到一个字符串?
- tensorflow - 有没有办法在服务器中只加载一次 tensorflow 模型,而不是每次都使用新的传入请求加载模型?