How to use the Ansible uri module to POST a file as form-data?

dok*_*par 2 post multipartform-data ansible sonarqube

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

I've also tried something like this:

    body: "backup={{ lookup('file', 'profile.xml') }}"
Run Code Online (Sandbox Code Playgroud)

Or just like this:

    body: "backup=profile.xml"
Run Code Online (Sandbox Code Playgroud)

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

j3r*_*3m7 8

我还尝试了许多使用 Ansibleuri命令的选项,但没有运气,尽管我可以通过 API 来uri设置身份验证等。

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

设置变量:

vars:
  sonar_api_url: "https://yoursonarqubeserver.com/api"
  sonar_token: "YourSonarQubeApiTokenThatRequiresAdminRights"
  sonar_profile: "YourSonarQubeProfileToLoad.xml"
Run Code Online (Sandbox Code Playgroud)

任务:

- 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 }}'
Run Code Online (Sandbox Code Playgroud)

请注意,API 令牌作为用户名通过 curl -u 传入,并带有一个板条密码。

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