How to write a pretty JSON file from a template in ansible?

12,011

Try template lookup:

- name: Deploy configuration file
  win_copy:
    content: "{{ lookup('template', 'templates/config.j2') | to_nice_json }}"
    dest: "C:\\SomeDir\\{{web_app.name}}_config.json"
Share:
12,011
alig227
Author by

alig227

Updated on August 01, 2022

Comments

  • alig227
    alig227 almost 2 years

    I am trying to write a config file using jinja2 templating and save the file as .json and pretty format it.

    How can I save the output file as a variable and then format it to JSON using to_nice_json? This play is part of a role that is called from another main playbook. Currently it writes the config file to a Windows host but it's not formatted as JSON.

    ---
    #Write config file
    - name: Deploy configuration file
      template: src=templates/config.j2 dest="C:\\SomeDir\\
    {{web_app.name}}_config.json"