Using lineinfile

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

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.

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

  • The AWX Team

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

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