Search code examples
ansibleescapingcharacterjinja2openstack

How to escape colon (":") in a variable name with Ansible or Jinja2?


I get Openstack networks information with Ansible module os_networks_facts.

This module returns information in an openstack_networks structure:

    "openstack_networks": [
    {
        "admin_state_up": true,
        "id": "5632dc44-dbda-4752-8155-fe782e95cc29",
        "mtu": 0,
        "name": "public_RSC",
        "port_security_enabled": true,
        "router:external": true,
        "shared": false,
        "status": "ACTIVE",
        "subnets": [
            "7b07432c-f0a0-415a-8b28-7e87918cc6d4",
            "a56e25cb-0710-4a64-869e-4af2d5bf9c64",
            "c4ff60af-44bc-4252-ab38-fd242d51f0f2"
        ],
        "tenant_id": "6025f8013cee46c093cb97cb36a1a86e"
    },
    {
        "admin_state_up": true,
        "id": "7812f951-4bc9-41c0-9db2-1f49b8a7ee47",
        "mtu": 0,
        "name": "kuby-network",
        "port_security_enabled": true,
        "router:external": false,
        "shared": false,
        "status": "ACTIVE",
        "subnets": [
            "6ad9ce9b-ba54-4d74-bbb6-8dfc50526eff"
        ],
        "tenant_id": "a9cffc26ba5a4a8e883f04dc7180a91d"
    }
]

I want to make a test on the value of "router:external" attribute. But, this attribute contains a colon in its name.

When I try to print it in Ansible with:

- hosts: localhost
  connection: local
  gather_facts: false

  tasks:
    - name: get network information
      os_networks_facts:
        cloud: "{{ InfraCloudName }}"

    - name: debug
      debug:
        msg: "{{ item.router:external }}"
      with_items: "{{ openstack_networks }}"

I have an error:

TASK [debug] ******************************************
fatal: [localhost]: FAILED! => {"msg": "template error while templating string: expected token 'end of print statement', got ':'. String: {{ item.router:external }}"}

When I try to test it in jija2 template with:

Networks
========

{% for n in openstack_networks %}
# {{ n.name }}

{% if n.router:external %}
{{ n.name }} is an external network.
{% endif %}

{% endfor %}

I have also an error:

fatal: [localhost]: FAILED! => {"changed": false, "msg": "AnsibleError: template error while templating string: expected token 'end of statement block', got 'external'. String: Networks\n========\n\n{% for n in openstack_networks %}\n# {{ n.name }}\n\n{% if n.router:external %}\n{{ n.name }} is an external network.\n{% endif %}\n\n{% endfor %}\n"}

So, is there a way to escape the ":" in the variable name ?


Solution

  • Remember that there are two syntaxes for accessing dictionary keys in Jinja. You can write this:

    variable.key
    

    Or you can wriet this:

    variable['key']
    

    Because in the second form the key is a quoted string, it can contain characters that are not valid in identifiers. So you want:

        - name: debug
          debug:
            msg: "{{ item['router:external'] }}"
          with_items: "{{ openstack_networks }}"