使用 --data-urlencode Ansible 转换 curl 请求

Ansible convert curl request with --data-urlencode

我想使用 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"
    },

如果 /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数据。

谢谢 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