alig227
alig227

Reputation: 357

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

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"

Upvotes: 4

Views: 15334

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68229

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"

Upvotes: 8

Related Questions