Search code examples
curlansiblebackupelk

Using of CURL model in ansible to run a API command


I am trying to set up a automatic backup for my ELK indices. To do it manually I am using the below curl command.

curl -u username:$password -X  PUT \
"http://IP address:9200/_snapshot/TEST_backup./backup_name" \
-H 'Content-Type: application/json' \
-d'{ "indices": "index name", "ignore_unavailable": true, "include_global_state": false }'

Could you please let me know how the ansible curl structure will pan out for the same as per my idea it should be like below but it is generating error.

- name: Run cURL commands
  hosts: localhost
  tasks:
    - name: First task
      uri:
        url: http://IP ADDRESS:9200/_snapshot/TEST_backup/backupname
        headers:
          Content-Type: "application/json"
          X-Application-Username: "username"
          X-Application-Password: "password"
        method: PUT
        body:
          indices: "index names"
          ignore_unavailable: "true"
          include_global_state: "false"

        body_format: json
        validate_certs: no

Solution

  • ...but it is generating error

    Without the exact error, it is hard to infer what could be the problem. Meanwhile, I don't really understand the way you are passing the credentials. Is there a specific reason why you don't use the relevant module parameters ? Did you try like this ?

    - name: Run cURL commands
      hosts: localhost
      tasks:
        - name: First task
          uri:
            url: http://IP ADDRESS:9200/_snapshot/TEST_backup/backupname
            method: PUT
            url_username: "username"
            url_password: "password"
            body:
              indices: "index names"
              ignore_unavailable: "true"
              include_global_state: "false"
            body_format: json
            validate_certs: no
    
    

    Note that I totally dropped the headers since body_format: json will automatically set Content-type: application/json (and we are not supposed to need the two others).

    In case your service on the other end is not returning 401 error correctly on first connection for basic auth, you can try to add force_basic_auth: true and see if it does any better (see doc link above for more details).