簡體   English   中英

Jinja2 模板問題

[英]Jinja2 templating issue

嘗試使用 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

當您擴展lb_lstrn_map時,您混合了列表和字典的語法。 對於 dicts,您應該使用更新,而不是擴展。 它應該是這樣的:

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

牙套不平衡

        {{  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 } ]
          })
        }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM