LinuxUnity LinuxUnity

Posts / Ansible

[Ansible] Part 4: Exploring Commonly Used Modules in Ansible

What are Ansible modules? Think of a module as a simple tool for performing specific tasks on remote machines.

11/07/2026 6 min read 19 views

Z Zedx

In this article

What are modules in Ansible?

Think of a module as a tool that performs a specific task on a remote machine. Each module helps automate an operation that you would normally have to perform manually, such as creating a directory, installing software, or starting a service. You only need to issuea commandthrough a Playbook or call the module directly, and Ansible will handle the rest.

Ansible has a large number of modules, organized into groups to make them easier to manage and use for specific purposes. The main groups of modules in Ansible are listed below:

  • Core Modules: Perform basic operations such as managing files, services, users, and software.
  • Cloud Modules: Manage infrastructure on cloud platforms such as AWS and Azure.
  • Networking Modules: Manage network devices from vendors such as Cisco and Juniper.
  • Security Modules: Configure and manage security policies such as firewalld, iptables, and SELinux.
  • Database Modules: Manage and interact with databases.
  • Container Modules: Manage Docker, Podman, or Kubernetes containers.
  • Windows Modules: Manage systems running the Windows operating system.
  • Custom Modules: Create custom modules to meet an organization's specific requirements.

Ansible currently has more than 2,000 modules, most of which are written in Python. You are not limited to the available modules; you can write your own modules to extend functionality or perform specialized tasks that the default modules cannot support.

To use each Ansible module correctly and effectively, you can refer to the official documentationor use some of the commands and built-in tools integrated into Ansible.

Syntax:

text
ansible-doc <module_name>

Example using the apt module:

ảnh minh hoạ ansible doc

The output displays the required parameters for a module, along with examples of how to use the module in a playbook. In this case, the parameters include name (the package name), state (the desired state),… as well as examples of how to install or remove the httpd package.

Some commonly used modules

Package management module

This module is designed for most popular package managers, such as apt, yum, dnf,… and allows you to install packages on the system. In addition to the functionality specific to each package manager, most of them support install, update, upgrade, downgrade, remove, and list operations for packages.

For example, suppose we have a task to install the apache2 package on Ubuntu. We will certainly need the apt module.

text
ansible web1 -i inventory-lab -m apt -a "name=apache2 state=present"
  • -m apt: apt module
  • -a: arguments passed to this module
  • name: name of the package to install
  • state: state after installation

In state there are states such as:

  • present: Ansible checks whether the apache2 package is already installed on the server. If it is installed, Ansible skips it and does not install it again. If it is not installed, Ansible proceeds with the installation.
  • latest: Ansible checks the repository for the latest version of the apache2 package and installs it. If the apache2 package is already present, Ansible checks for and installs the latest version of the package, if available.
  • absent: remove the apache2 package

Therefore, with the command above, Ansible installs apache2 on the web1 server.

  • changed: true indicates that the web1 server did not previously have this package and that Ansible installed it.
  • stdout: displays the output corresponding to when you execute the command sudo apt install apache2 on the web1 server itself.

ảnh minh hoạ module apt ansible

Next, run the command to install apache2 again. Parameters changed to display false This means that the apache2 package already exists on the web1 server, so Ansible does not install it.

Service module

This module is very useful for controlling services running on remote servers. Like other modules, the service module also includes a number of parameters, each with its own options or appropriate values. Use these parameters and their required values to manage services with functions such as stop, start, and reload, … on remote servers.

Continuing with the example above, for apache2 to operate, you need to start and allow this service to start automatically after each server reboot.

text
ansible web1 -i inventory-lab -m service -a "name=apache2 state=started enabled=true"

Use a browser to access http://<IP_web1> to check the web server that was just installed.

Change state to stopped Stop service apache2.

text
ansible web1 -i inventory-lab -m service -a "name=apache2 state=stopped"

Module setup

Module setup is used to collect information about the system (facts) of the remote servers you manage. These facts include information such as the operating system version, CPU, disk, IP address, network information, and many other parameters. This information can be used to make logical decisions in playbooks or generate system reports.

Syntax:

text
ansible web2 -i inventory-lab -m setup

Ansible uses this module to collect facts about the web2 server and assign them to variables.

Using the filter parameter to retrieve only specific data. For example, with the filter ansible_distribution*the result displays only information related to the operating system of the web2 server.

Some other parameters that you can filter include:

Information about the operating system:

  • ansible_distribution: Operating system distribution name (Ubuntu, CentOS, etc.)
  • ansible_os_family: Operating system family (Debian, RedHat, etc.)

Network information:

  • ansible_default_ipv4: Default IP address.
  • ansible_interfaces: List of network interfaces.

Hardware information:

Shell module

The shell module allows you to run shell commands on remote servers. It is a powerful tool for performing tasks that are not available through other Ansible modules or when you need to run custom scripts.

For example, you may need to execute a command cagefsctl --set-tmpwatch='/usr/sbin/tmpwatch -umclq 4'on hosts for which Ansible does not yet have a suitable module to support this operation. Therefore, you will need to execute the command through the shell module.

Syntax:

text
ansible web1 -i inventory-lab -m shell -a "<your_command>"

Conclusion

Thus, we have understood the function of modules in Ansible and reviewed several commonly used modules presented in Ad hoc form. In the next article, we will explore Playbooks along with several other modules, which will be used according to the Playbook structure.

Series: Ansible · Part 4/8

  1. Intro Automation in Linux Configuration Management
  2. Part 1 [Ansible] Part 1: Introduction to Ansible
  3. Part 2 [Ansible] Part 2: Installing and Getting Started with Ansible
  4. Part 3 [Ansible] Part 3: How to Create and Manage Inventory Effectively in Ansible
  5. Part 4 [Ansible] Part 4: Exploring Commonly Used Modules in Ansible
  6. Part 5 [Ansible] Part 5: A Beginner’s Guide to Writing Simple, Effective Playbooks
  7. Part 6 [Ansible] Part 6: Writing an Ansible Playbook to Install WordPress on a LAMP Stack
  8. Part 7 [Ansible] Part 7: Install Ansible CMDB and Create System Reports in Minutes

Related posts