Ansible Modules

10 Ansible Modules You Need to Know

Ansible is a popular and widely uses open source IT configuration automation and management tool. The platform employs human-readable and easily understandable YAML templets. This lets the users program continuous tasks with the Ansible modules that can happen automatically without any requirement of advanced level programming language.

The tool is completely agentless. That means the nodes or servers Ansible manages, don’t require any software. This dramatically eliminates possible security issues and makes the IT configuration management more secure and smoother. But how Ansible works? Let’s know more about Ansible and Ansible best practices.

The tool works by connecting to the nodes and transmitting small programs which are called Ansible modules. Most of the Ansible modules can be executed remotely. This makes the tool a push architecture. Here different configurations are pushed from Ansible to different servers without the help of any agents. This is quite different from the pull model. Speaking about this, it is an agent-based configuration management system where the IT configurations are pulled.

Kubernetes Training Course

What are the Ansible Modules?

Ansible modules are the standalone scripts which use inside the ansible-playbook. The playbook includes a play, and a play includes different tasks. If you are reading about Ansible for the first time, then you may find it very confusing. But by working and writing more with the Ansible playbooks, you can make yourself familiar with all these things.

The modules are mapped to resources and their states presented in YAML files. It is essential to know about must know ansible modules. They let the developers manage everything virtually. These are firewalls, load balancers, containers themselves, container orchestrators, AWS, Azure, OpenStack, private cloud, and security configuration.

There are some top ansible modules that are frequently used to automate different tasks. In this article, we will cover those top and must know ansible modules. However, before going into that, there are three important files you must consider. These are:

  • Inventory/ host file: This file contains all the entry of nodes which will manage.
  • Main file: The playbook which has some crucial modules that can carry out different tasks on a host.
  • Ansible.cfg file: It is located at /etc/ansible/ansible.cfg. The file carries important privilege escalation options and also the inventory file’s location.

Also Check: Chef vs Puppet

When Should You Develop an Ansible Module?

Even though Ansible comes with different modules, there are some chances that your issues are not yet covered. For example, a solution that makes sense in your company. Fortunately, ansible-galaxy offers some important guidelines about developing and installing modules and roles. Remember that, before you start developing something new; always check the ansible-galaxy website to see if a module or role exists.

Signs When You Need a New Module

  • Traditional configuration management methods, such as template module, file, etc. do not resolve your issues.
  • When you need to use some complex sets of commands or API calls through curl to complete your task.
  • The playbook is quite complicated and non-deterministic.

Certification helps the professionals to validate their skills and get global recognition for them. Check out the list of Top DevOps Certifications and be determined to get one!

Top AWS Modules You Need to Know

Not to mention, there are a number of Ansible modules that you have to use in your project for different functions. However, there are some top ansible modules which can help you in most of the project. These are easy-to-use Ansible modules and you should know them if you are working with Ansible. Let’s have a look at those modules.

Module 1- Package Management

There are some modules for package managers, for example, APT and DNF which let the users install different packages. Even though the functionality greatly depends on the package manager, these modules can upgrade, remove, install, and downgrade packages. The names for the modules are easy to understand. For example, the ansible yum module is yum_module, and the DNF module is dnf_module, etc. Some of the examples of this module are:

To install the Apache Web Server and MariaDB SQL:

- name: install the latest version of Apache and MariaDB

dnf:

  name:

  - httpd

  - mariadb-server

  state: the latest

To install and download packages:

- name: Install a list of packages

 yum:

 name:

 - nginx

 - PostgreSQL

 - PostgreSQL-server

 state: present

DevOps Engineer Professional Free Test

Module 2- Ansible Yum Module

This module is generally used to install different services. The syntax for this module is

ansible test-servers -m yum -a 'name=httpd state=present' -become -u ec2-user

This will install Apache 2 in the machines. The most important thing that you need to know that you need to use -become, the new in 2.6 version. Before, it was -s in the older version.

Module 3- Ansible Command Module

The developers or experts use the Ansible command module to carry out commands on the remote node. They widely use this module to execute simple Linux commands on the remote servers. It is part of the standalone or host group server. If you want to perform some shell commands on a remote node, then you can use the command module.

However, if there are some complex shell commands or multiple commands combined with PIPE, this command module will not work. In such a condition, you need to use the shell module. Besides PIPE, if y you choose a character like |>< in the command, then you must use the shell module.

ls -lrt /etc/httpd/conf 

It is OK and is perfect for the command module.

Check Now: Ansible certification

Module 4- Ansible User Module

The ansible user module helps in managing different user’s accounts in the Linux-based system. Besides, using this, you can create, sett passwords, add groups, and delete the user’s account. For example, let’s see how to create a user with this module.

Add the user ‘exampleuser’ with a specific uid and a primary group of ‘admin.’

- user:

 name: exampleuser

 comment: "Example User."

UID: 1040

 group: admin

There is only one important parameter, and that is the ‘name’ parameter. This is the user account name. However, if you don’t give a password, the user account will display in a locked state. That means you will not be able to log in through the password option.

Also Read: List of Top DevOps Tools

Module 5- Ansible Lineinfile Module

Talking about Ansible lineinfile module, this is generally used to alter or remove the existing line, insert line, and to replace the lines. Let’s know about the process to insert a line. You can set the file’s path to modify using the path/ dest parameter. You can insert lines through the line parameter. The line enters to the EOF. However, if the line is already there in the system, it won’t be added.

- hosts: loc

 tasks:

 - name: Ansible insert lineinfile example

lineinfile:

dest: /home/mdtutorials2/remote_server.txt

Line: Inserting a line in a file.

state: present

create: yes

Module 6 – Ansible File Module

Creating different new files is a common task in the server scripts. In Ansible tools, you will find various methods for creating a new file. You can even set different group permission, assign an owner to the file; create a file with content, and more. It sets attributes of directories, symlinks, and files. Besides, it removes symlinks, directories, and file.  To create a new file in Ansible, first, you need to set two critical parameters.

These are:

  • Path – It refers to the location where the users want to create a new file. There are two types of paths, i.e., relative and absolute paths. You need to include the file name which you want to create.
  • State – While creating a new file, you need to set the state parameter to touch. It functions as the touch operation in Linux.

Let’s see how to create a file with a name foo-conf and set the permission to 0644 using ansible file module.

- name: Change file ownership, group, and permissions

file:

 path: /etc/foo.conf

owner: foo

group: foo

mode: '0644.'

Docker Practice Tests

Module 7- Ansible Template Module

Creating static files to manage server configuration is not a perfect solution. If there is an efficient way, it would be beneficial. This is where you can use an Ansible template module. The templets contain all the configuration parameters. During the execution of the playbook, the variables replaced with the necessary values. You can carry out more than just replacing different variables using the Jinj2 templating engine. You can also go for loops, arithmetic calculations and more.

Example-

hosts: all

vars:

variable_to_be_replaced: 'Hello world'

inline_variable: 'hello again.'

tasks:

- name: Ansible Template Example

template:

Src: hello_world. j2

dest: /Users/mdtutorials2/Documents/Ansible/hello_world.txt

Module 8- Ansible Windows Modules

Ansible windows modules must be tested for windows hosts. To make Ansible manage the windows machines, you need to enable and configure the PowerShell. Keep in mind that you need a Linux Control Machine. You can’t perform a task from a Windows host. Some of the Ansible Windows modules are –

win_acl - Set file/directory/registry

Sets permissions for a system user or group

win_audit_rule

Adds an audit rule to files, folders or registry keys

win_scheduled_task_stat

Get information about Windows Schedules Tasks

Module 9 – Ansible Archive Module

This module creates the compressed archives of different files. The module assumes that the source of compression exists on the target. Let’s have a look into an example.

- name: Compress directory /path/to/foo/ into /path/to/foo.tgz

archive:

path: /path/to/foo

Dest: /path/to/foo.tgz

Module 10- Ansible Cli_Command Module

The module which is available in Ansible version 2.7, offers a platform-agnostic method to push text-based configurations to devices through the network_cli connection plugin. For example, to sets the hostname and exit with a message, use the following command:

- name: commit with comment

cli_config:

config: set system host-name foo

commit_comment: this is a test

Preparing for an Ansible interview? Go through these top Ansible interview questions and get ready to crack the Ansible interview.

Conclusion

There is a lot of Ansible modules available in the latest version of Ansible with a number of commands. However, these ten common Ansible modules are the most powerful ones. You can use them to automate different projects. With the change in your requirements, you can learn more about other important modules from Ansible official documentation. Learn more about them and be an Ansible expert.

Ansible is one of the top DevOps tools among DevOps professionals. If you are a DevOps professional preparing for one of the top AWS DevOps training or Azure DevOps certifications, enroll for the DevOps certification training courses and give your preparation a new edge.

About Dharmalingam N

Dharmalingam.N holds a master degree in Business Administration and writes on a wide range of topics ranging from technology to business analysis. He has a background in Relationship Management. Some of the topics he has written about and that have been published include; project management, business analysis and customer engagement.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top