簡體   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