Ansible任务中的多行JSON字符串

时间:2019-01-25 20:10:02

标签: json ansible httprequest

在一项艰巨的任务中,我试图根据以下条件将HTTP POST请求中的长JSON字符串分成多行:

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

我尝试过:

def typingeffect(string):
    for i in string:
        print(i, end="", flush=True)
        sleep(0.2)

但是当我运行它时,我得到了错误,总结如下:

- 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

关于如何将其分解为多个而没有上述错误的任何建议?

1 个答案:

答案 0 :(得分:1)

您指定了body_format: json,因此您可以用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: ""