Ansible modules are the fundamental building blocks of Ansible automation. They are self contained scripts or programs that perform specific tasks on the target hosts. Ansible modules are used to perform various types of actions, such as file manipulation, package installation, system configuration, network management, and more.
In this blog, we will explore the different types of Ansible modules and how to use them.
Types of Ansible Modules
There are several types of modules available in Ansible, and they can be categorized as follows,
Command Modules
Command modules are the most basic type of Ansible modules. They execute a shell command on the target host and return the output. Command modules are useful for running simple tasks or scripts on remote hosts.
To use a command module, you need to specify the module name in your Ansible playbook, along with any required parameters.
For example, to execute the 'l
s’ command on the target host, you can use the ‘command
‘ module as follows,
System Modules
System modules are used to perform system-level tasks on the target hosts, such as managing users, groups, and system services. They can also be used for managing files, directories, and packages on the target hosts.
To use a system module, you need to specify the module name in your Ansible playbook, along with any required parameters.
For example, to install the ‘nginx
‘ package on the target host, you can use the ‘apt'
module (for Debian/Ubuntu systems) or the ‘yum
‘ module (for Red Hat/CentOS systems) as follows,
Network Modules
Network modules are used to manage network devices, such as routers and switches. They provide a way to automate network infrastructure tasks, such as configuring interfaces, VLANs, and routing protocols.
To use a network module, you need to specify the module name in your Ansible playbook, along with any required parameters.
For example, to configure the interface on a network device, you can use the ‘ios_interface'
module (for Cisco IOS devices) as follows,
Cloud Modules
Cloud modules are used to interact with cloud providers, such as Amazon Web Services (AWS) and Microsoft Azure. They provide a way to manage cloud resources, such as instances, storage, and networking.
To use a cloud module, you need to specify the module name in your Ansible playbook, along with any required parameters.
For example, to launch an EC2 instance on AWS, you can use the ‘ec2'
module as follows,
File Modules
File modules are used to manage files and directories on the target hosts. They can be used to create, delete, copy, move, or modify files and directories.
To create a new directory on the target host, you can use the ‘file
‘ module as follows,
To copy a file from the local machine to the target host, you can use the ‘copy'
module as follows,
Package Modules
Package modules are used to manage software packages on the target hosts. They can be used to install, upgrade, or remove packages.
To install a package on the target host, you can use the ‘apt
‘ module (for Debian/Ubuntu systems) or the 'yum
‘ module (for Red Hat/CentOS systems) as follows,
To remove a package from the target host, you can use the ‘apt
‘ or ‘yum
‘ module with ‘state: absent
‘:
Service Modules
Service modules are used to manage system services on the target hosts. They can be used to start, stop, restart, enable, or disable services.
To start a service on the target host, you can use the ‘systemd
‘ module (for systemd-based systems) or the ‘service
‘ module (for SysV-init based systems) as follows,
To stop a service on the target host, you can use the ‘systemd
‘ or ‘service
‘ module with ‘state: stopped
‘:
These are just a few examples of the many Ansible modules available. By combining different modules in your Ansible playbooks, you can automate complex tasks on remote hosts and devices.
FAQs
Q: How Do You Install Ansible Modules?
Ans: Ansible modules are included with Ansible by default, so there is no need to install them separately. However, some modules may require additional dependencies to be installed on the remote host.
Q: How Do You Find Ansible Modules?
Ans: Ansible modules can be found in the Ansible documentation or by running the command “ansible-doc -l” on the command line. This will list all the available modules on your system.
Q: How Do You Use An Ansible Module?
Ans: To use an Ansible module, you need to include it in a playbook or ad-hoc command. For example, to use the “apt” module to install a package on a Debian-based system, you would use the following syntax: “ansible <hostname> -m apt -a ‘name=<package_name>'”.
Conclusion
Ansible modules provide a powerful way to automate tasks on remote hosts. There are several types of modules available, each designed for a specific use case.
To use a module, you need to specify its name in your Ansible playbook, along with any required parameters. With Ansible modules, you can automate almost any
task on almost any type of system or device.