# Using lineinfile

**URL:** <https://forum.ansible.com/t/using-lineinfile/37095>\
**Category:** AWX Project\
**Created:** [September 14, 2022, 4:30pm UTC](https://forum.ansible.com/t/using-lineinfile/37095 "2022-09-14T16:30:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bill\_Biker](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bill_biker/32/8087_2.png) [@Bill\_Biker](https://forum.ansible.com/u/Bill_Biker)\
**Post date:** [September 14, 2022, 4:30pm UTC](https://forum.ansible.com/t/using-lineinfile/37095/1 "2022-09-14T16:30:54Z")

</div>

Is it possible to use a variable for “line” in the lineinfile module?  
I am trying to create a csv file with a header - this works as I used only text for the header

Then I want to add comma separated data after the header line, but using a

collection of variables from ios get facts, like {{ansible\_net\_hostname }}, etc.

I have not get got this to work - I can create the file using lineinfile, with the header text, but  
cannot add any data to the file using variable,

Thanks for a great forum,

ewholz

---

<div class="post-metadata">

**Author:** ![Nuno\_Jordao](https://avatars.discourse-cdn.com/v4/letter/n/a4c791/32.png) [@Nuno\_Jordao](https://forum.ansible.com/u/Nuno_Jordao)\
**Post date:** [September 14, 2022, 5:46pm UTC](https://forum.ansible.com/t/using-lineinfile/37095/2 "2022-09-14T17:46:34Z")

</div>

Hello,

Do you want to collect data from different hosts and write the csv in the controller?  
You could use a template like this to create the file in the localhost:

hostname;col1;col2  
{% for myhost in ansible\_play\_hosts -%}  
{{ myhost }};{{ hostvars[myhost].data1 }};{{ hostvars[myhost].data2 }}  
{% endfor %}

and then use the template module to create the file in the localhost or the template lookup.

Regards.

---

<div class="post-metadata">

**Author:** ![AWX\_Project](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@AWX\_Project](https://forum.ansible.com/u/AWX_Project)\
**Post date:** [September 14, 2022, 6:58pm UTC](https://forum.ansible.com/t/using-lineinfile/37095/3 "2022-09-14T18:58:17Z")

</div>

Hello, your question seems to be about using Ansible, not about AWX. [https://groups.google.com/g/ansible-project](https://groups.google.com/g/ansible-project) is the best place to visit for user questions about Ansible. Thanks!

- The AWX Team

---

<div class="post-metadata">

**Author:** ![Bill\_Biker](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bill_biker/32/8087_2.png) [@Bill\_Biker](https://forum.ansible.com/u/Bill_Biker)\
**Post date:** [September 15, 2022, 3:08pm UTC](https://forum.ansible.com/t/using-lineinfile/37095/4 "2022-09-15T15:08:55Z")

</div>

This is about using AWX-OPERATOR aka AWX aka Red Hat AAP  
I do want to capture data for several hosts - I will try you suggestion - thanks so much for the replies

ewholz

---

<div class="post-metadata">

**Author:** ![Bill\_Biker](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bill_biker/32/8087_2.png) [@Bill\_Biker](https://forum.ansible.com/u/Bill_Biker)\
**Post date:** [September 20, 2022, 4:43pm UTC](https://forum.ansible.com/t/using-lineinfile/37095/5 "2022-09-20T16:43:07Z")

</div>

Thanks for tip Nuno!  
put me on the right track, and finally got it to work.  
Thanks again,  
ewholz
