I am trying to use the JSON response from an API to lookup the UUID of a VM I am trying to back up using the hostname. The API returns a list of hashes which include the hostnames and UUID of that hostname. I'd like to get Ansible to return the UUID of a machine when I feed it the hostname.
Here is an example of the response from the API, which I am storing in networker_vms
{
"networker_vms.json.vms": [
{
"hostname": ""
},
{
"hostname": ""
},
{
"hostname": "server1.company.com",
"uuid": "1ef92350-a5e3-8df3-6106-208a88aad352"
},
{
"hostname": "server2.company.com",
"uuid": "5003006d-23f4-a515-3722-181238489896"
},
{
"hostname": "server3.company.com",
"uuid": "50640076-07ae-571d-5e69-2183db42d2ee"
},
{
"hostname": "server4.company.com",
"uuid": "500410b1-505a-7539-5efd-89131d671b0a"
},
{
"hostname": "server5.company.com",
"uuid": "5024014b-e788-12a9-bcf6-5475ae50bf7c"
},
{
"hostname": "server6.company.com",
"uuid": "51462200-1abf-1c09-5007-fa72f76854fc"
}
],
}
I am able to get the UUID that coincides with a hostname with the following Ansible snippet, however I cannot get the code to work when switching the "mynames" variable to something like {{ ansible_hostname }} - I get the error "unexpected failure during module execution". I'm guessing it has to do with the stacking of the curly-braces, but am not sure....
- name: debug my_uuid
debug:
msg: "{{ mynames | map('extract', dict(networker_vms.json | json_query('vms[].[hostname,uuid]'))) }}"
vars:
mynames:
- server2.company.com
What is the best way for me to search the results of the API call for a specific hostname and get Ansible to put the corresponding uuid into a fact or variable for me?
Thanks!