Bootstrap FreeKB - Ansible - Submit a GET request to a REST API using the uri module
Ansible - Submit a GET request to a REST API using the uri module

Updated:   |  Ansible articles

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

The uri module can be used to submit a GET request to a REST API.

---
- hosts: all
  tasks:
  - name: submit GET request
    uri:
      url: http://example.com/api/v1
      method: GET
    register: out

  - debug:
      var: out
...

 

If the GET request fails, something like this will be returned.

fatal: [server1.example.com]: FAILED! => {
  "changed": false,
  "content": "",
  "elapsed": 0,
  "msg": "Status code was -1 and not [200]: Request failed: <urlopen error [Errno 111] Connection refused>",
  "redirected": false,
  "status": -1,
  "url": "http://www.example.com/foo.txt"
}

 

On the other hand, if the request is successful, something like this should be returned.

ok: [server1.example.com] => {
    "var": {
        "cache_control": "no-cache, no-store", 
        "changed": false, 
        "connection": "close", 
        "content_disposition": "attachment; filename=\"foo.txt\"; filename*=UTF-8''foo.txt", 
        "content_length": "681479272",
        "content_type": "application/octet-stream", 
        "cookies": {
            "BIGipServerOC3P1-130-BITBK-EPO-T8080": "199364482.36895.0000"
        }, 
        "cookies_string": "BIGipServerOC3P1-130-AITBK-APO-T8080=799123082.36895.0000", 
        "date": "Fri, 19 Jun 2020 06:02:32 GMT", 
        "elapsed": 0, 
        "expires": "Thu, 01 Jan 1970 00:00:00 GMT", 
        "failed": false, 
        "msg": "OK (unknown bytes)", 
        "pragma": "no-cache", 
        "redirected": false, 
        "set_cookie": "BIGipServerOC3P1-130-AITBK-EPO-T8080=799399082.36895.0000; path=/; Httponly; Secure", 
        "status": 200, 
        "transfer_encoding": "chunked", 
        "url": "https://www.example.com/foo.txt", 
        "x_arequestid": "*8MTOVAx62x865730x0", 
        "x_asen": "SEN-13899879", 
        "x_auserid": "1", 
        "x_ausername": "root", 
        "x_content_type_options": "nosniff", 
        "x_download_options": "noopen", 
        "x_frame_options": "SAMEORIGIN", 
        "x_xss_protection": "1; mode=block"
    }
}

 

You may need to include return_content to get the data being returned by the REST API.

---
- hosts: all
  tasks:
  - name: submit GET request
    uri:
      url: http://example.com/api/v1
      method: GET
      return_content: yes
    register: out
    failed_when: out.status != 204

  - debug:
      var: out
...

 

Sometimes the REST API will return a response code that Ansible thinks indicates that the GET request failed, when in fact the GET request was successful. For example, I had an REST API that was returning a status code of 204 when the reqeust was successful, and Ansible interpreted this as a failure. To account for this, I added failed_when so that the task was not failed when the response code was 204.

---
- hosts: all
  tasks:
  - name: submit GET request
    uri:
      url: http://example.com/api/v1
      method: GET
    register: out
    failed_when: out.status != 204

  - debug:
      var: out
...

 

If the GET request returns something like certificate verify failed you may be able to get around this by including validate_certs: false.

---
- hosts: all
  tasks:
  - name: submit GET request
    uri:
      url: https://example.com/api/v1
      method: GET
      validate_certs: false
...

 

If the REST API requires basic authentication, the force_basic_authurl_username and url_password options can be included.

---
- hosts: all
  tasks:
  - name: submit GET request with basic auth
    uri:
      url: http://example.com/api/v1
      method: GET
      force_basic_auth: yes
      url_username: john.doe
      url_password: itsasecret
    register: out

  - debug:
      var: out
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter c11b7a in the box below so that we can be sure you are a human.