【问题标题】:Ansible convert curl request with --data-urlencodeAnsible 使用 --data-urlencode 转换 curl 请求
【发布时间】:2018-08-31 09:47:19
【问题描述】:

我想通过 Ansible 的 REST API 将 HTML 文件上传到 GitLab。

我的 curl 请求运行良好:

 curl -H "Content-Type: application/x-www-form-urlencoded" --request POST  --header 'PRIVATE-TOKEN: my_tocken' --data-urlencode content@/tmp/report.html 'https://my_server/api/v4/projects/3/repository/files/my_customer%2Freportname%2Ehtml?branch=master&commit_message=create%20a%20new%20report' -k

如何用uri模块翻译?

uri:
  url: "https://my_server/api/v4/projects/3/repository/files/my_customer%2Freportname%2Ehtml?branch=master&commit_message=create%20a%20new%20report"
  validate_certs: no
  method: POST
  headers:
     Content-Type: application/x-www-form-urlencoded
     PRIVATE-TOKEN: "my_tocken"
  status_code: 200
  body: "data-urlencode=content@/tmp/report.html"

我明白了:

 "json": {
    "error": "content is missing"
    },

【问题讨论】:

    标签: curl ansible gitlab uri urlencode


    【解决方案1】:

    如果 /tmp/report.html 在 Ansible 控制器机器上,则:

    uri:
      url: "https://my_server/api/v4/projects/3/repository/files/my_customer%2Freportname%2Ehtml?branch=master&commit_message=create%20a%20new%20report"
      validate_certs: no
      method: POST
      headers:
         Content-Type: application/x-www-form-urlencoded
         PRIVATE-TOKEN: "my_tocken"
      status_code: 200
      body: content={{ lookup('file', '/tmp/report.html') | urlencode }}
    

    如果它在不同的目标上,你需要先slurp数据。

    【讨论】:

      【解决方案2】:

      感谢 Techraf,您是对的。

      正确的请求是:

       - name:  Gitlab | upload file
             uri:
               url: "https://my_server/api/v4/projects/3/repository/files/my_customer%2Freportname%2Ehtml?branch=master&commit_message=create%20a%20new%20report%20for%20server"
              validate_certs: no
               method: POST
               headers:
                  Content-Type: application/x-www-form-urlencoded
                  PRIVATE-TOKEN: "my_tocken"
            status_code: 201
            body: "content={{ lookup('file', '/tmp/report.html')|urlencode }}"
          delegate_to: localhost
      

      【讨论】:

      • 不需要单独发布答案,我可以编辑我的。更好,请解决您的问题,因为您写的 --data-urlencode content@/tmp/report.html 工作正常。
      猜你喜欢
      • 1970-01-01
      • 2019-06-21
      • 2017-07-19
      • 1970-01-01
      • 1970-01-01
      • 2018-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多