使用Ansible URI模块配置Cloud Director后部署时遇400错误求助
解决Ansible调用VMware Cloud Director API时的HTTP 400 Bad Request错误
错误根源
从返回信息Failed to decode JSON object: Expecting value: line 1 column 99 (char 98)可以确定,问题出在请求的JSON格式无效——密码变量未被正确包裹为JSON字符串。
原代码中dbPassword:{{ passwd }}和password:{{ passwd }}的写法,会导致passwd变量值直接嵌入JSON,缺少字符串必需的双引号,违反JSON语法规范,服务器无法解析。
解决方案
方案1:修复手动拼接的JSON字符串
给密码变量添加双引号,确保生成的JSON中字符串值符合语法要求:
- name: configure cloud director post deployment uri: url: 'https://{{ vcd_ip }}:5480/api/1.0.0/systemSetup' method: POST body: '{"applianceType":"primary","storage":{"nfs":"nfs_ip:/share"},"appliance":{"dbPassword":"{{ passwd }}","ceip":"checked"},"installation":{"name":"VCD10.3","id":"1"},"sysAdmin":{"username":"administrator","password":"{{ passwd }}","fullName":"xxxx","email":"xxxxx@email.com"}}' headers: Accept: 'application/*;version=35.2' Content-Type: application/json Authorization: 'Basic xcfgrtyunvdryrtpiy=' validate_certs: false register: result
方案2:使用uri模块的json参数(推荐)
避免手动拼接JSON字符串,直接传入YAML字典,Ansible会自动转换为标准JSON格式,彻底规避语法错误:
- name: configure cloud director post deployment uri: url: 'https://{{ vcd_ip }}:5480/api/1.0.0/systemSetup' method: POST json: applianceType: "primary" storage: nfs: "nfs_ip:/share" appliance: dbPassword: "{{ passwd }}" ceip: "checked" installation: name: "VCD10.3" id: "1" sysAdmin: username: "administrator" password: "{{ passwd }}" fullName: "xxxx" email: "xxxxx@email.com" headers: Accept: 'application/*;version=35.2' Authorization: 'Basic xcfgrtyunvdryrtpiy=' validate_certs: false register: result
额外验证步骤
- 可以通过
debug模块输出生成的请求内容,提前验证JSON格式:- debug: msg: '{"applianceType":"primary","storage":{"nfs":"nfs_ip:/share"},"appliance":{"dbPassword":"{{ passwd }}","ceip":"checked"},"installation":{"name":"VCD10.3","id":"1"},"sysAdmin":{"username":"administrator","password":"{{ passwd }}","fullName":"xxxx","email":"xxxxx@email.com"}}' - 若使用Bearer授权,确保格式为
Bearer <token>,且令牌未过期;同时确认passwd变量值符合VCD密码复杂度要求。
内容的提问来源于stack exchange,提问作者Vichu
相关产品推荐
相关产品推荐

