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

时间:2018-08-31 09:47:19

标签: curl ansible gitlab uri urlencode

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

我的卷曲请求工作正常:

 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"
    },

2 个答案:

答案 0 :(得分: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数据。

答案 1 :(得分:0)

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