Some Ansible modules that you should know

Tram Ho

Ansible is a “Configuration Management” tool, ie a tool that supports, configures, and installs the system automatically. These tools enable smooth system implementation, reduce repetitive tasks, save time, and can be deployed to similar systems.

One of the key concepts in ansible is modules . Modules are a core component of Ansible, it is a block / processing unit of a task (the task will add variables and parameters to make the processing on the remote server more flexible). In Ansible, instead of saying, “Ansible, execute this command!” . then you will say: “Ansible, execute this module and allow it to run whatever command it needs to get the job done!” . Through the process of learning and using Ansible, I will together with you summarize some basic, frequently used Ansible modules that you should know!

Module 1: Package management

This module is designed for most of the popular package management (package management) such as apt, dnf, etc., allowing you to install packages on your system. In addition to the specific functions of each package manager, most call managers can install, update, upgrade, downgrade, remove, and list packages.

For example, here I have a task that is to install the httpd package on CentOS / RHEL 8 using the dnf package manager.

Module 2: Flie

In Ansible, there are many modules that work with files, folders, links on destination nodes (client node) such as copy, template, file, … which are commonly used. First, we will learn about module files. The file module helps to manage the file and its properties. In addition to creating, deleting, and locating module files also set the rights and ownership or set symlinks for the file.

For example,

Module 3: Template

There are many different ways with Ansible to help you manipulate the contents of the file, in addition to the template module, I also find the lineinfile module is also very popular and used a lot. However, for me, after using it, I feel that the template module is much clearer and easier to understand than the lineinfile module.

The template in Ansible is a file that contains all of your configuration parameters, but the dynamic values ​​are provided as variables. During playbook execution, the variables can be replaced with values ​​you need. Additionally, you can do a lot more than replace variables, with the help of the Jinj2 template generator. You can have conditional statements, loops, write macros, filters to transform data, do arithmetic operations, etc. Template files will usually have the extension .j2 . Variables in the template file will be denoted with double quotes, ‘{{variable}}’.

For example:

Module 4: Copy

Copy module is the module commonly used when we want to copy a file from the Ansible server (Management node) to the destination node (client node).

For example:

Modlue 5: Service

For Unix / Linux client nodes, the service module is a very useful module to control the services running on these servers. Like other modules, the service module also comes with some parameters and these parameters have their own options or values ​​accordingly. Using these parameters and required values, you can manage services with functions such as stop, start, reload, … on client nodes.

For example:

Module 6: shell

In Ansible, we have a shell module that is used to run commands through shell (/ bin / sh) on remote target machines. This module takes commands as input along with a set of arguments.

For example:


At this point, hopefully you can remember and use some of the basic Ansible modules outlined above. Ansible comes bundled with thousands of different available modules and is still expanding, you can read more here . Besides, with Ansible, you can also write your own Ansible modules to suit your needs.

Reference source

Share the news now

Source : Viblo