Ansible URI模块使用POST,原始正文给出不可破坏的类型错误

Ansible URI模块使用POST,原始正文给出不可破坏的类型错误,ansible,Ansible,我正在尝试使用Ansible对URL进行POST调用并接收令牌。然后,此令牌将用于后续API调用。我能够使用Ansible URI模块进行一些GET API调用,但在进行POST调用时遇到了问题 工作的curl命令如下所示: curl -L -X POST 'https://myapiurl.com/api/1/auth/login' -H 'host: myapiurl.com' -H 'Content-Type: application/json' --data-raw '{ &qu

我正在尝试使用Ansible对URL进行POST调用并接收令牌。然后,此令牌将用于后续API调用。我能够使用Ansible URI模块进行一些GET API调用,但在进行POST调用时遇到了问题

工作的curl命令如下所示:

curl -L -X POST 'https://myapiurl.com/api/1/auth/login' -H 'host: myapiurl.com' -H 'Content-Type: application/json' --data-raw '{
    "username": "user",
    "password": "pass"
}'
- hosts: lab
  connection: local
  tasks:
- name: Get token
  uri:
    method: POST
    url:  "https://myapiurl.com/api/1/auth/login" 
    headers:
      Content-Type: application/json
      host: myapiurl.com
    body:
      username: user
      password: pass
    body_format: json
    validate_certs: no
  register: login
我的主要任务是:

- hosts: lab
  connection: local
  tasks:
- name: Get token
  uri:
    method: POST
    url:  "https://myapiurl.com/api/1/auth/login" 
    headers:
      Content-Type: application/json
      host: myapiurl.com
    body:
      name: user
      password: pass
    validate_certs: no
  register: login
详细错误输出:

TypeError: unhashable type
fatal: [host-name]: FAILED! => {
    "changed": false,
    "content": "",
    "elapsed": 0,
    "invocation": {
        "module_args": {
            "attributes": null,
            "backup": null,
            "body": {
                "name": "user",
                "password": "pass"
            },
            "body_format": "raw",
            "client_cert": null,
            "client_key": null,
            "content": null,
            "creates": null,
            "delimiter": null,
            "dest": null,
            "directory_mode": null,
            "follow": false,
            "follow_redirects": "safe",
            "force": false,
            "force_basic_auth": false,
            "group": null,
            "headers": {
                "Content-Type": "application/json",
                "host": "myapiurl.com"
            },
            "http_agent": "ansible-httpget",
            "method": "POST",
            "mode": null,
            "owner": null,
            "regexp": null,
            "remote_src": null,
            "removes": null,
            "return_content": false,
            "selevel": null,
            "serole": null,
            "setype": null,
            "seuser": null,
            "src": null,
            "status_code": [
                200
            ],
            "timeout": 30,
            "unix_socket": null,
            "unsafe_writes": null,
            "url": "https://myapiurl.com/api/1/auth/login",
            "url_password": null,
            "url_username": null,
            "use_proxy": true,
            "validate_certs": false
        }
    },
    "msg": "Status code was -1 and not [200]: An unknown error occurred: unhashable type",
    "redirected": false,
    "status": -1,
    "url": "https://myapiurl.com/api/1/auth/login"
}

不确定我遗漏了什么。

身体可能有问题吗?尝试将
用户名
置于
用户
的列表中:

body:
  username: user
  password: pass
    

我想我明白了。我需要在任务中添加“body_format:json”,如下所示:

curl -L -X POST 'https://myapiurl.com/api/1/auth/login' -H 'host: myapiurl.com' -H 'Content-Type: application/json' --data-raw '{
    "username": "user",
    "password": "pass"
}'
- hosts: lab
  connection: local
  tasks:
- name: Get token
  uri:
    method: POST
    url:  "https://myapiurl.com/api/1/auth/login" 
    headers:
      Content-Type: application/json
      host: myapiurl.com
    body:
      username: user
      password: pass
    body_format: json
    validate_certs: no
  register: login

是的,用户名是其中的一部分。我必须使用用户名,而不仅仅是名字。