将变量记录到 ansible 主机上的日志文件中

Log variables to a logfile on ansible host

我有一个注册三个变量的剧本。我想在我的清单中的所有主机上生成这三个变量的 CSV 报告。

This 所以答案建议使用:

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

但这不会附加到 csv 文件。 另外,在这种情况下,我必须用逗号分隔符手动撰写。

关于如何将变量记录(附加)到本地文件的任何想法?

如果您想向文件追加一行而不是替换它的内容,那么这可能最适合 lineinfile 模块并利用该模块在文件末尾插入一行的能力.

与您使用的副本任务等效的任务如下:

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

请注意,我在本地使用 delegating tasks 的长手而不是 local_action。我个人觉得语法读起来更清晰,但如果您更喜欢 local_action:

更紧凑的语法,则可以轻松地使用以下内容
- local_action: lineinfile line={{ foo_result }} insertafter=EOF dest=/path/to/destination/file