5

I have a playbook that registers three variables. I want to produce a CSV report of those three variables on all hosts in my inventory.

This SO answer suggests to use:

- local_action: copy content={{ foo_result }} dest=/path/to/destination/file

But that does not append to the csv file. Also, I have to manually compose by comma separators in this case.

Any ideas on how to log (append) variables to a local file?

Community
  • 1
  • 1
Jev Björsell
  • 891
  • 1
  • 9
  • 25
  • It sounds like a use case for the [template](http://docs.ansible.com/ansible/template_module.html) . I am not sure if it works with csv. – ThoFin Nov 27 '15 at 07:13

1 Answers1

6

If you are wanting to append a line to a file rather than replace it's contents then this is probably best suited to the lineinfile module and utilising the module's ability to insert a line at the end of the file.

The equivalent task to the copy one that you used would be something like:

- name: log foo_result to file
  lineinfile:
     line: "{{ foo_result }}"
     insertafter: EOF
     dest: /path/to/destination/file
  delegate_to: 127.0.0.1

Note that I've used the long hand for delegating tasks locally rather than local_action. I personally feel that the syntax reads a lot clearer but you could easily use the following instead if you prefer the more compact syntax of local_action:

- local_action: lineinfile line={{ foo_result }} insertafter=EOF dest=/path/to/destination/file
ydaetskcoR
  • 53,225
  • 8
  • 158
  • 177
  • Nice, this works well, thank you. I'm generally surprised there is not a more general way to write these types of reports in csv, json, etc, etc. – Jev Björsell Nov 27 '15 at 15:59