Jinja2语法错误:预期令牌'打印结束语句'



2025-03-18 05:45:18 (25天前)
  1. Ansible使用以下Jinja2模板生成命名区域配置文件。它在剧本执行期间失败(使用模板模块),但错误对我来说有点神秘,所以我没有......

3 条回复
  1. 0# 子阳 | 2019-08-31 10-32





    {{ ansiblehostname }} IN A {{ ansible{{netint_dmz_untrusted}}[‘ipv4’][‘address’] }}




    你不能使用这样的嵌套表达式,因为Jinja不进行递归计算。为了做你想做的事,请对待

    vars

    作为一个

    dict

    并以这种方式查找关键:




    1. {{ ansible_hostname }} IN A {{ vars[“ansible
      “+net_int_dmz_untrusted][‘ipv4’][‘address’] }}

    2. </code>




    另外,虽然你没有问过这个问题,但你真的会更乐意将这些表达式分配给变量,而不是将它们复制粘贴到整个地方:




    1. {# This template defines a named zone based on the dictionnary of the containers metadata #}
      {% set theaddr4 = vars[“ansible“+netint_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 }}

    2. </code>

  2. 1# 哎?小查查 | 2019-08-31 10-32



    在这里添加答案作为第一个解决了错误的一半。
    下面的Jinja2模板是最后一个提供预期输出的模板:




    1. @ IN SOA {{ net_search_domain }}. admin.{{ net_search_domain }}. (
      {{ ansible_date_time[‘epoch’] }} ; serial
      3600 ; refresh
      1800 ; retry
      604800 ; expire
      600 ) ; ttl

    2. {% 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 }}

    3. {% 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 %}

    4. </code>

登录 后才能参与评论