gpt4 book ai didi

ansible - Jinja2 语法错误 : expected token 'end of print statement'

转载 作者:行者123 更新时间:2023-12-03 08:26:13 28 4
gpt4 key购买 nike

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

最佳答案

在这里添加一个答案作为第一个答案可以解决一半的错误。
下面的 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 %}

关于ansible - Jinja2 语法错误 : expected token 'end of print statement' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55081219/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com