【问题标题】:translate a curl PUT command into ansible uri将 curl PUT 命令转换为 ansible uri
【发布时间】:2018-10-01 13:13:21
【问题描述】:

我想通过its REST api 与 Seafile 服务器交互。

到目前为止,我将 POST 或 GET 查询转换为 ansible uri 模块没有任何问题。但是,我无法让 PUT 查询正常工作。

以下适用于 curl:

curl -X PUT -d "share_type=group&group_id=<groupid>&permission=rw" -H 'Authorization: Token <mysecrettoken>' -H 'Accept: application/json; charset=utf-8; indent=4' https://<myserverurl>/api2/repos/<mylibraryid>/dir/shared_items/?p=/

当我将其翻译为以下 ansible 任务时,它失败了:

- name: mytask
  uri:
    url: "https://<myserverurl>/api2/repos/<mylibraryid>/dir/shared_items/?p=/"
    method: PUT
    headers: '{ "Authorization": "Token <mysecrettoken>" }'
    body: '{ "share_type": "group", "group_id": "<groupid>", "permission": "rw"}'
    body_format: json
    return_content: yes

我得到错误:

HTTP Error 500: INTERNAL SERVER ERROR", "redirected": false, "server": "nginx", "set_cookie": "SERVERID=<serverid>; path=/", "status": 500, "transfer_encoding": "chunked", "url": "https://<myserverurl>/api2/repos/<mylibraryid>/dir/shared_items/?p=/", "vary": "Accept-Language, Cookie"}

在使用请求库的 python 脚本中,我必须将最终的?p=/ 提供为params={'p': '/'}。这是失败的原因吗?那么如何正确提交参数呢?

【问题讨论】:

    标签: ansible put seafile-server


    【解决方案1】:

    您应该将 headers 作为 YAML 哈希而不是 JSON 字符串传递:

    - name: mytask
      uri:
        url: "https://<myserverurl>/api2/repos/<mylibraryid>/dir/shared_items/?p=/"
        method: PUT
        headers: 
          Authorization: "Token <mysecrettoken>"
        body: '{ "share_type": "group", "group_id": "<groupid>", "permission": "rw"}'
        body_format: json
        return_content: yes
    

    参考见the docs,尤其是倒数第二个例子:

    - uri:
        url: https://your.form.based.auth.example.com/dashboard.php
        method: GET
        return_content: yes
        headers:
          Cookie: "{{ login.set_cookie }}"
    

    【讨论】:

      猜你喜欢
      • 2021-12-05
      • 2023-02-04
      • 1970-01-01
      • 1970-01-01
      • 2018-09-06
      • 1970-01-01
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多