【问题标题】:API call using Ansible uri module使用 Ansible uri 模块的 API 调用
【发布时间】:2018-07-06 16:53:33
【问题描述】:

我正在尝试使用Ansible uri 模块与DeployHQ's API 通信。

这是我尝试从 Ansible 使用的 DeployHQ 文档示例:

curl -H "Content-type: application/json" \
-H "Accept: application/json" \
--user adam@atechmedia.com:my-api-key \
-d '{"deployment":{ "parent_identifier":"7563d091-ca73-588e-cfe2- e4936f190145", \
  "start_revision" : "e84b5937f1132932dd56026db26a76f406555c19", \
  "end_revision" : "e84b5937f1132932dd56026db26a76f406555c19", \
  "mode" : "queue", \
  "copy_config_files" : 1, \
  "email_notify" : 1 \
}}' http://test.deployhq.com/projects/project/deployments/

这就是我通过 Ansible 发送它的方式:

- uri:
    url: https://cepr.deployhq.com/projects/cepr-live/servers
    user: me@myemail.org:secret_api_key
    body_format: json
    method: GET
    headers:
      Content-Type: application/json
      Accept: application/json
    deployment:
      parent_identifier: id
      start_revision: my_start_rev
      end_revision: my_end_rev
      mode: queue
      copy_config_files: 1
      email_notify: 1
    return_content: yes

问题是我收到了 403 响应(拒绝访问),所以这与向他们传递 --user 参数有关。我从 cli 发送 cURL 请求完全没有问题,因此传入的用户凭据 --user 是正确的。 DeployHQ 可以从他们的日志中看到 json 请求看起来是正确的,但它没有经过身份验证(显然出于安全原因他们无法查看标头)。

它一定很简单,但我花了整个下午尝试了许多用户组合:(包括和用户:和密码:用于 api 密钥)- 体内:和体外:(如上)。 DeployHQ 支持人员说他们使用基本的 http_auth,所以我尝试了这些与参数的组合:

force_basic_auth: yes

我也尝试在 url 中传递 --user 。一点运气都没有。

其他人做过吗?!

已解决...

- uri:
    url: https://cepr.deployhq.com/projects/cepr-live/servers
    user: me@myemail.org
    password: secret_api_key
    force_basic_auth: yes
    ....

都在同一级别。非常感谢 cmets 让我回到密码字段。

【问题讨论】:

  • 您是否尝试过使用userpassword 选项,而不是在user 中附加密钥? ansible uri plugin 的文档仅将 user 列为用户名,所以我认为您需要两者。
  • 搞定了——最后就是这么简单,一定是我没有尝试过的组合之一,你确实需要 user: 和 password: 元素,但你还需要'force_basic_auth: yes' 紧接着......

标签: curl ansible credentials


【解决方案1】:

根据ansible uri documentation,还有一个password字段。看起来user 应该只是用户名。您是否尝试过类似的方法:

- uri:
    url: https://cepr.deployhq.com/projects/cepr-live/servers
    user: me@myemail.org
    password: secret_api_key
    body_format: json
    method: GET
    headers:
      Content-Type: application/json
      Accept: application/json
    deployment:
      parent_identifier: id
      start_revision: my_start_rev
      end_revision: my_end_rev
      mode: queue
      copy_config_files: 1
      email_notify: 1
    return_content: yes

【讨论】:

  • 是的 - 我已经尝试了几乎所有 user: 和 user with password 的组合。它必须是关于基本 http auth 并传入 user@myemail.com 和 api 密钥
【解决方案2】:

设置force_basic_auth

- name: call an api
  vars:
    cust_id: "123"
  uri:
    url: "url"
    url_username: "username"
    url_password: "password"
    force_basic_auth: yes
    method: PUT
    headers:
      xyz: true
    body_format: json
    body: "{{ cust_id }}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 2022-11-18
    • 2021-06-27
    • 1970-01-01
    相关资源
    最近更新 更多