Lee*_*Fox 5 ansible ansible-playbook
这篇来自 Ansible.com 的文章展示了如何迭代模板文件中的组:https : //support.ansible.com/hc/en-us/articles/201957887-How-to-loop-over-a-组内主机列表模板-
它显示了以下代码:
{% for host in groups['db_servers'] %}
{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但我想要迭代的服务器是通过多个组定义的。所以想象一下,我想遍历 db_servers 和 qa 组中的所有服务器。我怎样才能做到这一点?
我试图以与我在剧本中相同的方式指定组的交集,但这不起作用。所以,当我尝试:
{% for host in groups['db_servers:&qa'] %}
{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
fatal: [54.173.247.115] => {'msg': "AnsibleUndefinedVariable: One or more undefined variables: 'dict object' has no attribute 'db_servers:&qa'", 'failed': True}
Run Code Online (Sandbox Code Playgroud)
关于如何迭代模板文件中的多个组的任何建议?
Ansible 有intersect过滤器。请参阅集合理论过滤器。
{% for host in groups['db_servers'] | intersect(groups['qa']) %}
{{ hostvars[host]['ansible_eth0']['ipv4']['address'] }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3510 次 |
| 最近记录: |