将curl命令转换为Ansible uri模块时遇OPERATION_NOT_SUPPORTED错误求助
问题排查:Ansible uri模块替代curl命令报错OPERATION_NOT_SUPPORTED
原始curl命令
curl -u "username:password" "https://company.abc.apps.company.eu/qps/rest/1.0/download/ca/downloadbinary/" --header "Content-Type: application/xml" --data-raw "<ServiceRequest><data><DownloadBinary><platform>LINUX_UBUNTU</platform><architecture>ARM_64</architecture></DownloadBinary></data></ServiceRequest>" -o file.deb
当前Ansible任务
- name: Download Binaries? ansible.builtin.uri: url: https://company.abc.apps.company.eu/qps/rest/1.0/download/ca/downloadbinary/ url_username: username url_password: password force_basic_auth: true headers: Content-Type: application/xml body_format: raw body: "<ServiceRequest><data><DownloadBinary><platform>LINUX_UBUNTU</platform><architecture>ARM_64</architecture></DownloadBinary></data></ServiceRequest>" dest: file.deb
错误响应
<?xml version="1.0" encoding="UTF-8"?> <ServiceResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://companty.abc.apps.company.eu/qps/xsd/1.0/ca/downloadbinary.xsd"> <responseCode>OPERATION_NOT_SUPPORTED</responseCode> <responseErrorDetails> <errorMessage>Operation not supported for this object using this method.</errorMessage> <errorResolution>Refer to the QPS guide for authorized operation for this domain object.</errorResolution> </responseErrorDetails> </ServiceResponse>
问题原因
核心差异在于HTTP请求方法:
- curl使用
--data-raw参数时,默认发送POST请求 - Ansible uri模块默认使用GET请求,即使指定
body也不会自动切换为POST,导致服务端收到不支持的请求方法,返回OPERATION_NOT_SUPPORTED错误
解决方案
在uri模块中显式添加method: POST参数,对齐curl的请求方法:
- name: Download Binaries ansible.builtin.uri: url: https://company.abc.apps.company.eu/qps/rest/1.0/download/ca/downloadbinary/ url_username: username url_password: password force_basic_auth: true method: POST # 显式指定POST方法 headers: Content-Type: application/xml body_format: raw body: "<ServiceRequest><data><DownloadBinary><platform>LINUX_UBUNTU</platform><architecture>ARM_64</architecture></DownloadBinary></data></ServiceRequest>" dest: file.deb
额外检查项
- 确认URL路径(含末尾斜杠)与curl完全一致,部分服务端对路径匹配敏感
- 验证
body中的XML内容无转义错误或格式差异 - 若问题仍存在,可添加
return_content: true和status_code: 200(或服务端预期的成功状态码),获取更详细的响应信息辅助排查
内容的提问来源于stack exchange,提问作者Pugs08
相关产品推荐
相关产品推荐

