I’d like to leverage systems with an API to provide variables to playbooks running on the controller.
Something similar to a dynamic inventory script loading from an external system.
For example with a curl command and can get the exact contents I need. IE: {“myVariable”:“myValue”} or yaml format:
myVariable: myValue
I’ve looked at the uri module and it looks promising. The values returned by the GET call are embedded in the
returned contents along with many other API variables.
The idea being when I need a value that changes frequently or shouldn’t be included in static playbooks I call the
external system that has the master value and load it.
I’d prefer not pulling the data into a variable file first. That would avoid file management issues.
I’m not too familiar with jinja2 but perhaps there’s a way to take the output from URI and parse out the values.
I’m sure there is a way to do this that I haven’t come across yet.
If anyone has already solved this point me in the right direction.
v/r
I’ve used this using uri module, fust to get the cookies and use the cookie to run several api queries. I used jinja to template the data for API PUT and POST but it’s a bit tricky with the formating.
I’m not in front of my pc but perhaps first thing tomorrow I’ll share what I have.
Mark Garcia
nschende
(Nick Schendel - Solution Architect, Red Hat)
3
I am working on some playbooks right now using the URI module and doing what you describe. If you register the output of the task, you can call the response contents with output.json.keyname. Where output is the name you used in the register. So as an example:
name: get some value via URI call
uri:
url: https://{{ server_hostname }}/api/somepath
register: uri_output
Now in later plays you can use uri_output.json.keyx, or uri_output.somekey for values returned in the response body itself. Some of the stuff I am doing will return a json aray, and for those I am using uri_output.json.data[0].key to get at what I need. Now I just need to figure out how to query the returned array to match a particular value.
I was also able to set a specific value to a variable with set_fact:
name: set variable for target_vm_id
set_fact:
target_vm_id: “{{ vminfo.json.data[0].id }}”