如何解决使用 jinja2 的 ansible 库存组
devices:
children:
abcgroup:
children:
xyzgroup:
children:
group1:
hosts:
device509:
ansible_ssh_host: 1.1.1.1
device510:
ansible_ssh_host: 2.2.2.2
group2:
hosts:
devcice609:
ansible_ssh_host: 3.3.3.3
device610:
ansible_ssh_host: 4.4.4.4
defgroup:
hosts:
device0508:
ansible_ssh_host: 30.30.30.30
device0608:
ansible_ssh_host: 31.31.31.31
我的 jinja2 模板
peer-keepalive destination {% if 'group1' in group_names %}{{ansible_ssh_host[1]}} source {{ansible_ssh_host}} {% endif %} peer-keepalive destination {% if 'group2' in group_names %}{{ansible_ssh_host[1]}} source {{ansible_ssh_host}} {% endif %}
on device509 i should get below output
peer-keepalive destination 2.2.2.2 source 1.1.1.1
on device510 i should get below output
peer-keepalive destination 1.1.1.1 source 2.2.2.2
on device609 i should get below output
peer-keepalive destination 4.4.4.4 source 3.3.3.3
on device610 i should get below output
peer-keepalive destination 3.3.3.3 source 4.4.4.4
我在每个设备上得到的输出如下,我的 jinja2 模板是错误的,我得到了点 (.)
on device509 i get below peer-keepalive destination . source 1.1.1.1 device510 i get below peer-keepalive destination . source 2.2.2.2 on device609 i get below peer-keepalive destination . source 3.3.3.3 device610 i get below peer-keepalive destination . source 4.4.4.4
如何解决这个问题
解决方法
这对我有用
peer-keepalive destination {{ hostvars[groups['group1'][1]].ansible_ssh_host }} source {{ hostvars[groups['group1'][0]].ansible_ssh_host }}
peer-keepalive destination {{ hostvars[groups['group1'][0]].ansible_ssh_host }} source {{ hostvars[groups['group1'][1]].ansible_ssh_host }}
group2 也一样
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。