首页 > 解决方案 > 使用 Ansible uri 模块的 API 调用

问题描述

我正在尝试使用Ansible uri模块与DeployHQ 的 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
    ....

都在同一级别。非常感谢您的评论让我回到密码字段。

标签: curlansiblecredentials

解决方案


根据ansible uri 文档,还有一个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

推荐阅读