首页 > 解决方案 > Ansible URI API (JSON-RPC)

问题描述

我对 ansible 的 URI 模块有疑问,也许有人知道问题可能是什么?

我尝试使用 ansible uri 模块与 I-Doit API (JSON-RPC) 进行通信。无论我尝试手动运行 curl 还是使用 ansible,我第一次尝试 curl 都可以正常工作。

# curl manual example
curl --location --request POST 'https://SERVER-FQDN/i-doit/src/jsonrpc.php' \
--header 'Content-Type: application/json' \
--header 'X-RPC-Auth-Session: SESSION-KEY' \
--header 'Cookie: PHPSESSID=SESSION-KEY' \
--data-raw '{
    "version": "2.0",
    "method": "idoit.version",
    "params": {
        "apikey": "API-KEY",
        "language": "en"
    },
    "id": 1
}'

# example in ansible
---
- name: CURL REST / JSON-RPC API Call
  shell: |
    curl -s --noproxy "*" --location --request POST 'https://SERVER-FQDN/i-doit/src/jsonrpc.php' \
    --header "Content-Type: application/json" \
    --header "X-RPC-Auth-Session: {{ session_key }}" \
    --header "Cookie: PHPSESSID= {{ session_key }}" \
    --data-raw '{
        "version": "2.0",
        "method": "idoit.version",
        "params": {
          "apikey": "{{ api_key }}",
          "language": "en"
        },
        "id": 1
      }'

如果我尝试使用 URI 模块重现调用,例如

- name: REST / JSON-RPC API Call check I-Doit Version
  uri:
    url: "https://SERVER-FQDN/i-doit/src/jsonrpc.php"
    method: post
    validate_certs: no
    follow_redirects: all
    use_proxy: no
    timeout: 200
    headers:
      X-RPC-Auth-Session: "{{ SESSION-KEY }}"
      Content-Type: "application/json"
    body_format: json
    return_content: yes
    body: "{{ lookup('file','test.json') }}"
  ignore_errors: yes

# test.json content
{
    "version": "2.0",
    "method": "idoit.version",
    "params": {
        "apikey": "API-KEY",
        "language": "en"
    },
    "id": 1
}

请求失败,错误代码 -1 和消息:“连接失败:在收到有效响应之前连接已关闭:未收到状态行 - 服务器已关闭连接”

URI 模块与 rest 或 json-rpc 的使用有区别吗?

感谢您的帮助或想法

丹特加布里埃尔

标签: ansible

解决方案


推荐阅读