# Adding task output to a file in a loop

**URL:** <https://forum.ansible.com/t/adding-task-output-to-a-file-in-a-loop/32865>\
**Category:** Ansible Project\
**Created:** [May 6, 2020, 7:41pm UTC](https://forum.ansible.com/t/adding-task-output-to-a-file-in-a-loop/32865 "2020-05-06T19:41:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [May 6, 2020, 7:41pm UTC](https://forum.ansible.com/t/adding-task-output-to-a-file-in-a-loop/32865/1 "2020-05-06T19:41:54Z")

</div>

I have a shell script that I’m trying to convert to Ansible. It currently is similar to the following:

for uid in {1000…6500}  
user = ipa user-find --uid=$uid --raw |grep uid: | awk ‘{print $2}’  
echo “user,uid” \>\> uid.csv  
do

How do I accomplish this in Ansible? I know I can use the shell module with a loop similar to the following:

- name: Get user name based on the UID  
shell: “set -o pipefail && /bin/ipa user-find --uid={{ uid }} --raw | grep uid: | awk ‘{print $2}’”  
register: output  
loop: {{ range(1000, 6500, 1) | list }}

But how can I have each iteration add the values I need to a file? I know that I can use lineinfile, but I need to have it do this for each iteration of the loop.

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 7, 2020, 3:51am UTC](https://forum.ansible.com/t/adding-task-output-to-a-file-in-a-loop/32865/2 "2020-05-07T03:51:23Z")

</div>

I would use a template , you can use the registered results in a for  
loop inside jinja2.

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [May 7, 2020, 11:38am UTC](https://forum.ansible.com/t/adding-task-output-to-a-file-in-a-loop/32865/3 "2020-05-07T11:38:56Z")

</div>

Each time through the loop, will the registered output overwrite the previous values?

Harry

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [May 7, 2020, 3:04pm UTC](https://forum.ansible.com/t/adding-task-output-to-a-file-in-a-loop/32865/4 "2020-05-07T15:04:21Z")

</div>

> for uid in {1000..6500}  
> &nbsp;&nbsp;user = ipa user-find --uid=$uid --raw |grep uid: | awk '{print $2}'  
> &nbsp;&nbsp;echo "user,uid" \>\> uid.csv  
> do

This is the Ansible equivalent, I think.

The module "getent" creates the dictionary "getent\_passwd". Next "set\_fact"  
creates the list of dictionaries with attributes "user" and "uid" only. Next  
"set\_fact" selects required "uid" and module "template" writes the file.

&nbsp;&nbsp;- getent:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;database: passwd  
&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_list: "{{ getent\_passwd|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dict2items|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query('.{user: key, uid: to\_number(value[1])}')  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}}"  
&nbsp;&nbsp;- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;my\_list: "{{ my\_list|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query('[?uid \>= `1000`]')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_query('[?uid \<= `65000`]')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sort(attribute='uid')|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;list }}"  
&nbsp;&nbsp;- template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: uid.csv.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: uid.csv

This is the template

&nbsp;&nbsp;\> cat uid.csv.j2  
&nbsp;&nbsp;{% for item in my\_list %}  
&nbsp;&nbsp;{{ item.user }},{{ item.uid }}  
&nbsp;&nbsp;{% endfor %}

Notes

&nbsp;&nbsp;\* The function "to\_number" is needed to store uid as numbers  
&nbsp;&nbsp;&nbsp;&nbsp;[https://jmespath.readthedocs.io/en/latest/proposals/functions.html#to-number](https://jmespath.readthedocs.io/en/latest/proposals/functions.html#to-number)

&nbsp;&nbsp;\* The file "uid.csv" will be created on the remote host.

&nbsp;&nbsp;\* To create the file on the controller use "delegate\_to" and mark the file  
&nbsp;&nbsp;&nbsp;&nbsp;by the remote hostname. For example

&nbsp;&nbsp;- template:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: uid.csv.j2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ inventory\_hostname }}-uid.csv"  
&nbsp;&nbsp;&nbsp;&nbsp;delegate\_to: localhost

&nbsp;&nbsp;&nbsp;&nbsp;,or use module "fetch". See parameter "flat"  
&nbsp;&nbsp;&nbsp;&nbsp;[https://docs.ansible.com/ansible/latest/modules/fetch\_module.html#parameter-flat](https://docs.ansible.com/ansible/latest/modules/fetch_module.html#parameter-flat)

HTH,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [May 13, 2020, 5:09pm UTC](https://forum.ansible.com/t/adding-task-output-to-a-file-in-a-loop/32865/5 "2020-05-13T17:09:04Z")

</div>

OK, I’m finally getting back to this. I failed to mention that I’m trying to query our LDAP server, which is Red Hat IDM (based off of FreeIPA). Here’s what I have so far but its not working:
