Ansible:复制模块不保留文本格式

时间:2018-02-22 13:03:55

标签: ansible ansible-2.x ansible-facts

任务:使用ansible-playbook从主机组中收集事实,并将它们放入本地计算机上的文件中。

我的解决方案:

---
- hosts: foo
  order: sorted
  gather_facts: no
  remote_user: foo
  become: yes
  become_method: sudo
  tasks:
  - name: gather foo hosts information
    setup:
    register: gathered_data
  - name: write gathered information into a file
    local_action: copy content="{{ gathered_data }}" dest=/foo/gathered_data.out
...

问题:一切正常但输出文件是一条巨大的线。是否可以保持文本格式,就像ansible -m setup foo输出一样?

还在为此任务寻找更好的解决方案。 Ansible版本:2.4.2.0

1 个答案:

答案 0 :(得分:1)

您可以使用其中一个filters for formatting data,例如:

content: "{{ gathered_data | to_nice_json }}”