I am trying to get my attribute value from JSON data.
Here is my JSON data:
{
"user1": "{\n \"data\":[\n {\n \"secure\": [\n {\n \"key\": \"-----BEGIN KEY-----\nMIIEowIBAAKCAQEAgOh+Afb0oQEnvHifHuzBwhCP3\n-----END KEY-----\"\n }\n ],\n \"owner\": \"shallake\",\n \"position\": \"gl\"\n }\n ]\n}"
}
Initially, I had invalid JSON data, so I converted it into valid JSON using to_json
and from_json
. The above JSON data is the result that I get.
code:
- set_fact:
user: "{{ lookup('file','filepath/myfile.json') | to_json }}"
- set_fact:
user1: "{{ user | from_json}}"
- set_fact:
user3: "{{ item }}"
with_items: "{{ user1['data'] | map(attribute='position') | list }}"
In a third set_fact
, I am trying to get position attribute value. But it shows an error like this:
the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'item' is undefined
"\"{\n \\"data\\":[\n {\n \\"secure\\": [\n {\n \\"key\\": \\"-----BEGINPRIVATE KEY-----\nMIIEowIBAAKCAQEAgOh+Afb0oQEnvHifHuzBwl+Tiu8LXoJXb/ii/eh\ngYEP3\n-----END PRIVATE KEY-----\\"\n }\n ],\n \\"owner\\": \\"shalloke\\",\n \\"position\\": \\"gl\\"\n }\n ]\n}\n\n\""
So how can I get a position value from the above JSON data result using Ansible loop?