'project' parameter is missing error when trying to create sonar project using JSON payload in Ansible

SonarQube version : * Enterprise Edition * Version 7.9.1 (build 27448)

What I am trying to achieve : I am trying to create sonar project using Ansible.

task definition :


  • name: Create a sonar project
    uri:
    url: “http://sonardashboard.orgname.com/api/projects/create
    method: POST
    user: “{{ git_username }}”
    password: “{{ git_password }}”
    validate_certs: no
    return_content: yes
    force_basic_auth: yes
    body: “{{ lookup(‘template’,‘Project.j2’) | to_json }}”
    body_format: json
    status_code: 201
    retries: 3
    delay: 1
    with_items:
    • “{{ app_name }}”

Project.j2 :

{
“name”: “{{ item.Project }}-{{ item.name }}”,
“project”: “Testing:{{ item.name }}”,
“visibility”: “private”
}

Error :

failed: [localhost] (item={u’name’: u’hello-world’, u’Project’: u’test-project’}) => {“changed”: false, “connection”: “close”, “content”: “{“errors”:[{“msg”:“The ‘project’ parameter is missing”}]}”, “content_length”: “57”, “content_type”: “application/json”, “date”: “Sat, 25 Apr 2020 12:19:00 GMT”, “item”: {“Project”: “test-project”, “name”: “hello-world”}, “json”: {“errors”: [{“msg”: “The ‘project’ parameter is missing”}]}, “msg”: "Status code was 400 and not [201]: HTTP Error 400: ", “redirected”: false, “status”: 400, “url”: “http://sonardashboard.orgname.com/api/projects/create”}
to retry, use: --limit @/tmp/workspace/Configuration-Management/Ansible_Run/sonar.retry

Hi @Aaqib_Yaragatti,

It looks like your Ansible code isn’t constructing the API call correctly. This isn’t really the right place to get help on calling Web APIs from Ansible scripts, but let me outline what SonarQube is expecting in the API call; I’ll leave it with you to back-translate that into your Ansible script. If I was writing this using curl, I’d write something like:

curl -X POST -u username:password "http://<yourSonarQubeURL>/api/projects/create?name=Project-name&project=Testing:name&visibility=private"

I hope this helps.

Regards,

Cameron.

Hi Cameron,

Curl works fine for me , but if I try to create with JSON payload I get error.

Hi Aaqib,

Can I ask why you’re constructing a JSON payload? This is a simple POST API call, it doesn’t take any data.

Cheers,

Cameron.

Hi Cameron,
We also trying to create bulk number of projects through ansible with curl .kindly suggest the methodologies to deploy this strategy .

Regards,
kaleel

hi @Aaqib_Yaragatti,
have you fixed this issue ?

Regards,
@kaleelcsa

Hi Kaleel,

I am achieving this by using a simple curl and ansible command module.

Below is ansible task :

Hope it helps !!!.