How to use the Ansible uri module to POST a file as form-data?
Solution 1
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.
I did succeed however using a parameterised shell: curl
command:
Set Vars:
vars:
sonar_api_url: "https://yoursonarqubeserver.com/api"
sonar_token: "YourSonarQubeApiTokenThatRequiresAdminRights"
sonar_profile: "YourSonarQubeProfileToLoad.xml"
Task:
- 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 }}'
Note the API token is passed in as the username with a plank password via the curl -u.
I also have a sample GitHub repo with a working example you can refer to.
Solution 2
Update: The uri module does support the file upload now. Depending on if the file is located on the controller or on master or remote machine you will need to set remote_src
flag. The remote feature has been added in the 2.7 release.
Related videos on Youtube
dokaspar
Updated on May 25, 2022Comments
-
dokaspar almost 2 years
SonarQube allows the upload of a
profile.xml
file via form-dataPOST
request as follows:curl -u admin:admin -X POST http://sonar:9000/qualityprofiles/restore -F [email protected]
I'm trying to translate this
curl
command into Ansible, using theuri
module. Unfortunately, I don't see any chance to map the-F
form-data option and the parameterbackup
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') }}"
Or just like this:
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?
-
Konstantin Suvorov about 7 yearsAFAIK,
uri
module doesn't support file uploading -
Markus almost 7 yearsnot a direct solution to the file posting, but it seems that you like to script the setup of your quality profile. I ran into the same issue and solved it by using the Sonarqube api:
http://localhost:9000/api/qualityprofiles/activate_rule
-
Markus almost 7 yearsbut you have to convert your profile.xml to a yaml file e.g:
sonarqube_update_rules_checkstyle: - key: checkstyle:com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck severity: MAJOR params: checkFields=true;checkMethods=true
-
-
Phil about 3 yearsPlease show an example of one that works.