将’curl’命令转换为’ansible.builtin.uri’模块?

huangapple go评论74阅读模式
英文:

Converting 'curl' command to 'ansible.builtin.uri' module?

问题

我有一个文件附件,我正试图通过REST API上传到Confluence。我可以通过使用curl复制他们的示例来做到这一点,但是在使用Ansible时失败了,我无法弄清楚原因。

这是curl命令:

curl -u <username>:<password> -X POST -H "X-Atlassian-Token: nocheck" -F "file=@<filepath>" -F "comment=File attached via REST API" https://myconfluenceserver.com/rest/api/content/<page ID>/child/attachment

当然,< > 中的值已被替换为真实值。此命令成功执行。

这是Ansible任务:

ansible.builtin.uri:
  user: "{{ osa_credentials.username }}"
  password: "{{ osa_credentials.password }}"
  url: "{{ url }}"
  method: POST
  headers:
    X-Atlassian-Token: nocheck
  force_basic_auth: yes
  src: "{{ filepath }}"
  validate_certs: no

当我运行curl命令时,它正常工作,但使用Ansible时会抛出错误 400。以下是完整的错误文本:

fatal: [localhost]: FAILED! => {
    "changed": false,
    "connection": "close",
    "content_language": "en",
    "content_length": "435",
    "content_type": "text/html;charset=utf-8",
    "date": "Thu, 29 Jun 2023 06:34:10 GMT",
    "elapsed": 1,
    "invocation": {
        "module_args": {
            "attributes": null,
            "body": null,
            "body_format": "raw",
            "ca_path": null,
            "ciphers": null,
            "client_cert": null,
            "client_key": null,
            "creates": null,
            "decompress": true,
            "dest": null,
            "follow_redirects": "safe",
            "force": false,
            "force_basic_auth": true,
            "group": null,
            "headers": {
                "Content-Length": 543818,
                "X-Atlassian-Token": "nocheck"
            },
            "http_agent": "ansible-httpget",
            "method": "POST",
            "mode": null,
            "owner": null,
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "remote_src": false,
            "removes": null,
            "return_content": false,
            "selevel": null,
            "serole": null,
            "setype": null,
            "seuser": null,
            "src": "<filepath>",
            "status_code": [
                200
            ],
            "timeout": 30,
            "unix_socket": null,
            "unredirected_headers": [],
            "unsafe_writes": true,
            "url": "https://myconfluenceserver.com/rest/api/content/<page ID>/child/attachment",
            "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "url_username": "<username>",
            "use_gssapi": false,
            "use_netrc": true,
            "use_proxy": true,
            "user": "<username>",
            "validate_certs": false
        }
    },
    "msg": "Status code was 400 and not [200]: HTTP Error 400: ",
    "redirected": false,
    "server": "Apache",
    "status": 400,
    "url": "https://myconfluenceserver.com/rest/api/content/<page ID>/child/attachment"
}

我漏掉了什么?

英文:

I have a file attachment I'm trying to upload to Confluence via the REST API. I can do it by replicating their example with curl, but it's failing when using Ansible and I can't figure out why.

Here's the curl command:

curl -u &lt;username&gt;:&lt;password&gt; -X POST -H &quot;X-Atlassian-Token: nocheck&quot; -F &quot;file=@&lt;filepath&gt;&quot; -F &quot;comment=File attached via REST API&quot; https://myconfluenceserver.com/rest/api/content/&lt;page ID&gt;/child/attachment

Of course the values in &lt;&gt; are replaced with real values. This command is successful.

Here's the Ansible task:

ansible.builtin.uri:
  user: &quot;{{ osa_credentials.username }}&quot;
  password: &quot;{{ osa_credentials.password }}&quot;
  url: &quot;{{ url }}&quot;
  method: POST
  headers:
    X-Atlassian-Token: nocheck
  force_basic_auth: yes
  src: &quot;{{ filepath }}&quot;
  validate_certs: no

When I run the curl command it works, but with Ansible it throws error 400. Here is the full error text:

fatal: [localhost]: FAILED! =&gt; {
    &quot;changed&quot;: false,
    &quot;connection&quot;: &quot;close&quot;,
    &quot;content_language&quot;: &quot;en&quot;,
    &quot;content_length&quot;: &quot;435&quot;,
    &quot;content_type&quot;: &quot;text/html;charset=utf-8&quot;,
    &quot;date&quot;: &quot;Thu, 29 Jun 2023 06:34:10 GMT&quot;,
    &quot;elapsed&quot;: 1,
    &quot;invocation&quot;: {
        &quot;module_args&quot;: {
            &quot;attributes&quot;: null,
            &quot;body&quot;: null,
            &quot;body_format&quot;: &quot;raw&quot;,
            &quot;ca_path&quot;: null,
            &quot;ciphers&quot;: null,
            &quot;client_cert&quot;: null,
            &quot;client_key&quot;: null,
            &quot;creates&quot;: null,
            &quot;decompress&quot;: true,
            &quot;dest&quot;: null,
            &quot;follow_redirects&quot;: &quot;safe&quot;,
            &quot;force&quot;: false,
            &quot;force_basic_auth&quot;: true,
            &quot;group&quot;: null,
            &quot;headers&quot;: {
                &quot;Content-Length&quot;: 543818,
                &quot;X-Atlassian-Token&quot;: &quot;nocheck&quot;
            },
            &quot;http_agent&quot;: &quot;ansible-httpget&quot;,
            &quot;method&quot;: &quot;POST&quot;,
            &quot;mode&quot;: null,
            &quot;owner&quot;: null,
            &quot;password&quot;: &quot;VALUE_SPECIFIED_IN_NO_LOG_PARAMETER&quot;,
            &quot;remote_src&quot;: false,
            &quot;removes&quot;: null,
            &quot;return_content&quot;: false,
            &quot;selevel&quot;: null,
            &quot;serole&quot;: null,
            &quot;setype&quot;: null,
            &quot;seuser&quot;: null,
            &quot;src&quot;: &quot;&lt;filepath&gt;&quot;,
            &quot;status_code&quot;: [
                200
            ],
            &quot;timeout&quot;: 30,
            &quot;unix_socket&quot;: null,
            &quot;unredirected_headers&quot;: [],
            &quot;unsafe_writes&quot;: true,
            &quot;url&quot;: &quot;https://myconfluenceserver.com/rest/api/content/&lt;page ID&gt;/child/attachment&quot;,
            &quot;url_password&quot;: &quot;VALUE_SPECIFIED_IN_NO_LOG_PARAMETER&quot;,
            &quot;url_username&quot;: &quot;&lt;username&gt;&quot;,
            &quot;use_gssapi&quot;: false,
            &quot;use_netrc&quot;: true,
            &quot;use_proxy&quot;: true,
            &quot;user&quot;: &quot;&lt;username&gt;&quot;,
            &quot;validate_certs&quot;: false
        }
    },
    &quot;msg&quot;: &quot;Status code was 400 and not [200]: HTTP Error 400: &quot;,
    &quot;redirected&quot;: false,
    &quot;server&quot;: &quot;Apache&quot;,
    &quot;status&quot;: 400,
    &quot;url&quot;: &quot;https://myconfluenceserver.com/rest/api/content/&lt;page ID&gt;/child/attachment&quot;
}

What am I missing?

答案1

得分: 2

以下是您要翻译的内容:

Confluence REST API attachment - What content type to use? How to resolve HTTP Response Code 415?

根据Ansible uri 模块与 multipart/mixed以及以下的最小示例

---
- hosts: localhost
become: false
gather_facts: false
vars:
CONFLUENCE_USER: &quot;user&quot;
CONFLUENCE_PASSWORD: &quot;test&quot;
PAGE_ID: &quot;123456789&quot;
tasks:
- name: Test REST API - Confluence Attach File
ansible.builtin.uri:
user: &quot;{{ CONFLUENCE_USER }}&quot;
password: &quot;{{ CONFLUENCE_PASSWORD }}&quot;
url: &quot;https://confluence.example.com/rest/api/content/{{ PAGE_ID }}/child/attachment&quot;
validate_certs: yes
method: POST
headers:
X-Atlassian-Token: nocheck
force_basic_auth: yes
body_format: form-multipart
body:
file:
content: &quot;{{ lookup(&#39;file&#39;,&#39;test.file&#39;) }}&quot;
filename: test.file
register: result
- debug:
var: result.json.results

对Confluence的测试运行将成功地上传文件并输出如下:

TASK [debug] *********
ok: [localhost] =&gt;
result.json.results:
- _expandable:
ancestors: &#39;&#39;
body: &#39;&#39;
children: /rest/api/content/987654321/child
descendants: /rest/api/content/987654321/descendant
history: /rest/api/content/987654321/history
operations: &#39;&#39;
restrictions: /rest/api/content/987654321/restriction/byOperation
space: /rest/api/space/~user
_links:
download: /download/attachments/123456789/test.file?version=1&amp;modificationDate=1688109306468&amp;api=v2
self: https://confluence.example.com/rest/api/content/987654321
webui: /display/~user/StackOverflow?preview=%2F123456789%2F987654321%2Ftest.file
...
status: current
title: test.file
type: attachment
...

那么应该使用什么正确的body格式,或者正确的完成这个任务的方法是什么?

使用以下内容:

      body_format: form-multipart
body:
file:
content: &quot;{{ lookup(&#39;file&#39;,&#39;test.file&#39;) }}&quot;
filename: test.file

man curl中所述:

-F, --form <name=content>

(HTTP SMTP IMAP) 对于HTTP协议系列,这让curl模拟用户按下提交按钮后填写的表单。这使curl按照RFC 2388的定义,使用Content-Type multipart/form-dataPOST数据。

文档

英文:

> Confluence REST API attachment - What content type to use? How to resolve HTTP Response Code 415?

According Ansible uri module with multipart/mixed? and the following minimal example

---
- hosts: localhost
become: false
gather_facts: false
vars:
CONFLUENCE_USER: &quot;user&quot;
CONFLUENCE_PASSWORD: &quot;test&quot;
PAGE_ID: &quot;123456789&quot;
tasks:
- name: Test REST API - Confluence Attach File
ansible.builtin.uri:
user: &quot;{{ CONFLUENCE_USER }}&quot;
password: &quot;{{ CONFLUENCE_PASSWORD }}&quot;
url: &quot;https://confluence.example.com/rest/api/content/{{ PAGE_ID }}/child/attachment&quot;
validate_certs: yes
method: POST
headers:
X-Atlassian-Token: nocheck
force_basic_auth: yes
body_format: form-multipart
body:
file:
content: &quot;{{ lookup(&#39;file&#39;,&#39;test.file&#39;) }}&quot;
filename: test.file
register: result
- debug:
var: result.json.results

a test run against Confluence will successful result into a file upload and output of

TASK [debug] *********
ok: [localhost] =&gt;
result.json.results:
- _expandable:
ancestors: &#39;&#39;
body: &#39;&#39;
children: /rest/api/content/987654321/child
descendants: /rest/api/content/987654321/descendant
history: /rest/api/content/987654321/history
operations: &#39;&#39;
restrictions: /rest/api/content/987654321/restriction/byOperation
space: /rest/api/space/~user
_links:
download: /download/attachments/123456789/test.file?version=1&amp;modificationDate=1688109306468&amp;api=v2
self: https://confluence.example.com/rest/api/content/987654321
webui: /display/~user/StackOverflow?preview=%2F123456789%2F987654321%2Ftest.file
...
status: current
title: test.file
type: attachment
...

> So what's the right body format to use, or what's the right way to accomplish this?

Using

      body_format: form-multipart
body:
file:
content: &quot;{{ lookup(&#39;file&#39;,&#39;test.file&#39;) }}&quot;
filename: test.file

and as stated in man curl

> -F, --form <name=content>
>
> (HTTP SMTP IMAP) For HTTP protocol family, this lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388

Documentation

huangapple
  • 本文由 发表于 2023年6月29日 14:38:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定