我需要通过jinja模板创建文件,但遇到一些问题。
我的变量
my:
acl:
- name: test
allow:
- 0.0.0.0
deny:
- 1.1.1.1
- name: china
allow:
- 1.2.3.4
deny:
- 10.10.10.10
我的任务:
- name: Create acl file
template:
force: yes
src: acl.conf.j2
dest: "/etc/nginx/conf.d/{{ item.name }}.conf"
become: yes
with_items:
- "{{ my.acl }}"
我的模板
{% for allow in my.acl %}
allow {{allow.allow}};
{% endfor %}
{% for deny in my.acl %}
deny {{deny.deny}};
{% endfor %}
结果china.conf
allow ['0.0.0.0'];
allow ['1.2.3.4'];
deny ['1.1.1.1'];
deny ['10.10.10.10'];
结果test.conf
allow ['0.0.0.0'];
allow ['1.2.3.4'];
deny ['1.1.1.1'];
deny ['10.10.10.10'];
我只需要在china文件中仅在对象china中定义的ip地址,而无需使用['']
我怎样才能做到这一点?
修复模板
{% for allow in item.allow %}
allow {{ allow }};
{% endfor %}
{% for deny in item.deny %}
deny {{ deny }};
{% endfor %}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句