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

问题描述 投票:0回答:2

我想通过其带有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"
    },
curl ansible gitlab uri urlencode
2个回答
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


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
© www.soinside.com 2019 - 2024. All rights reserved.