Creating .JSON file with the data received from vars_prompt or survey in Ansible

Viewed 68

I have a requirement as below. I have to get the data from the user in the form of vars_prompt in ansible playbook or survey in AWX and I have to use the data to create .JSON file.

A sample requirement is shown below

I have to use the data obtained from the vars_prompt mentioned below

---
- name: Playbook for creating a .JSON file
  hosts: all
  vars_prompt: 
  - name: fruitName
    prompt: Please enter Fruit Name
    default: "Apple"
    private: no
  - name: fruitSize
    prompt: Please enter required fruit size
    default: "Large"
    private: no
  - name: fruitColour
    prompt: Please provide fruit colour
    default: "Red"
    private: no

to create a .JSON file as shown below.

{
    "fruitName": "Apple",
    "fruitSize": "Large",
    "fruitColour": "Red"
}

I am unable to find the correct way of doing it.

Can someone help me on how do it ? (Even a basic idea which helps in implementing it is highly appreciated)

1 Answers

For example, use copy

    - copy:
        dest: fruit.json
        content: |
          {
            "fruitName": "{{ fruitName }}',
            "fruitSize": "{{ fruitSize }}",
            "fruitColour": "{{ fruitColour }}"
          }

See also template.

Related