【问题标题】:Multiline json string in ansible taskansible任务中的多行json字符串
【发布时间】:2019-01-25 20:10:02
【问题描述】:

在 ansible 任务中,我试图将 HTTP POST 请求中的长 JSON 字符串分解为多行,基于:

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

我试过了:

- 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

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

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

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

【问题讨论】:

    标签: json ansible httprequest


    【解决方案1】:

    你指定了body_format: json,所以你可以用yaml写你的body

    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: ""
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多