Ansible Pilot

Submit a GET request to a REST API endpoint - Interact with web services - Ansible module uri

How to retrieve a JSON list of users via a GET request to a REST API web service HTTPS endpoint from a remote Linux host in a few lines of Ansible code.
December 9, 2021
Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons

YouTube Video

How to submit a GET request to a REST API endpoint with Ansible?

I’m going to show you a live demo and some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.

Ansible submits a GET request to a REST API endpoint

Today we’re talking about Ansible module uri. The full name is ansible.builtin.uri, which means that is part of the collection of modules “builtin” with ansible and shipped with it. It’s a module pretty stable and out for years and it works in a different variety of POSIX operating systems. It interacts with web services and supports Digest, Basic, and WSSE HTTP authentication mechanisms. If you need to download content, use the Ansible ansible.builtin.get_url module. For Windows targets, use the module instead.


This module has some parameters to perform any tasks. The only required is “url”, where you specify the API URL. The parameter “method” specifies the HTTP method of the request: “GET”, “POST”, “PUT”, “PATCH”, “DELETE”. The parameters “user” and “password” specify the credentials to access the API. Several authentications methods are supported, for the simplest is the Basic HTTP authentication remember to enable the “force_basic_auth” boolean. The parameter “status_code” sets the expected single or list of expected HTTP status codes. The most commons are okay is 200, not fount is 404, and so on… If Please note that Ansible is going to return an error if the status code is different. The parameter “headers” set the custom HTTP headers and HTTP Content-Type. The parameter “body_format” sets the serialization format of the body content. Default is raw, but you could customize it to send an image for example. There are some restrictions with Content-Type and some serializations. The parameter “return_content” is very important to return the body of the response as a “content” key in the dictionary result. The default timeout is set to 30 seconds, but you could customize it with the “timeout” parameter.


Let’s jump into a real-life playbook on how to submit a GET request to a REST API endpoint with Ansible.


- name: uri module demo
  hosts: all
  become: false
    server: ""
    endpoint: "/api/users?page=2"
    - name: list users
        url: "{{ server }}{{ endpoint }}"
        method: GET
        status_code: 200
        timeout: 30
      register: result

    - name: debug


browser result (

{"page":2,"per_page":6,"total":12,"total_pages":2,"data":[{"id":7,"email":"[email protected]","first_name":"Michael","last_name":"Lawson","avatar":""},{"id":8,"email":"[email protected]","first_name":"Lindsay","last_name":"Ferguson","avatar":""},{"id":9,"email":"[email protected]","first_name":"Tobias","last_name":"Funke","avatar":""},{"id":10,"email":"[email protected]","first_name":"Byron","last_name":"Fields","avatar":""},{"id":11,"email":"[email protected]","first_name":"George","last_name":"Edwards","avatar":""},{"id":12,"email":"[email protected]","first_name":"Rachel","last_name":"Howell","avatar":""}],"support":{"url":"","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}


$ ansible-playbook -i virtualmachines/demo/inventory interract\ webservices/list_users.yml
PLAY [uri module demo] ****************************************************************************
TASK [Gathering Facts] ****************************************************************************
ok: []
TASK [list users] *********************************************************************************
ok: []
TASK [debug] **************************************************************************************
ok: [] => {
    "": [
            "avatar": "",
            "email": "[email protected]",
            "first_name": "Michael",
            "id": 7,
            "last_name": "Lawson"
            "avatar": "",
            "email": "[email protected]",
            "first_name": "Lindsay",
            "id": 8,
            "last_name": "Ferguson"
            "avatar": "",
            "email": "[email protected]",
            "first_name": "Tobias",
            "id": 9,
            "last_name": "Funke"
            "avatar": "",
            "email": "[email protected]",
            "first_name": "Byron",
            "id": 10,
            "last_name": "Fields"
            "avatar": "",
            "email": "[email protected]",
            "first_name": "George",
            "id": 11,
            "last_name": "Edwards"
            "avatar": "",
            "email": "[email protected]",
            "first_name": "Rachel",
            "id": 12,
            "last_name": "Howell"

code with ❤️ in GitHub


Now you know how to submit a GET request to a REST API endpoint with Ansible. Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.


Learn the Ansible automation technology with some real-life examples in my

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Access the Complete Video Course and Learn Quick Ansible by 200+ Practical Lessons
Follow me

Subscribe not to miss any new releases