Home > Back-end >  template error while templating string: expected name or number. String: when reading JSON using Ans
template error while templating string: expected name or number. String: when reading JSON using Ans

Time:03-16

Below is my sample this1.json

[
  {
    "?xml": {
      "attributes": {
        "encoding": "UTF-8",
        "version": "1.0"
      }
    }
  },
  {
    "domain": [
      {
        "name": "mydom"
      },
      {
        "domain-version": "12.2.1.3.0"
      },
      {
        "server": [
          {
            "name": "AdminServer"
          },
          {
            "ssl": {
              "name": "AdminServer"
            }
          },
          {
            "listen-port": "12400"
          },
          {
            "listen-address": "mydom.host1.bank.com"
          }
        ]
      },
      {
        "server": [
          {
            "name": "myserv1"
          },
          {
            "ssl": [
              {
                "name": "myserv1"
              },
              {
                "login-timeout-millis": "25000"
              }
            ]
          },
          {
            "log": [
              {
                "name": "myserv1"
              },
              {
                "file-name": "/web/bea_logs/domains/mydom/myserv1/myserv1.log"
              }
            ]
          }
        ]
      },
      {
        "server": [
          {
            "name": "myserv2"
          },
          {
            "ssl": {
              "name": "myserv2"
            }
          },
          {
            "reverse-dns-allowed": "false"
          },
          {
            "log": [
              {
                "name": "myserv2"
              },
              {
                "file-name": "/web/bea_logs/domains/mydom/myserv2/myserv2.log"
              }
            ]
          },
          {
            "server-start": [
              {
                "name": "CANVL01"
              },
              {
                "java-vendor": "Sun"
              },
              {
                "java-home": "/web/bea/platform1221/jdk"
              }
            ]
          }
        ]
      }
    ]
  }
]

The below code is able to extract file-name element from log array.

- hosts: localhost
  gather_facts: no
  vars:
    json: "{{ lookup('file', './this1.json') | from_json }}"
  tasks:
    - name: display
      debug:
        msg: "{{ server.0.name }} -> {{ filename.0.log[1]['file-name'] }}"
      loop: "{{ json[1].domain }}"
      vars:
        server: "{{ item.server | selectattr('name', 'defined') }}"
        filename:  "{{ item.server | selectattr('log', 'defined') }}"
      when: item.server is defined and (item.server | selectattr('log', 'defined')) != []

On similar lines I wish to extract java-home element of server-start array. Below is the code for the same:

 - name: display CPATH
   debug:
     msg: "{{ server.0.name }} -> {{ cpath.0.'server-start'[2]['java-home'] }}"
   loop: "{{ jsondata[1].domain }}"
   vars:
     server: "{{ item.server | selectattr('name', 'defined') }}"
     cpath:  "{{ item.server | selectattr('server-start', 'defined') }}"
   when: item.server is defined and (item.server | selectattr('server-start', 'defined')) != []

However, i get this error:

skipping: [localhost] => (item={'server': [{'name': 'myserv1'}, {'ssl': [{'name': 'myserv1'}, {'login-timeout-millis': '25000'}]}, {'log': [{'name': 'myserv1'}, {'file-name': '/web/bea_logs/domains/mydom/myserv1/myserv1.log'}]}]})
fatal: [localhost]: FAILED! => {"msg": "template error while templating string: expected name or number. String: {{ server.0.name }} -> {{ cpath.0.'server-start'[2]['java-home'] }}"}

I tried the following but none of them works:

     msg: "{{ server.0.name }} -> {{ cpath.0.\"server-start\"[2]['java-home'] }}"
     msg: "{{ server.0.name }} -> {{ cpath.0.server-start[2]['java-home'] }}"
     msg: "{{ server.0.name }} -> {{ cpath.0.'[server-start]'[2]['java-home'] }}"
     msg: "{{ server.0.name }} -> {{ cpath.0.[server-start][2]['java-home'] }}"
     msg: "{{ server.0.name }} -> {{ cpath.0.['server-start'][2]['java-home'] }}"

None of this worked. Can you please suggest?

Note: This is just a sample json and the position of server-start may vary. Hence using selectattr

CodePudding user response:

try this playbook:

- hosts: localhost
  gather_facts: no
  vars:
    json: "{{ lookup('file', './file2.json') | from_json }}"
  tasks:
    - name: display
      debug:
        msg: "{{ server.0.name }} -> {{ cpath[0]['server-start'][2]['java-home'] }}"
      loop: "{{ json[1].domain }}"
      vars:
        server: "{{ item.server | selectattr('name', 'defined') }}"
        cpath:  "{{ item.server | selectattr('server-start', 'defined') }}"
      when: item.server is defined and (item.server | selectattr('server-start', 'defined')) != []

result:

skipping: [localhost] => (item={'name': 'mydom'}) 
skipping: [localhost] => (item={'domain-version': '12.2.1.3.0'}) 
skipping: [localhost] => (item={'server': [{'name': 'AdminServer'}, {'ssl': {'name': 'AdminServer'}}, {'listen-port': '12400'}, {'listen-address': 'mydom.host1.bank.com'}]}) 
skipping: [localhost] => (item={'server': [{'name': 'myserv1'}, {'ssl': [{'name': 'myserv1'}, {'login-timeout-millis': '25000'}]}, {'log': [{'name': 'myserv1'}, {'file-name': '/web/bea_logs/domains/mydom/myserv1/myserv1.log'}]}]}) 
ok: [localhost] => (item={'server': [{'name': 'myserv2'}, {'ssl': {'name': 'myserv2'}}, {'reverse-dns-allowed': 'false'}, {'log': [{'name': 'myserv2'}, {'file-name': '/web/bea_logs/domains/mydom/myserv2/myserv2.log'}]}, {'server-start': [{'name': 'CANVL01'}, {'java-vendor': 'Sun'}, {'java-home': '/web/bea/platform1221/jdk'}]}]}) => {
    "msg": "myserv2 -> /web/bea/platform1221/jdk"
}
  • Related