问题描述
我在使用完全不理解任何 json 参数的 API 时遇到了正确的语法问题。 在正文中必须正好是这种形式 '["XXXX"]' 中的一个值,这样它才能工作。
- uri:
url: "{{ AssociatePolicies_url }}"
method: POST
return_content: yes
validate_certs: no
body_format: form-urlencoded
body:
apiKey: "{{ apiKey }}"
ASSIGNED_POLICIES: '["XXXX"]'
RESOURCEID: "{{ resourceId }}"
runcOMPCHECK_STATUS: true
headers:
Content-Type: "application/x-www-form-urlencoded;charset=UTF-8"
但是当我现在将值作为变量传递时,我没有以正确的形式得到它
---
- name: Post to API
hosts: KingKong
connection: local
gather_facts: no
vars:
policygroup:
- XXXX
# - YYYY
# - ZZZZ
tasks:
- uri:
url: "{{ AssociatePolicies_url }}"
method: POST
return_content: yes
validate_certs: no
body_format: form-urlencoded
body:
apiKey: "{{ apiKey }}"
ASSIGNED_POLICIES: "'["{{ item }}"]'"
RESOURCEID: "{{ resourceId }}"
runcOMPCHECK_STATUS: true
headers:
Content-Type: "application/x-www-form-urlencoded;charset=UTF-8"
with_items: "{{ policygroup }}"
是否响应:解析 ASSIGNED_POLICIES json 数据时出错
经过各种方法后,我没有更接近结果:
ASSIGNED_POLICIES: "\'[{{ item }}]\'" is the format '[XXXX]
ASSIGNED_POLICIES: \'["{{ item }}"]\' is the format \\'[\"XXXX\"]\\'
ASSIGNED_POLICIES: "\'[\"{{ item }}\"]\'" is the format '[\"XXXX\"]'
ASSIGNED_POLICIES: "\'["{{ item }}"]\'" playbook error
ASSIGNED_POLICIES: "\'[{{ \" }}{{ item }}{{\"}}]\'" is the format '[ }}{{ item }}{{]'"}]
我几乎接近格式的地方是:
ASSIGNED_POLICIES: "\'['{{ item }}']\'" is the format "'['XXXX']'"
但在XXXX的前面和后面有单引号但必须有双引号。
也许你们中的一个有创意
解决方法
我已经更正了上面的源代码,并在下面发布了我收到的回复。 我看到的回复:
"content": "{\"error\":{\"message\":\"Error while parsing ASSIGNED_POLICIES json data.\",\"errorcode\":\"JSON_PARSE_ERROR\"}}\n"
和错误的格式:
"ASSIGNED_POLICIES": "'[XXXX]'"
XXXX 前后缺少双引号。 这一定是正确的帖子
"ASSIGNED_POLICIES": "'["XXXX"]'"
.
{
"changed": false,"_ansible_verbose_always": true,"result_4": {
"msg": "All items completed","changed": false,"results": [
{
"content_length": "104","status": 200,"cookies": {
"opmcsrfcookie": "192d408c-be9b-42f7-985b-56360c06fdd7"
},"set_cookie": "opmcsrfcookie=192d408c-be9b-42f7-985b-56360c06fdd7;path=/;Secure;priority=high","vary": "Accept-Encoding","elapsed": 0,"msg": "OK (104 bytes)","content_type": "application/json;charset=UTF-8","date": "Wed,16 Mar 2021 16:22:55 GMT","x_frame_options": "deny","ansible_loop_var": "item","x_xss_protection": "1","url": "https://https://AssociatePolicies_url","x_content_type_options": "nosniff","connection": "close","content": "{\"error\":{\"message\":\"Error while parsing ASSIGNED_POLICIES json data.\",\"errorcode\":\"JSON_PARSE_ERROR\"}}\n","failed": false,"json": {
"error": {
"errorcode": "JSON_PARSE_ERROR","message": "Error while parsing ASSIGNED_POLICIES json data."
}
},"item": 301,"invocation": {
"module_args": {
"directory_mode": null,"force": false,"remote_src": null,"status_code": [
200
],"follow": false,"owner": null,"body_format": "form-urlencoded","client_key": null,"group": null,"use_proxy": true,"unix_socket": null,"unsafe_writes": null,"setype": null,"content": null,"serole": null,"follow_redirects": "safe","return_content": true,"client_cert": null,"body": {
"RUNCOMPCHECK_STATUS": true,"RESOURCEID": "abcde","apiKey": "just_a_Key","ASSIGNED_POLICIES": "'[XXXX]'"
},"url_username": null,"src": null,"dest": null,"selevel": null,"force_basic_auth": false,"removes": null,"http_agent": "ansible-httpget","regexp": null,"url_password": null,"url": "https://AssociatePolicies_url","validate_certs": false,"seuser": null,"method": "POST","creates": null,"headers": {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
},"delimiter": null,"mode": null,"timeout": 30,"attributes": null,"backup": null
}
},"redirected": false,"cookies_string": "opmcsrfcookie=192d408c-be9b-42f7-985b-56360c06fdd7"
}
]
},"_ansible_no_log": false
}