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 Modules: Complete Guide to Built-in & Custom Modules

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

Complete guide to Ansible modules. Understand built-in modules, collection modules, how to find, use, and create custom modules for automation.

Ansible modules are the foundation of Ansible's automation capabilities. They are small programs that perform specific tasks, enabling you to automate everything from software installation to cloud provisioning. This article explains what Ansible modules are, their types, and how to use them in playbooks.

What Are Ansible Modules?

Ansible modules are standalone units of code executed by Ansible to perform specific tasks on managed nodes. These tasks can range from system configuration and file manipulation to cloud infrastructure provisioning.

Modules are also referred to as "task plugins" or "library plugins" and are invoked in Ansible playbooks as tasks.

Key Features of Ansible Modules:Idempotence: Modules are designed to achieve the same result regardless of how many times they are run. • Agentless Execution: Modules execute over SSH or WinRM without requiring agents on the target systems. • Extensibility: You can create custom modules to extend functionality.

Types of Ansible Modules

Ansible includes a wide variety of modules categorized by their functionality:

1. Core Modules • Maintained by the Ansible team and included in all installations. • Examples: file, user, service, package.

2. Cloud Modules • Manage resources in cloud platforms like AWS, Azure, and Google Cloud. • Examples: amazon.aws.ec2, azure.azcollection.azure_rm_vm.

3. Networking Modules • Automate network device configurations. • Examples: ios_config, junos_config, nxos_command.

4. Database Modules • Manage databases like MySQL, PostgreSQL, and MongoDB. • Examples: mysql_user, postgresql_db.

5. Custom Modules • User-created modules to meet specific requirements.

Anatomy of an Ansible Module Task

Here’s an example task using the apt module to install Nginx:

Task Components:name: A description of the task. • apt: The module used for the task. • name: The package to be installed. • state: Ensures the package is installed.

Commonly Used Ansible Modules File Management • copy: Copy files to target machines. • template: Deploy Jinja2 templates. • file: Manage file and directory properties. Service Management • service: Start, stop, and manage services. • systemd: Interact with systemd services. Package Management • yum: Manage packages on RHEL-based systems. • apt: Manage packages on Debian-based systems. Cloud and Virtualization • ec2: Provision AWS EC2 instances. • vmware_guest: Manage VMware virtual machines. User Management • user: Create, update, or delete user accounts. • group: Manage user groups.

Writing Custom Ansible Modules

Custom modules can be written in Python, PowerShell, or any scripting language. A simple Python module follows this structure:

Using Modules in Playbooks

Modules are called in tasks within a playbook. Example playbook to ensure a service is running:

Conclusion

Ansible modules are the essential building blocks of Ansible automation. Whether you're managing servers, deploying applications, or provisioning cloud resources, modules enable you to perform tasks efficiently and consistently.

Explore More About Ansible Modules in the Official Documentation

Module Categories

| Category | Examples | Use Case | |----------|---------|----------| | System | user, group, service, cron | OS configuration | | Files | copy, template, file, lineinfile | File management | | Packages | apt, yum, pip, snap | Software installation | | Cloud | ec2_instance, azure_rm_virtualmachine | Cloud resources | | Network | ios_config, nxos_command | Network devices | | Database | mysql_db, postgresql_user | Database management | | Windows | win_copy, win_service, win_user | Windows automation |

Using Modules

FQCN (Fully Qualified Collection Name)

Module Return Values

Find and Explore Modules

Key Built-in Modules

| Module | Purpose | |--------|---------| | copy | Copy files to remote | | template | Render Jinja2 templates | | file | Manage files/dirs/links | | lineinfile | Edit lines in files | | apt / yum | Package management | | service | Manage services | | user / group | User management | | command / shell | Run commands | | uri | HTTP requests | | debug | Print messages | | set_fact | Set variables | | assert | Validate conditions |

Install Collection Modules

Write a Custom Module

FAQ

Modules vs Plugins?

Modules are a type of plugin that execute tasks on remote hosts. Other plugins (callback, lookup, filter) extend Ansible's core functionality.

How do modules execute?

Ansible copies the module to the remote host, executes it with Python, captures JSON output, then removes the module. Exception: raw module runs without Python.

What's the difference between command and shell?

command runs without a shell (no pipes, redirects). shell runs through /bin/sh (supports |, >, &&). Prefer command for security; use shell when you need shell features.

What Are Modules?

Modules are discrete units of code that Ansible executes on managed nodes. Each module handles a specific task:

Find Modules

Common Module Categories

Files

Packages

System

Commands

Networking & Cloud

Module Return Values

Module Parameters

Collections vs Built-in

FAQ

How many modules are there?

Thousands — ansible.builtin has ~70 core modules. Collections add thousands more for cloud, networking, databases, etc.

Can I use modules without a playbook?

Yes — ad-hoc commands: ansible all -m ping or ansible web1 -m apt -a "name=nginx" -b

What language are modules written in?

Mostly Python, but modules can be any executable (Bash, Go, Ruby, etc.).

Related ArticlesAnsible Template GuideAnsible Nginx GuideAnsible AWS Guide

Category: installation

Browse all Ansible tutorials · AnsiblePilot Home