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

15,305

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.

Share:
15,305

Related videos on Youtube

dokaspar
Author by

dokaspar

Updated on May 25, 2022

Comments

  • dokaspar
    dokaspar almost 2 years

    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 [email protected]
    

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

    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
      Konstantin Suvorov about 7 years
      AFAIK, uri module doesn't support file uploading
    • Markus
      Markus almost 7 years
      not 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
      Markus almost 7 years
      but 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.Req‌​uireThisCheck severity: MAJOR params: checkFields=true;checkMethods=true
  • Phil
    Phil about 3 years
    Please show an example of one that works.