Re: [ansible-project] Iterating through stdout_lines (jinja formatted data) and returning data

10 views
Skip to first unread message

Kai Stian Olstad

unread,
Jan 29, 2019, 3:53:52 PM1/29/19
to ansible...@googlegroups.com
On 29.01.2019 21:19, John Harmon wrote:
> I wish to automate user removal from nagios. This can be done via
> their
> REST api; however, I first need to find the user, grab their user_id,
> and
> then I can perform the delete based on the user_id. Their API returns
> the
> info in jinja format.

It's not Jinja, its JSON.


> I don't know how to iterate through the output, find
> the username, and grab the correlating user_id. For example, how can I
> obtain the user_id for the user: somegal?
>
> My playbook:
> ---
> - hosts: nagios
> gather_facts: false
>
> tasks:
> - name: Get nagios user info
> shell: 'curl -XGET
> "http://nagios/nagiosxi/api/v1/system/user?apikey=<my api
> key>&pretty=1"'
> register: nagios_userid

You should use the uri module since it gives you the result in .json
Never used Nagios api, but looks like is just a ordinary REST api.
So this should work.

- name: Get nagios user infouri:
url: http://nagios/nagiosxi/api/v1/system/user?apikey=<my api key>
headers:
Content-Type: application/json
register: nagios_userid

Then you can use nagios_userid.json as an ordinary variable in Ansible.


--
Kai Stian Olstad

John Harmon

unread,
Jan 29, 2019, 4:52:12 PM1/29/19
to Ansible Project
Thank you... much easier than what I was doing.
Reply all
Reply to author
Forward
0 new messages