Ok, I guess a little background would help. I'm using the uri module to retrieve a list of hosts in an inventory from our Tower server. It returns the JSON output below. I've paired it down to just the pieces I care about. There are two servers in this inventory. I'd like to retrieve the id based on the server name.
ok: [localhost] => {
"host_list.json.results": [
{
"ansible_facts_modified": null,
"created": "2019-08-27T18:50:09.132071Z",
"description": "",
"enabled": true,
"has_active_failures": false,
"has_inventory_sources": false,
"id": 799,
"insights_system_id": null,
"instance_id": "",
"inventory": 18,
"last_job": null,
"last_job_host_summary": null,
"modified": "2019-08-27T18:50:09.132085Z",
"name": "server1",
},
{
"ansible_facts_modified": "2019-08-28T14:41:17.846100Z",
"created": "2019-08-26T19:28:55.187225Z",
"description": "",
"enabled": true,
"has_active_failures": false,
"has_inventory_sources": false,
"id": 790,
"insights_system_id": null,
"instance_id": "",
"inventory": 18,
"last_job": 7879,
"last_job_host_summary": 10273,
"modified": "2019-08-28T14:41:17.846235Z",
"name": "server2",
}
]
}
I tried json_query:
- set_fact:
host_id: "{{ host_list.json.results|json_query('[?name == 'server2'].id') }}"
It threw the following error:
fatal: [localhost]: FAILED! => {"msg": "template error while templating string: expected token ',', got 'server2'. String: {{ host_list.json.results|json_query('[?name == 'server2'].id') }}"}