Jev Björsell
Jev Björsell

Reputation: 881

Log variables to a logfile on ansible host

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?

Upvotes: 5

Views: 8483

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56859

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

Upvotes: 6

Related Questions