Loading variables with data from REST

33 views
Skip to first unread message

Paul Hoffman

unread,
Sep 24, 2019, 2:25:45 PM9/24/19
to Ansible Project
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

Mark Anthony Garcia

unread,
Sep 24, 2019, 2:38:25 PM9/24/19
to ansible...@googlegroups.com
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 

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/cd69ac14-8216-441d-a413-6f4e42804892%40googlegroups.com.

Nick Schendel

unread,
Sep 24, 2019, 3:10:46 PM9/24/19
to Ansible Project
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 }}"

Paul Hoffman

unread,
Sep 24, 2019, 5:04:12 PM9/24/19
to Ansible Project
Thanks Nick,  
That worked great to give me back the value itself.

Brian Coca

unread,
Sep 25, 2019, 11:23:24 AM9/25/19
to Ansible Project
you can also use a lookup

varname: '{{ lookup("url" .... }}'


--
----------
Brian Coca

Reply all
Reply to author
Forward
0 new messages