nschende
(Nick Schendel - Solution Architect, Red Hat)
1
Trying to wrap my head about how to query json results from a URI call & could use some guidance. I am guessing there are plenty in this group which this will be an easy question for.
I am registering the json response from a URI call to our backup system to ‘snapshotlist’
I am trying to get the value of “id” for the object with fileName that ends with host2retore_1.vmdk
This is what I currently have to try to pull that value out based on some example I found on a blog somewhere. However I get errors. Anyone able to help me figure out what I am doing wrong?
name: set variable for disk_snap_id
set_fact:
disk_snap_id: “{{ json_query(‘snapshotdetails.json.snapshotDiskDetails[?fileName.contains(@, ‘host2restore_1.vmdk’)].virtualDiskId’) }}”
Also, I don’t see any key called ‘snapshotdetails’.
And that json string should be filtered through from_json, ideally.
It looks like your “json” var isn’t really what you are using.
If I shuffle things around slightly, then below playbook works:
nschende
(Nick Schendel - Solution Architect, Red Hat)
3
Oops I said the wrong register variable name in my question. snapshotlist is the registered output of a previous task, snapshotdetails is the register of the task right before this particular task. the chunk of json I pasted in is pulled from the previous task’s output.
I was previously able to use snapshotdetails.json.snapshotDiskDetails[1].virtualDiskId to get a value in the play, it was the filtering that I have been struggling with.
Thanks for the reply I will give that a shot.
nschende
(Nick Schendel - Solution Architect, Red Hat)
4
Still struggling to get this working. Posting my entire playbook in case it can help understand what I might be doing wrong:
hosts: ‘{{inventory_hostname}}’
gather_facts: no
vars:
cluser_ip: “{{ rubrik_hostname }}”
tasks:
name: login to rubrik to generate token
uri:
url: https://{{ cluser_ip }}/api/v1/session # URL to generate an auth token to be used by subsequent calls
force_basic_auth: yes
user: ‘{{ lookup(“env”, “rubrik_username”) }}’
password: ‘{{ lookup(“env”, “rubrik_password”) }}’
method: POST
status_code: 200
return_content: yes
validate_certs: yes
register: authtoken # token is available as authtoken.json.token
delegate_to: localhost
become: no
name: get target vm id
uri:
url: https://{{ cluser_ip }}/api/v1/vmware/vm?name={{ inventory_hostname_nfq }}
headers:
Content-Type: “application/json”
Authorization: “Bearer {{ authtoken.json.token }}” # pass authentication token from earlier rather than basic auth
method: GET
status_code: 200
return_content: yes
validate_certs: yes
register: vminfo
delegate_to: localhost
become: no
name: set variable for target_vm_id
set_fact:
target_vm_id: “{{ vminfo.json.data[0].id }}”
name: get list of snapshots
uri:
url: https://{{ cluser_ip }}/api/v1/vmware/vm/{{ source_vm_id }}/snapshot
headers:
Content-Type: “application/json”
Authorization: “Bearer {{ authtoken.json.token }}” # pass authentication token from earlier rather than basic auth
method: GET
status_code: 200
return_content: yes
register: snapshotlist
delegate_to: localhost
become: no
name: get info on the snapshot to be used
uri:
url: https://{{ cluser_ip }}/api/v1/vmware/vm/snapshot/{{ snapshotlist.json.data[0].id }}
headers:
Content-Type: “application/json”
Authorization: “Bearer {{ authtoken.json.token }}” # pass authentication token from earlier rather than basic auth
method: GET
status_code: 200
return_content: yes
register: snapshotdetails
delegate_to: localhost
become: no
name: set variable for disk_snap_id
set_fact:
disk_snap_id: “{{ snapshotdetails | from_json | json_query(‘json.snapshotDiskDetails[?fileName.contains(@, ‘host2restore_1’)].virtualDiskId’) }}”
I also attempted setting the actual query string in a separate var, and passing that to the json_query as I have seen in many examples but that also did not work. not sure what I am doing wrong. A few permutations I have tried have run successfully but then returned a blank value. As you can see from the playbook snapshotdetails is a registered variable from the previous task.
The query for json_query is between single quotes. But you also use
single quotes to quote 'host2restore_1' - that doesn't work.
You need to use backticks instead.
If you look closely you can see that those were in the previous
example. I agree that it's not easy to spot
Dick
nschende
(Nick Schendel - Solution Architect, Red Hat)
6
Yup I absolutely did not notice that, thank you for pointing it out. now I am getting the following error:
fatal: [host2]: FAILED! => {
“msg”: “Unexpected templating type error occurred on ({{ snapshotdetails | from_json | json_query(‘json.snapshotDiskDetails[?fileName.contains(@, host2restore_1)].virtualDiskId’) }}): expected string or buffer”
}