Ansible Modules

In order to develop more meaningful Playbooks, we need to know more about Ansible Modules.

What is Ansible Module?

Ansible works by connecting to your nodes and pushing out small programs, called modules to them. Modules are used to accomplish automation tasks in Ansible.

These programs are written to be resource models of the desired state of the system. Ansible then executes these modules and removes them when finished.

Ansible modules are categorized into various groups based on their functionality. There are hundreds of Ansible modules are available. We have categorized all the modules as shown in the below image:

  1. System : System modules are actions to be performed at a system level such as modifying the users and groups on the system, modifying iptables and firewall configurations, working with logical volume groups, mounting operations and working with services.

  2. Command : Command module are used to execute command or script on the host. This could be a simple command using the command module or an interactive execution using expect by responding to prompts. You could also run a script on the host using the script module.

  3. Files : Files module will help in working with files. For example, using an ACL module to set an acl information on files, use the archive and unarchive module to compress and unpack files, use find, line in file, and replace the module to modify the contents of an existing file.

  4. Database : Database module helps in working with databases such as mongodb, mysql, mssql, postgresql, proxysql and vertica to add or remove databases or modifying database configurations, etc.

  5. Cloud : The Cloud section has a vast collection of modules for different cloud providers like Amazon, Azure, Google, Docker, VMware, Digital Ocean, Openstack, and many more. There are number a of modules available of each of these that allow you to perform various tasks such as, creating and destroying instances, performing configuration changes, security, managing containers, clusters, and much more.

  6. Windows : Windows module helps you in the Windows environment. Some of them are, Win_copy to copy files, Win_command to execute a command, configuring a domain, configuring IIS, configuring registry, and lot more.

A module provides a defined interface, accepts arguments, and returns information to Ansible by printing a JSON string to stdout before exiting. Lets take a look at command module for example.

command module

Command Modules executes a command on a remote node, it is good to know that command module is a default module if no modules is specified.

Parameter

Comments

chdir

cd into this directory before running the command

creates

a filename or (since 2.0) glob pattern, when it already exists, this step will not be run

executable

change the shell used to execute the command. Should be an absolute path to the executable

free_form

the command module takes a free form command to run. There is no parameter actually named 'free form'. see the examples!

removes

a filename or (since 2.0) glob pattern, when it does not exist, this step will not be run.

warn (added in1.8)

if command warnings are on in ansible.cfg, do not warn about this particular line if set to no/false.

script module

Runs a local script on one or more remote node(s) after transferring it.

service module

Used to manage services on a system, Start, Stop, Restart. The Service module does not have a free_form input, which means we have to pass input in a key value pair format.

also we can write above playbook it in a dictionary or map format like this:

startedensures that httpd service is started, so if it is already started, do nothing. As we mentioned before, this is called idempotency.

Majority of the modules in Ansible are idempotent and Ansible highly recommends this. The overall idea is that you should be able to run the same playbook again and again and Ansible should report that everything is in an expected state.

lineinfile Module

Lineinfile module is used to find a line in a file and replace it or add it if it doesn't already exist.

What if we do the same thing using a script and run it multiple times?

mail module

This module is useful for sending emails from playbooks.

yum module

Installs, upgrade, downgrades, removes, and lists packages and groups with the yum package manager.

firewall module

This module allows for addition or deletion of services and ports (either TCP or UDP) in either running or permanent firewalld rules.

Run ansible-doc <module-name> to get more information about any module you would like, it also gives you some examples!

Custom Modules

that's all.

.

.

.

With the special thanks of mumshad mannambeth.

https://www.redhat.com/en/topics/automation/learning-ansible-tutorial

https://linuxbuz.com/linuxhowto/what-is-ansible-modules-and-how-to-use-it

https://docs.ansible.com/ansible/2.8/modules/command_module.html

https://docs.ansible.com/ansible/2.4/script_module.html

https://docs.ansible.com/ansible/2.5/modules/service_module.html

https://docs.ansible.com/ansible/2.5/modules/lineinfile_module.html

https://docs.ansible.com/ansible/2.3/mail_module.html

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_module.html

https://docs.ansible.com/ansible/latest/collections/ansible/posix/firewalld_module.html

.

Last updated

Was this helpful?