AnsiblePilot — Master Ansible Automation
AnsiblePilot is the leading resource for learning Ansible automation, DevOps, and infrastructure as code. Browse over 1,400 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 8 Ansible books published by Apress and Leanpub including "Ansible for VMware by Examples" and "Ansible for Kubernetes by Example", 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.
Creating Custom Ansible Plugins to Fetch API Data Easily — Video Tutorial
Learn how to create custom Ansible plugins to fetch data from APIs. Enhance your automation tasks with this detailed step-by-step guide.
What You'll Learn
- Introduction
- Why Create a Custom Plugin?
- Our Goal
- Prerequisites
- Step-by-Step Guide
- Step 1: Directory Structure
- Step 2: Write the Plugin Code
- Step 3: Using the Custom Plugin in a Playbook
- Step 4: Running the Playbook
- Conclusion
Full Tutorial Content
Introduction
Ansible is a powerful automation tool used for configuration management, application deployment, and task automation. While it comes with a rich set of modules and plugins, there are times when you might need to extend its capabilities by writing custom plugins. In this article, we will walk you through the process of creating a custom Ansible plugin to retrieve user data from an API endpoint.
Why Create a Custom Plugin?
Creating a custom plugin allows you to:
- Extend Ansible's functionality to meet specific needs.
- Integrate with external APIs that are not covered by existing modules.
- Simplify complex tasks by encapsulating them in reusable plugins.
Our Goal
We will create a custom lookup plugin that fetches a list of users from `https://reqres.in/api/users?page=2` and makes this data available for use in Ansible playbooks.
Prerequisites
- Basic understanding of Ansible and Python.
- Ansible installed on your machine.
- Internet connection to access the API.
Step-by-Step Guide
Step 1: Directory Structure
Create the necessary directory structure for the custom plugin. By default, Ansible looks for plugins in the `lookup_plugins` directory within your project.
```sh
mkdir -p my_ansible_project/lookup_plugins
```
Step 2: Write the Plugin Code
Create a file named `list_users.py` inside the `lookup_plugins` directory with the following content:
```python
python 3 headers, required if submitting to Ansible
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
DOCUMENTATION = r"""
name: list_users
author: Your Name
version_added: "0.1"
short_description: Retrieve list of users from an API
description:
- This lookup retrieves the list of users from the provided API.
"""
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from ansible.utils.display import Display
import requests
display = Display()
class LookupModule(LookupBase):
_URL_ = "https://reqres.in/api/users?page=2"
def run(self, terms, variables=None, **kwargs):
try:
res = requests.get(self._URL_)
res.raise_for_status()
users = res.json().get('data', [])
except requests.exceptions.HTTPError as e:
raise AnsibleError(f'There was an error retrieving users. The lookup API returned {res.status_code}')
except Exception as e:
raise AnsibleError(f'Unhandled exception in lookup plugin. Origin: {e}')
return users
```
This code defines a custom lookup plugin that makes a GET request to the specified API endpoint and returns the list of users. The `run` method handles the API request and error handling.
Step 3: Using the Custom Plugin in a Playbook
Now that we have our custom plugin, let's use it in an Ansible playbook. Create a playbook file named `test_plugin.yml` with the following content:
```yaml
---
- name: Test custom plugin
hosts: localhost
tasks:
About This Tutorial
- Author: Luca Berton
- Difficulty: Advanced
- Read time: 3 min
- Category: installation
Read the full written article: Creating Custom Ansible Plugins to Fetch API Data Easily