繁体   English   中英

ansible 库存组使用 jinja2

[英]ansible inventory groups using jinja2

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 %}

想要 output 或想要的 output

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

output 我在每个设备上都在下面,我的 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 同样的事情

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM