如何使用 Ansible uri 模块将文件作为表单数据发布? [英] How to use the Ansible uri module to POST a file as form-data?

查看:33
本文介绍了如何使用 Ansible uri 模块将文件作为表单数据发布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SonarQube 允许通过表单数据 POST 请求上传 profile.xml 文件,如下所示:

SonarQube allows the upload of a profile.xml file via form-data POST request as follows:

curl -u admin:admin -X POST http://sonar:9000/qualityprofiles/restore -F backup=@profile.xml

我正在尝试使用 uri 模块将此 curl 命令转换为 Ansible.不幸的是,我没有看到任何机会将 -F 表单数据选项和参数 backup 映射到 Ansible.这是我的尝试:

I'm trying to translate this curl command into Ansible, using the uri module. Unfortunately, I don't see any chance to map the -F form-data option and the parameter backup to Ansible. Here's my attempt:

- name: create quality profile
  uri:
    url: "{{ sonar_api_url }}/qualityprofiles/restore"
    method: POST
    body: "{{ lookup('file', 'profile.xml') }}"
    user: "{{ sonar_admin_user }}"
    password: "{{ sonar_admin_pass }}"
    force_basic_auth: "yes"
    status_code: 200

我也试过这样的:

    body: "backup={{ lookup('file', 'profile.xml') }}"

或者就这样:

    body: "backup=profile.xml"

但都没有成功.我不断收到错误必须提供备份文件".任何想法如何实现?

But all without success. I keep getting the error "A backup file must be provided". Any ideas how this can be achieved?

推荐答案

我也尝试了许多使用 Ansible uri 命令的选项,尽管能够通过 uri<使用 API,但都没有成功/code> 用于设置身份验证等.

I also tried many options with the Ansible uri command with no luck despite being able to use the API via uri for things like setting Authentication etc.

但是我使用参数化的 shell: curl 命令确实成功了:

I did succeed however using a parameterised shell: curl command:

设置变量:

vars:
  sonar_api_url: "https://yoursonarqubeserver.com/api"
  sonar_token: "YourSonarQubeApiTokenThatRequiresAdminRights"
  sonar_profile: "YourSonarQubeProfileToLoad.xml"

任务:

- name: POST a SonarQube Profile xml via curl
  shell: 'curl  -X "POST" "{{ sonar_api_url }}/qualityprofiles/restore" \
                -H "Content-Type: multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__" \
                -u {{ sonar_token }}: \
                -k \
                --form backup=@{{ sonar_profile }}'

注意 API 令牌是通过 curl -u 作为用户名和板条密码传入的.

Note the API token is passed in as the username with a plank password via the curl -u.

我还有一个 示例 GitHub 存储库,其中包含一个您可以参考的工作示例.

I also have a sample GitHub repo with a working example you can refer to.

这篇关于如何使用 Ansible uri 模块将文件作为表单数据发布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆