我有几个可变文件,如下所示:
$ cat vars_ip.yml
---
12.1.1.25: TAP1
12.1.1.26: TAP2
11.0.0.26: TAP3
11.0.0.27: TAP4
9.2.2.211: SAT1
9.2.2.212: SAT2
10.7.7.28: SAT3
10.7.7.29: SAT4
9.2.2.213: SAT5
10.7.7.30: SAT6
$ cat vars.yml
---
layers:
- name: MY_LAYER
things_prod:
- 12.1.1.25
- 12.1.1.26
things_dr:
- 11.0.0.26
- 11.0.0.27
bgcolor:
- '#AED6F1'
- name: YR_LAYER
things_prod:
- 9.2.2.211
- 9.2.2.212
- 9.2.2.213
things_dr:
- 10.7.7.28
- 10.7.7.29
- 10.7.7.30
bgcolor:
- '#F9E79F'
这是我的剧本:
$ cat varloop.yml
---
- name: variables in Jinja2
hosts: localhost
vars_files:
- vars.yml
- vars_ip.yml
gather_facts: no
tasks:
- name: Create the Jinja2 based template
template: src=./varloop.j2 dest=./sync/sync.html
这是我的Jinja模板文件:
$ cat varloop.j2
{% for layer in layers %}
{% for item in layer.things_prod %}
{{ item }} {{ layer.things_dr[loop.index-1] }} var[{{ item }}] var[layer.things_dr[loop.index-1]]
{% endfor %}
{% endfor %}
当前输出:
$ cat ./sync/sync.html
12.1.1.25 11.0.0.26 var[12.1.1.25] var[layer.things_dr[loop.index-1]]
12.1.1.26 11.0.0.27 var[12.1.1.26] var[layer.things_dr[loop.index-1]]
9.2.2.211 10.7.7.28 var[9.2.2.211] var[layer.things_dr[loop.index-1]]
9.2.2.212 10.7.7.29 var[9.2.2.212] var[layer.things_dr[loop.index-1]]
9.2.2.213 10.7.7.30 var[9.2.2.213] var[layer.things_dr[loop.index-1]]
我需要将vars_ip.yml变量文件中的值填充到jinja模板中,如下所示:
我的预期输出:
12.1.1.25 11.0.0.26 TAP1 TAP3
12.1.1.26 11.0.0.27 TAP2 TAP4
9.2.2.211 10.7.7.28 WAS1 WAS3
9.2.2.212 10.7.7.29 WAS2 TAP4
9.2.2.213 10.7.7.30 WAS3 WAS6
该怎么做?
答案 0 :(得分:0)
@Vladimir Botka我正在尝试“ n”个可行的解决方案,然后才真正开始工作!!
这为我提供了值,并且IP地址被视为变量:
{% for layer in layers %}
{% for item in layer.things_prod %}
{{ item }} {{ layer.things_dr[loop.index-1] }} {{ vars[item] }} {{ vars[layer.things_dr[loop.index-1]] }}
{% endfor %}
{% endfor %}
输出:
12.1.1.25 11.0.0.26 TAP1 TAP3
12.1.1.26 11.0.0.27 TAP2 TAP4
9.2.2.211 10.7.7.28 SAT1 SAT3
9.2.2.212 10.7.7.29 SAT2 SAT4
9.2.2.213 10.7.7.30 SAT5 SAT6