在 ansible 中美化 JSON 文件

Prettify JSON file in ansible

假设我有一个来自模板的 JSON 文件

- name: consul config file
  template: >
    src={{ consul_config_template }}
    dest={{ consul_config_file }}
    owner={{ consul_user }}
    group={{ consul_group }}
    mode=0755

如何美化 JSON 文件以删除多余的空格和换行符?在 template?

之后有没有我可以调用的 ansible 模块

你可以一次完成:

- copy:
    content: "{{ item | to_nice_json }}"
    dest: "{{ consul_config_file }}"
    owner: "{{ consul_user }}"
    group: "{{ consul_group }}"
    mode: 0755
  with_template: "{{ consul_config_template }}"