0

In an ansible task I am trying to break a long JSON string in a HTTP POST request into multiple lines based on:

In YAML, how do I break a string over multiple lines?

I have tried:

- name: "Test POST request"
  uri:
    url: "{{ HOST }}/api/"
    method: POST
    return_content: yes
    body: >-
     "{\"id\":\"{{ app_id }}\",
       \"name\":\"prefix-{{ name }}\",
       \"type\":\"ds\",
       \"typeLogoUrl\":\"\",
       \"access\":\"all\",
       \"url\":\"{{ HOST_URL }}",
       \"password\":\"\",
       \"user\":\"\",
       \"database\":\"\",
       \"jsonData\":{\"a\":\"{{ a_var }}\",\"b\":true,\"c\":\"{{ c_var }}\"},
       \"secureJsonFields\":{}}"
    body_format: json
    user: "{{ user }}"
    password: "{{ password }}"
    force_basic_auth: yes
    headers:
      Content-Type: application/json

But when I run it I get errors, summarized below:

[{\"classification\":\"DeserializationError\",\"message\":\"invalid character '\\\\n' in string literal\"}

Any suggestion on how to break this down into multiple without above error?

u123
  • 15,603
  • 58
  • 186
  • 303

1 Answers1

3

You specified body_format: json, so you can write your body in yaml

body: 
  id: "{{ org_id }}"
  name: "prefix-{{ namespace }}"
  type: datasource
  typeLogoUrl: ""
  access: proxy
  url: "{{ HOST_URL }}"
  password: ""
  user:""
  database: ""
  jsonData:
    a: "{{ a_var }}"
    b: true
    c: "{{ c_var }}"
  secureJsonFields: ""
sebthebert
  • 12,196
  • 2
  • 26
  • 37