How to have a space in a variable from a JSON payload returned by uri module

16 views
Skip to first unread message

Rasmus Sjørslev

unread,
Jul 5, 2017, 6:54:11 PM7/5/17
to Ansible Project
Im using the uri module to call a REST endpoint but the JSON structure returns contains a space which im not sure how to escape.

First the call i make and register:

    - name: Get subnet information (DNS, gateway, netmask etc.)
      uri:
        url: "{{ phpipam_url }}/subnets/7/"
        method: GET
        headers:
          token: "{{ ipam_login.json.data.token }}"
        status_code: 200
      register: subnet_info


subnet_info then contains the JSON payload:

"json": {
            "code": 200, 
            "data": {
                "DNSrecords": "1", 
                "DNSrecursive": "1", 
                "allowRequests": "1", 
                "calculation": {
                    "Broadcast": "192.168.111.255", 
                    "IP address": "/", 
                    "Max host IP": "192.168.111.254", 
                    "Min host IP": "192.168.111.1", 
                    "Network": "192.168.111.0", 
                    "Number of hosts": 254, 
                    "Subnet Class": "private C", 
                    "Subnet bitmask": "24", 
                    "Subnet netmask": "255.255.255.0", 
                    "Subnet wildcard": "0.0.0.255", 
                    "Type": "IPv4"
...

what i am after here is the Subnet netmask in the data.calculation but when i reference it like this:

"'{{ subnet_info.json.data.calculation.Subnet Netmask }}'"
or
'{{ subnet_info.json.data.calculation.Subnet Netmask }}'
or
"{{ subnet_info.json.data.calculation.Subnet Netmask }}"

i get the following error:

fatal: [localhost]: FAILED! => {
    "failed": true, 
    "msg": "template error while templating string: expected token 'end of print statement', got 'Netmask'. String: '{{ subnet_info.json.data.calculation.Subnet Netmask }}'"
}

how can i get to that JSON data that contains a space and reference that as a variable?

Kai Stian Olstad

unread,
Jul 6, 2017, 5:07:37 AM7/6/17
to ansible...@googlegroups.com
You can't use the dot notation when there are spaces, use square
brackets instead.

"{{ subnet_info.json.data.calculation['Subnet netmask'] }}"

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages