Ansible uri 模块授权无法按预期工作

时间:2021-04-07 09:49:08

标签: automation ansible devops

我正在尝试通过 ansible uri 模块创建 kibana 空间 这是我的任务

- name: Create altynorda kibana space
  uri:
    url: '{{ kibana_altynorda_url }}/api/spaces/space'
    method: POST
    body:
      {"id":"altynorda","name":"altynorda","description":"plati za parkovku","color":"#aabbcc","initials":"MK","disabledFeatures":["timelion","canvas"]}
    body_format: json
    headers:
      kbn-xsrf: 'true'
      Content-Type: 'application/json'
    url_username: elastic
    url_password: poshelnaxyi
  when:
    - first_host|default(false)|bool
    - '"altynorda" not in altynorda_spaces_result'
  delegate_to: localhost

因此,当我通过 ansible uri 模块启动此任务时,Kibana 提示错误

FAILED! => {"cache_control": "private, no-cache, no-store, must-revalidate", "changed": false, "connection": "close", "content": "{\"statusCode\":401,\"error\":\"Unauthorized\",\"message\":\"Unauthorized\"}", "content_length": "66", "content_type": "application/json; charset=utf-8", "date": "Wed, 07 Apr 2021 09:12:24 GMT", "elapsed": 0, "json": {"error": "Unauthorized", "message": "Unauthorized", "statusCode": 401}

但是当我通过普通的 cURL 执行此操作时,它接受了

curl -u elastic:poshelnaxyi -X POST "https://kibana.kz/api/spaces/space" -H 'kbn-xsrf: true' -H 'Content-Type: application/json' -d'

{

  "id": "altynorda",

  "name": "altynorda",

  "description" : "plati za parkovku",

  "color": "#aabbcc",

  "initials": "MK",

  "disabledFeatures": ["timelion", "canvas"]

}

'

{"id":"altynorda","name":"altynorda","description":"plati za parkovku","color":"#aabbcc","initials":"MK","disabledFeatures":["timelion","canvas"]}

1 个答案:

答案 0 :(得分:1)

您可以尝试添加选项吗

force_basic_auth: yes

由于文档中的原因,我通常必须添加此选项 https://docs.ansible.com/ansible/latest/collections/ansible/builtin/uri_module.html#parameter-force_basic_auth