简体   繁体   中英

Jinja2 templating issue

Trying to set a fact in ansible using Jinja2.

Getting the following error

Error: template error while templating string: expected token ',', got ':'

PFB code

- 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

You mixed the syntax for lists and dicts when you extend lb_lstrn_map . And for dicts you should use update, not extend. It should be something like:

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

The braces are not balanced

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

Correct syntax

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM