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

查看:651
本文介绍了如何使用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

我也尝试过这样的事情:

I've also tried something like this:

    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​​来进行诸如设置身份验证之类的事情,但还是没有运气.

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 }}'

请注意,通过curl -u将API令牌作为用户名与木板密码一起传递.

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天全站免登陆