# Iterating through stdout\_lines (jinja formatted data) and returning data

**URL:** <https://forum.ansible.com/t/iterating-through-stdout-lines-jinja-formatted-data-and-returning-data/29564>\
**Category:** Ansible Project\
**Tags:** ansible-project\
**Created:** [January 29, 2019, 8:53pm UTC](https://forum.ansible.com/t/iterating-through-stdout-lines-jinja-formatted-data-and-returning-data/29564 "2019-01-29T20:53:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 29, 2019, 8:53pm UTC](https://forum.ansible.com/t/iterating-through-stdout-lines-jinja-formatted-data-and-returning-data/29564/1 "2019-01-29T20:53:41Z")

</div>

> 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  
> &nbsp;&nbsp;gather\_facts: false
> 
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Get nagios user info  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: 'curl -XGET  
> "[http://nagios/nagiosxi/api/v1/system/user?apikey=&lt;my](http://nagios/nagiosxi/api/v1/system/user?apikey=&lt;my) api \>&pretty=1"'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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.

&nbsp;&nbsp;&nbsp;- name: Get nagios user infouri:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url: [http://nagios/nagiosxi/api/v1/system/user?apikey=&lt;my](http://nagios/nagiosxi/api/v1/system/user?apikey=&lt;my) api key\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;headers:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Content-Type: application/json  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: nagios\_userid

Then you can use nagios\_userid.json as an ordinary variable in Ansible.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [January 29, 2019, 9:52pm UTC](https://forum.ansible.com/t/iterating-through-stdout-lines-jinja-formatted-data-and-returning-data/29564/2 "2019-01-29T21:52:11Z")

</div>

Thank you… much easier than what I was doing.
