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

jev*_*rth 4 ansible ansible-playbook

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

这个SO 答案建议使用:

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

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

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

yda*_*coR 5

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

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

- name: log foo_result to file
  lineinfile:
     line: "{{ foo_result }}"
     insertafter: EOF
     dest: /path/to/destination/file
  delegate_to: 127.0.0.1
Run Code Online (Sandbox Code Playgroud)

请注意,我使用长手在本地委派任务而不是local_action. 我个人认为语法更清晰,但如果您更喜欢更紧凑的语法,您可以轻松地使用以下内容local_action

- local_action: lineinfile line={{ foo_result }} insertafter=EOF dest=/path/to/destination/file
Run Code Online (Sandbox Code Playgroud)