Ansible:Shell模块可获取access_token,URI模块报TypeError错误原因咨询
问题分析:Ansible URI模块抛出TypeError异常的原因
你遇到的这个TypeError: write() argument 1 must be string or buffer, not list错误,核心是URI模块的参数配置不符合application/x-www-form-urlencoded格式的要求,具体有两个关键错误点:
错误1:Body格式误用列表而非字典
你在Send OAuth details2任务里,把body写成了列表形式:
body: - client_id: "{{tenantname}}" - client_secret: "{{tenantsecret}}"
但对于application/x-www-form-urlencoded类型的请求,请求体需要是键值对组成的字典,列表形式会让Ansible无法正确序列化请求内容,从而抛出"参数不是字符串或缓冲区"的类型错误。
错误2:body_format参数位置错误且取值不符
你把body_format: json放在了headers字典里,但它是URI模块的顶层参数,不属于请求头的一部分。同时,因为你指定的Content-Type是application/x-www-form-urlencoded,body_format应该设为form-urlencoded而非json——设置成json会让Ansible把请求体序列化成JSON格式,和你声明的Content-Type完全不匹配。
修正后的URI模块任务
把任务改成下面这样就能正常工作了:
- name: Send OAuth details2 uri: url: "{{mlp_uaa}}" method: POST body: client_id: "{{tenantname}}" client_secret: "{{tenantsecret}}" body_format: form-urlencoded status_code: 200 headers: Content-Type: "application/x-www-form-urlencoded" return_content: yes register: token_details2
修改后,Ansible会正确把字典形式的body序列化成client_id=xxx&client_secret=xxx的格式,和你用curl发送的请求完全一致,自然就能正常获取access_token了。
内容的提问来源于stack exchange,提问作者Foudel
相关产品推荐
相关产品推荐

