Adding postgres datasource using API

Hi All,
I am trying add postgres datasource using ansible and task looks like below.

  • name: Add PostgreSQL data source
    uri:
    url: “{{ grafana_url }}/api/datasources”
    method: POST
    body_format: json
    body:
    name: ‘PostgreSQL’
    type: ‘postgres’
    url: ‘localhost:5432’
    access: ‘proxy’
    database: ‘your_database’
    user: ‘your_username’
    secureJsonData:
    password: ‘your_password’
    isDefault: true
    headers:
    Authorization: “Bearer {{ token }}”
    Content-Type: “application/json”
    return_content: yes
    status_code: 200

Eventhough the datasource getting added but type shows undefined, when i open settings, i can see only Delete option. Can someone help me to identify what could be the issue and i am using grafana 10.4 OSS.

I think type should be PostgreSQL?

image

I tried postgresql as well, still same error. Anything mistake I am doing in playbook above and by the way I am using poatgres 10.4.0 version.

I went with below approach and it worked and also i went with grafana version to 10.0.0. since community.grafana.grafana_datasource tested till 10.2.2 i think.

  • name: Create postgres datasource
    community.grafana.grafana_datasource:
    name: “PostgreSQL”
    grafana_url: “{{ grafana_url }}”
    grafana_user: “{{ grafana_admin_user }}”
    grafana_password: “{{ grafana_admin_password }}”
    #org_id: “1”
    ds_type: “postgres”
    ds_url: “{{ postgres_url }}”
    database: “{{ db_name }}”
    user: “{{ db_owner }}”
    sslmode: “disable”
    additional_json_data:
    postgresVersion: 15
    timescaledb: false
    additional_secure_json_data:
    password: “{{ db_password }}”
1 Like