【问题标题】:Jinja2 templating issueJinja2 模板问题
【发布时间】:2020-03-11 19:51:07
【问题描述】:

尝试使用 Jinja2 在 ansible 中设置一个事实。

得到以下错误

错误:模板化字符串时出现模板错误:预期的标记',',得到':'

PFB代码

- set_fact:
    lb_lstnr_map: []

- name: "Build listeners map"
  set_fact:
    lb_lstnr_map: >-
      {%- if item == 443 and cert_arn -%}
        {{  lb_lstrn_map.extend({
          'Protocol': 'HTTPS',
          'Port': 443,
          'DefaultActions': [ { 'Type': 'forward', 'TargetGroupName': tg_name } ],
          'SslPolicy': ssl_policy,
          'Certificates': [ { 'CertificateArn': cert_arn } ] })
         }}
        {%- else -%}
        {{  lb_lstrn_map.extend({
          'Protocol': 'TCP' if lb_type = 'network' else 'HTTP',
          'Port': item,
          'DefaultActions': [ {'Type': 'forward', 'TargetGroupName': tg_name } ]
          }
          })
        }}
      {% endif %}
  with_items: lb_listeners

【问题讨论】:

    标签: python ansible jinja2


    【解决方案1】:

    您在扩展 lb_lstrn_map 时混合了列表和字典的语法。对于字典,您应该使用更新,而不是扩展。它应该是这样的:

    lb_lstrn_map.update({'Protocol': 'HTTPS', 'Port': 443,
                           'DefaultActions': {
                           ['Type': 'forward', 'TargetGroupName': tg_name ]
                           },
                          'SslPolicy': ssl_policy,
                          'Certificates': ['CertificateArn': cert_arn],
                        })
    

    【讨论】:

    • 我已经按照建议改了,但还是一样的错误
    【解决方案2】:

    大括号不平衡

            {{  lb_lstrn_map.extend({
              'Protocol': 'TCP' if lb_type = 'network' else 'HTTP',
              'Port': item,
              'DefaultActions': [ {'Type': 'forward', 'TargetGroupName': tg_name } ]
              }
              })
            }}
    

    正确的语法

            {{  lb_lstrn_map.extend({
              'Protocol': 'TCP' if lb_type = 'network' else 'HTTP',
              'Port': item,
              'DefaultActions': [ {'Type': 'forward', 'TargetGroupName': tg_name } ]
              })
            }}
    

    【讨论】:

      猜你喜欢
      • 2019-02-27
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-06
      • 1970-01-01
      • 2022-01-12
      相关资源
      最近更新 更多