You should be able to have a task that delegates to localhost (if you want the CSV file local), and use a loop through the data in the variable + ansible.builtin.lineinfile to add lines
Donāt use lifeline, use a template with run_once and loop over all hosts, lineinfile does not handle parallelism and will end up overwriting entries.
template
host, date, hostname, certificate
{% for host in ansible_play_hosts %}
{% if 'result' in hostvars[host] %}
{{host}}, {{ hostvars[host]['result']['stdout'].split('\n', 3) | join(',') }}
{% endif %}
{% endfor %}
@jrglynn2 donāt use connection: local this only works if controller and targets are configured the same, but for those in heterogeneous environments that breaks, use delegate_to: localhost
(u'Thu Sep 26 18:12:43 UTC 2024', u'compute01.slc.csswx.nas.faa.gov', u'Dec 2 21:24:48 2033 GMT', u'Tue Dec 5 21:28:15 UTC 2023')
Where are the āuāā coming from? How can I get rid of them and the parens?
the u is for āpython unicode stringā, it happens when you stringify some python objects using their default representation. The parens and the u show that you are stringifying a tuple, which you are creating with the , s
Then youāve gotten lucky (or just havenāt noticed when it failed to work.) This approach is not safe and should not be used ever. Even if it seems to work perfectly for you it is inherently a flawed pattern that relies on luck to win the race.
IĀ“ve really only used lineinfile in conjunction with delegate_to: localhost and run_once: true as a gateway of last resort once - which should be ok if a specific module does not exist and template is unpractical. And yes, I would have noticed if it failed to work However, I can see that parallel writing to the same file is not a good idea.
I totally understand why lineinfile is not a great option. Iāve never used templating before. I tried to use the code example you gave me but failed horribly. What other options do I have? I need to run this script against 250+ VMs to collect the data I need.
We are not talking about lineinfile in the abstract, we are talking about the concrete approach of using it to update a single file on localhost for multiple hosts, since that is what you suggested doing. Specifically, this was your suggestion:
Then you have not used the construct you suggested, got incredibly lucky, or just didnāt notice when it failed. This is not about my experience, it is an inherent flaw in how that task works.
The argument arises because you provided conceptually broken tasks that should not be learned from, and will cause problems for people that can be subtle and hard to notice. I donāt know why youāre still arguing that itās a safe thing to do when itās demonstrably not, as shown in my immediately previous post. I didnāt have to try to break it or jump through hoops to get those results, I just ran it ten times and it failed on 4 of them.
Donāt write code with race conditions. Itās a bad idea.