You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible的uri模块请求返回401但shell执行curl正常,求正确uri写法

问题根因

你原有uri模块写法的核心错误是body_format参数配置不匹配:

  • curl命令发送的是application/x-www-form-urlencoded格式的表单请求体
  • 但你的uri配置里把body_format设为了json,会把请求体序列化为JSON格式,后端无法正常解析账号密码参数,因此返回401认证错误。

正确uri模块实现

- name: Get authentication token
  uri:
    url: https://myIPcontrol.com:8443/inc-rest/api/v1/login
    method: POST
    timeout: 30
    validate_certs: no
    headers:
        Content-Type: application/x-www-form-urlencoded
        Accept: application/json
    body:
      username: admin
      password: mypsw
    body_format: form-urlencoded
    return_content: yes
  register: authtoken

当body_format设置为form-urlencoded时,Ansible会自动把body字段的字典内容转换为key1=value1&key2=value2的表单格式,和你curl命令的发送效果完全一致。

内容的提问来源于stack exchange,提问作者sundrys1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 12:06:04