Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Types of Ansible Modules
2.1.
Core Modules
2.2.
Extra Modules
2.3.
Plugins
3.
Ansible Module Examples
3.1.
Debug
3.2.
Package
3.3.
Firewalld
3.4.
User
3.5.
Template
3.6.
Shell
4.
Frequently Asked Questions
4.1.
How do we get a list of Ansible modules?
4.2.
How do we find an Ansible module code?
4.3.
How to debug an ansible module?
5.
Conclusion
Last Updated: Mar 27, 2024

Ansible Modules

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

Ansible combines several parts that work together to provide an automation tool. These are primarily modules, playbooks, and plugins. Ansible operates by connecting to nodes and sending little programmes, known as modules, to these nodes. In Ansible, modules get utilised to complete automated tasks. Modules are short pieces of code that will get executed. Several built-in modules serve as a jumping-off point for creating jobs. These programmes are designed and made to be resource models of the ideal state of the system. Ansible then runs these modules and removes them when they are finished. Ansible modules are reusable, standalone scripts used by the Ansible API and the Ansible or ansible-playbook applications. They exit by sending a JSON string to stdout before returning information to Ansible.

Ansible Modules

Types of Ansible Modules

Ansible modules are similar to functions or independent scripts in that they conduct particular tasks indefinitely. These return json strings in stdout, and the input depends on the module type. These are used in Ansible playbooks.

Ansible modules are classified into two types:

  1. Core Modules
  2. Extra Modules

Core Modules

The core Ansible team maintains these modules. As a result, these are included with Ansible. The reported issues have a higher priority than those in the "extras" repo.

Extra Modules

The Ansible community maintains these modules. So, for the time being, these are shipped with Ansible. They may, however, be phased out in the future. These can be utilised. However, if any feature requests or issues arise, they will be addressed on a low-priority basis. Popular supplementary modules may now be incorporated into core modules at any moment. These distinct repositories for these modules can be found as ansible-modules-core and ansible-modules-extra, respectively.

Plugins

Plugins are distinct modules that operate on the primary control computer to log data. Other types of plugins exist as well.

Ansible Architecture and Modules
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Ansible Module Examples

Now we will know about a few different modules. They are good to know and learn when we start or learn Ansible. The modules we will discuss are the debug, package, firewall, user, template and shell/command. For each module, we will describe what they will do, and we will have some samples to discuss together.

Debug

Let us start with our first module, "debug". The debug module is handy when we want to display stuff on the screen or when it does not work. We want to see the version, see if a step succeeded, and know why a condition was not met. We will then use the debug message to put it on the screen. Let us have a look at an example.

debug
msg: This is a debug message
debug
msg: "This is a debug message ((variabale}}"
debug
var: output
name: This is a debug task with a name attached.
debug
msg: "This is a debug message {(variabale}}"

The most simple example is the first one here. Just make a task called debug and add a message parameter. We must use quotes around the whole string to use a variable in the second example. If we want to display a variable, then we can use debug var option. Starting all tasks in our playbooks with a name tag is good practice.

So the last example starts with a name tag. this is a debug task with the name attached, which is just lovely to see. Otherwise, also in the login, we will only see debug, and we will see the whole message. Which is a little bit nicer, and as we said, it is an excellent practice to work like that.

Package

The next module we are going to discuss is the "package". Use the package module when we need to install an OS package. No need to know what package manager is used. However, we can specify it, but it is not needed. Let us have a look at some examples. 

name: install the latest version of telnet
package:
name:  [telnet, tcsh]
state: latest
name: just install telnet, don't care about the version
package:
name: telnet
state: present
name: make sure no telnet is available
package:
name: telnet
state: absent

Here we have three examples. The first example installs two packages. "telnet" and  "c shell". It will inform Ansible to install the latest version available. The second example tells it to install telnet. However, it does not specify the latest. It just says to make sure it is installed. The last example is how we can say it must not be installed. So it must be absent, which is the option we can use. The state is absent, and depending on our playbook, we can use the latest or present depending on the speed we need. For example, telnet, we could make sure it is present because it is not that important that we have the latest features of telnet.

Firewalld

The next module we will look at is a firewall daemon. Manage firewall ports from our code. Quickly easily, and documented. Open ports temporarily or indefinite. Let us have a look at some examples. 

name: Close port 80 permanently
firewalld:
port: 80/tcp
permanent: yes
state: disabled
name: "Open port 443 for HTTPS immediately"
firewalld
port: 443/tcp
permanent: yes
state: enabled
immediate: yes
name: "Open port {{port}} from variable port"
firewalld:
port: "{{port}}/tcp"
permanent: yes
state: enabled
immediate: yes

We have three examples. Option one is to close port 80 and make this permanent. The second example makes sure that the HTTPS port 443 is opened immediately. So in the previous example, port 80 will stay active until the firewall daemon has reloaded the rules or has been restarted. The second example makes it active immediately, and we can also use variables for ports.

User

Then we move on to the next module, the user module. The module to create, change and delete users on Linux. We have three examples here.

name: Create user CodingNinjasIndia
user
name: CodingNinjasIndia
comment: CodingNinjasIndia admin user
name: Create user CodingNinjasIndia part of group youtube (and adm)
user:
name: CodingNinjasIndia
comment: CodingNinjasIndia admin user
group: youtube
groups: adm
name: Create group youtube
group:
name: youtube
state: present

We create a user "CodingNinjasIndia", and we can give it a name. There are comments. We can also add groups to the user, so in this example, we add the group youtube and the group "adm". so a Linux user can be part of multiple groups. In the last example, we also show how to create a group. We can also specify group ids and user ids if the home directory needs to be in a different place Etc.

We can also remove users by making them absent. So we have got another state tag. We can then say absent. We can set passwords. Everything is possible to manage the users in this way.

Template

Then we move over to the next module, which is the template. We can use templates to move files from the code to the servers and change the variables in those templates, which is very handy.

name: Copy 01-CodingNinjasIndia file to the sudoers dir
template: 
src: "templates/01-CodingNinjasIndia.j2"
dest: "/etc/sudoers.d/01-CodingNinjasIndia"
owner: root
group: root
mode: '0644'


# Managed by Ansible Automation
# Source location: templates/01-CodingNinjasIndia.j2
#Target location: /etc/sudoers.d/01-CodingNinjasIndia
%youtube ALL=({{application} }adm) NOPASSWD: ALL

In this example, we have only one example here. However, we create a new sudoers file, allowing the group youtube to become an application adm user and execute all commands without a password. The template is stated below the example and is in the code in the templates directory. The destination is on the remote server where we run the code. We can set it on our group mode; the file will be copied in this example. The application variable will be replaced with the application variable value. Which, for example, is stated in our inventory or playbook.

Shell

The following module is "shell". We can use it to execute OS commands to do virtually anything. Register the output,t return code or error message.

name: Install db only if /db/data/{{db_name}} not there
shell:
cmd:  "{{software_share_dir}}/install"
creates: "/db/data/{{db_name}}"
name: Install db and register output
shell:
cmd: "{{software_share_dir}}/install"
creates: "/db/data/{{db_name}}
register: dbinstall
name: Execute uptime in csh specific
shell: uptime
args:
executable: /bin/csh
name: Execute uptime in csh specific
shell: echo $youtube_channel
environment:
youtube_channel: CodingNinjasIndia
youtube show: automation_show

Here we have four examples. In the first example, we install the db as an example. We install a database with the command install, and this will create the directory "db/data/{{db name}}". When this directory already exists, it will not execute the task.

In the following example, we are also registering the output. So we are going to install the database again using the install command, and the output will be in the db install variable. With the debug command, we can print out what comes out. And then, we can see the standard error, standard out, return code, what was the exact command, what arguments were given, Etc.

The third example is where we can also say which shell to use when executing the code. So in this example, the c shell is given. The last example shows how to add environment variables to the remotely executing script.

Frequently Asked Questions

How do we get a list of Ansible modules?

The ansible-doc command is a handy feature of Ansible. This command will provide a list of the system's installed modules.

How do we find an Ansible module code?

We can look for it in the lib/ansible/module_utils directory under the primary Ansible path. This is where we can find the module utility source code.

How to debug an ansible module?

Using epdb is the most straightforward approach to running a debugger in a module, whether it be local or remote. To use this, add the code "import epdb; epdb. serve()" to the module's control node at the desired breakpoint. Run epdb to establish an association with the debugger.

Conclusion

In the article, we read about Ansible Modules. These were the ansible modules that we use commonly. We also read about the types of Ansible modules and their uses. There are always more choices of ansible modules and more things to learn. Read articles on Ansible and Devops here. Find stuff on cloudAzure and AWS. Explore Coding Ninjas Studio to find more exciting stuff. Happy Coding!

Thank you

Live masterclass