AnsiblePilot — Master Ansible Automation

AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,100 tutorials covering Ansible modules, playbooks, roles, collections, and real-world examples. Whether you are a beginner or an experienced engineer, our step-by-step guides help you automate Linux, Windows, cloud, containers, and network infrastructure.

Popular Topics

About Luca Berton

Luca Berton is an Ansible automation expert, author of "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example" published by Apress, and creator of the Ansible Pilot YouTube channel. He shares practical automation knowledge through tutorials, books, and video courses to help IT professionals and DevOps engineers master infrastructure automation.

Ansible uri Module: REST API Requests (GET, POST, PUT, DELETE)

By Luca Berton · Published 2024-01-01 · Category: troubleshooting

How to interact with REST APIs using Ansible uri module. Send GET, POST, PUT, DELETE requests, handle authentication, parse JSON responses with examples.

How to submit a GET request to a REST API endpoint with Ansible? I'm going to show you a live Playbook 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 • ansible.builtin.uri • Interacts with web services supports Digest, Basic, and WSSE HTTP authentication mechanisms

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 ansible.windows.win_uri module instead.

Parameters • url string - (http|https)://host.domain[:port]/path • method string - "GET", "POST", "PUT", "PATCH", "DELETE" • user (url_username), password (url_password) string - username, password credentials • force_basic_auth boolean - no,yes - Basic authentication header • status_code list/integer - [200, 202] • headers dictionary - custom HTTP headers, Content-Type • body_format string - raw, json, form-urlencoded, form-multipart • body raw - request body • return_content boolean - no/yes - return the body of the response • timeout integer - 30 seconds

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.

Links • https://reqres.in/ • https://reqres.in/api/users?page=2 • https://docs.ansible.com/ansible/latest/collections/ansible/builtin/uri_module.html

## Playbook

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

code • get_list_users.yml

API browser result (https://reqres.in/api/users?page=2):

execution

code with ❤️ in GitHub

Conclusion Now you know how to submit a GET request to a REST API endpoint with Ansible.

GET Request

POST Request (JSON)

PUT (Update)

DELETE

Authentication Methods

Parse Response

Health Check Pattern

Upload File

Key Parameters

| Parameter | Description | |-----------|-------------| | url | Request URL | | method | GET, POST, PUT, PATCH, DELETE | | body | Request body | | body_format | json, form-urlencoded, form-multipart, raw | | headers | HTTP headers dict | | status_code | Expected status codes | | return_content | Include body in result | | validate_certs | SSL verification (default: true) | | timeout | Request timeout (seconds) | | url_username | Basic auth username | | url_password | Basic auth password |

FAQ

How do I handle API pagination?

Can I disable SSL verification?

How do I handle rate limiting?

Use until/retries/delay or add pause between requests.

GET Request

POST Request

PUT (Update)

DELETE

Authentication

Health Check Pattern

Download File

Parse Paginated API

Error Handling

FAQ

uri vs get_url?

uri is for API interactions (any HTTP method, JSON parsing). get_url is for downloading files (simpler, supports checksums).

How to handle self-signed certificates?

Can I send form data?

Related ArticlesAnsible Become GuideAnsible Inventory GuideAnsible AWS GuideAnsible for Windows Guide

Category: troubleshooting

Watch the video: Ansible uri Module: REST API Requests (GET, POST, PUT, DELETE) — Video Tutorial

Browse all Ansible tutorials · AnsiblePilot Home