首页 > 解决方案 > 使用 Ansible URI 模块的问题 - 即使添加了身份验证凭据,也会出现 401

问题描述

我正在使用 Ansible 的 URI 模块来调用一个简单的 GET 端点,但我收到如下所示的 401 错误消息。

在此处输入图像描述

这是剧本配置

  - name: Elasticsearch Cluster health Check
    uri:
      url: https://elastichost:9200/_cluster/health?&pretty
      url_password : xxxxxxx
      url_username : xxxxxx
      validate_certs : no
      method: GET
      body_format: json
      force_basic_auth: yes
      return_content: yes
      headers:
         Content-Type: "application/json" 
    register: showhealth
    tags: NonProd

  - name: Show Elasticsearch Health
    debug: var=showhealth  
    tags: NonProd

有关如何解决的任何输入

标签: ansibleansible-2.x

解决方案


弹性的具体问题。elastic 是安全的,您需要令牌或证书才能连接到 api。

而且,您的用户需要正确的权利。

例子

- name: Create update_user
  uri:
    url: 'uri'
    method: POST
    user: elasticuser
    body_format: json
    headers:
      Content-Type: application/json
    body: '{{ item.body }}'
    client_cert: 'certificate.crt'
    client_key: 'certificate.key'
    password: '{{ elastic_password }}'
    validate_certs: false
    return_content: true

推荐阅读