Ansible使用下面的Jinja2模板来生成命名区域配置文件。在执行剧本(使用模板模块)期间失败,但该错误对我来说有点神秘,因此我无法修复它。
{# This template defines a named zone based on the dictionnary of the containers metadata #}
@ IN SOA {{ net_search_domain }}. admin.{{ net_search_domain }}. (
{{ ansible_date_time['epoch'] }} ; serial
3600 ; refresh
1800 ; retry
604800 ; expire
600 ) ; ttl
{{ ansible_hostname }} IN A {{ ansible_{{net_int_dmz_untrusted}}['ipv4']['address'] }}
{{ ansible_hostname }} IN AAAA {{ ansible_{{net_int_dmz_untrusted}}['ipv6']['address'] }}
{% for items in net_containers %}
{{ item.value.rproxy_be }} IN A {{ item.value.ipv4_container }}
{{ item.value.rproxy_be }} IN AAAA {{ item.value.ipv6_container }}
{% if {{ item.value.rproxy_fe }} != {{ item.value.rproxy_be }} %}
{{ item.value.rproxy_fe }} IN A {{ ansible_{{net_int_dmz_untrusted}}['ipv4']['address'] }}
{{ item.value.rproxy_fe }} IN AAAA {{ ansible_{{net_int_dmz_untrusted}}['ipv6']['address'] }}
{% endif %}
{% endfor %}
rtr IN AAAA xxxx:yyyy:zzzz:wwww:208:a2ff:fe0e:d127
printer IN AAAA xxxx:yyyy:zzzz:wwww:3205:5CFF:FE7C:6240
Ansible运行时错误:
failed: [192.168.11.6] (item={'file': '/home/nicolas/Documents/Configurations/Ansible/server/roles/containers/templates/named_domain.j2', 'target': '/srv/docker/dns/example.net'}) => {"changed": false, "item": {"file": "/home/nicolas/Documents/Configurations/Ansible/server/roles/containers/templates/named_domain.j2", "target": "/srv/docker/dns/example.net"},
"msg": "AnsibleError: template error while templating string: expected token 'end of print statement', got '{'. String: {# This template defines a named zone based on the dictionnary of the containers metadata #}
\n@ IN SOA {{ net_search_domain }}. admin.{{ net_search_domain }}. (\n {{ ansible_date_time['epoch'] }} ; serial\n 3600 ; refresh
\n 1800 ; retry\n 604800 ; expire\n 600 ) ; ttl\n\n{{ ansible_hostname }} IN A {{ ansible_{{net_int_dmz_untrusted}}['ipv4']['address'] }}
\n{{ ansible_hostname }} IN AAAA {{ ansible_{{net_int_dmz_untrusted}}['ipv6']['address'] }}\n\n{% for items in net_containers %}\n{{ item.value.rproxy_be }} IN A {{ item.value.ipv4_container }}
\n{{ item.value.rproxy_be }} IN AAAA {{ item.value.ipv6_container }}\n{% if {{ item.value.rproxy_fe }} != {{ item.value.rproxy_be }} %}\n{{ item.value.rproxy_fe }} IN A {{ ansible_{{net_int_dmz_untrusted}}['ipv4']['address'] }}\n{{ item.value.rproxy_fe }} IN AAAA {{ ansible_{{net_int_dmz_untrusted}}['ipv6']['address'] }}
\n{% endif %}\n{% endfor %}\n\nrtr IN AAAA xxxx:yyyy:zzzz:wwww:208:a2ff:fe0e:d127\nprinter IN AAAA xxxx:yyyy:zzzz:wwww:5CFF:FE7C:6240\n\n\n"}
答案 0 :(得分:0)
{{ ansible_hostname }} IN A {{ ansible_{{net_int_dmz_untrusted}}['ipv4']['address'] }}
您不能使用这样的嵌套表达式,因为Jinja不会进行递归求值。要执行您要执行的操作,请将vars
视为dict
,然后以这种方式查找密钥:
{{ ansible_hostname }} IN A {{ vars["ansible_"+net_int_dmz_untrusted]['ipv4']['address'] }}
单独地,尽管您没有问这个,但是将这些表达式分配给变量确实比将其复制粘贴到整个地方更加快乐:
{# This template defines a named zone based on the dictionnary of the containers metadata #}
{% set the_addr4 = vars["ansible_"+net_int_dmz_untrusted]['ipv4']['address'] %}
{% set the_addr6 = vars["ansible_"+net_int_dmz_untrusted]['ipv6']['address'] %}
{{ ansible_hostname }} IN A {{ the_addr4 }}
{{ ansible_hostname }} IN AAAA {{ the_addr6 }}
答案 1 :(得分:0)
在第一个答案中添加答案可以解决一半错误。 下面的Jinja2模板是提供预期输出的最后一个模板:
@ IN SOA {{ net_search_domain }}. admin.{{ net_search_domain }}. (
{{ ansible_date_time['epoch'] }} ; serial
3600 ; refresh
1800 ; retry
604800 ; expire
600 ) ; ttl
{% set addr4 = hostvars[inventory_hostname]['ansible_default_ipv4']['address'] %}
{% set addr6 = hostvars[inventory_hostname]['ansible_default_ipv6']['address'] %}
{{ ansible_hostname }} IN A {{ addr4 }}
{{ ansible_hostname }} IN AAAA {{ addr6 }}
{% for item in net_containers %}
{{ net_containers[item].rproxy_be }} IN A {{ net_containers[item].ipv4_container }}
{{ net_containers[item].rproxy_be }} IN AAAA {{ net_containers[item].ipv6_container }}
{% if net_containers[item].rproxy_be != net_containers[item].rproxy_fe %}
{{ net_containers[item].rproxy_fe }} IN A {{ addr4 }}
{{ net_containers[item].rproxy_fe }} IN AAAA {{ addr6 }}
{% endif %}
{% endfor %}