【问题标题】:Ansible: copy module doesn't keep text formattingAnsible:复制模块不保留文本格式
【发布时间】:2018-02-22 13:03:55
【问题描述】:

任务:使用 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

【问题讨论】:

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


    【解决方案1】:

    您可以使用filters for formatting data 之一,例如:

    content: "{{ gathered_data | to_nice_json }}”
    

    【讨论】:

    • 非常感谢!不幸的是,“内容:”在我的情况下不起作用,所以我必须使用“内容 =”,它工作得很好。
    • 因为您使用 Ansible 表示法,而我包含了纯 YAML。这两种符号是等价的。
    猜你喜欢
    • 1970-01-01
    • 2015-10-08
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多