我有以下剧本:
---
# file: access_token/tasks
- name: Verify if tenantname is provided
fail: msg="Please provide the name of the tenant."
when: tenantname is undefined
- name: Verify if tenantsecret is provided
fail: msg="Please provide the application secret of the tenant."
when: tenantsecret is undefined
- name: Send OAuth details
shell: |
curl -X POST \
'{{mlp_uaa}}' \
-H 'content-type: application/x-www-form-urlencoded' \
-d 'client_id={{tenantname}}&client_secret={{tenantsecret}}'
register: token_details
- debug:
# var=token_details.stdout_lines
var: (token_details.stdout | from_json).access_token
# uri module, which fails
- name: Send OAuth details2
uri:
url: "{{mlp_uaa}}"
method: POST
body:
- client_id: "{{tenantname}}"
- client_secret: "{{tenantsecret}}"
status_code: 200
headers:
body_format: json
Content-Type: "application/x-www-form-urlencoded"
return_content: yes
register: token_details2
你能告诉我为什么使用shell我能够获得access_token
的值并使用URI我得到输出(请注意我使用Ubuntu 16.04
):{{3 }}
谢谢:)
答案 0 :(得分:1)
您定义了body_format: json
,因此您必须传递JSON字符串作为正文:
如果body_format设置为'json',它将采用已格式化的JSON字符串或将数据结构转换为JSON。
目前您正在传递一个双元素列表对象:
- client_id: "{{tenantname}}"
- client_secret: "{{tenantsecret}}"
具有相同列表的JSON字符串如下所示:
'{ [ "client_id": "{{tenantname}}", "client_secret": "{{tenantsecret}}" ] }'