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. 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. 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. 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. 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. 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. 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.
1
---
2
# Simple ansible playbook command-playbook.yml
3
-
4
name: Play commands
5
hosts: localhost
6
tasks:
7
- name: Execute command 'date'
8
command: date
9
10
- name: Display resolv.conf contents
11
command: cat /etc/resolv.conf
12
13
- name: Display resolv.conf contents using chdir
14
command: cat resolv.conf chdir=/etc
15
16
- name: Create a directory
17
command: mkdir /home/demo-module/user1/folder
Copied!
free_formindicates that this module takes a free form command to run. Like cat resolv.conf or mkdir /folder in above example. Not all modules support input like this, like copy module.

script module

Runs a local script on one or more remote node(s) after transferring it.
1
---
2
#Sample Ansible script-playbook.yml
3
-
4
name: Play Scripts
5
hosts: centos
6
become: yes
7
tasks:
8
- name: Run a script on remote server
9
script: /home/user1/demo-module/script.sh
Copied!

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.
1
---
2
#Sample Ansible service-playbook1.yml
3
-
4
name: Start some Services in order
5
hosts: centos
6
become: yes
7
tasks:
8
- name: Start the database service
9
service: name=postgresql state=started
10
11
- name: Start the httpd service
12
service: name=httpd state=started
Copied!
also we can write above playbook it in a dictionary or map format like this:
1
---
2
#Sample Ansible service-playbook2.yml
3
-
4
name: Start some Services in order
5
hosts: centos
6
become: yes
7
tasks:
8
- name: Start the database service
9
service:
10
name: postgresql
11
state: started
12
13
- name: Start the httpd service
14
service:
15
name: httpd
16
state: started
Copied!
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.
1
---
2
#Sample Ansible Playbook-lineinfile.yml
3
-
4
name: Add DNS server
5
hosts: localhost
6
tasks:
7
- name: Add DNS server to resolv.conf
8
lineinfile:
9
path: /etc/resolv.conf
10
line: 'nameserver 8.8.8.8'
Copied!
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.
1
---
2
#Sample Ansible mail-playbook.yml
3
-
4
name: sending mail
5
hosts: localhost
6
tasks:
7
- name: sending mail to root
8
mail:
9
subject: 'System has been successfully configured'
10
delegate_to: localhost
11
12
- name: Sending an e-mail using Gmail SMTP servers
13
mail:
14
host: smtp.gmail.com
15
port: 587
16
17
password: mysecret
18
to: John Smith <[email protected]>
19
subject: Ansible-report
20
body: 'System has been successfully provisioned.'
21
delegate_to: localhost
22
23
- name: sendMail to a mail server with attachments
24
mail:
25
host: smtp.example.com
26
port: 465
27
28
31
attach: /etc/fstab /etc/hosts
32
subject: Ansible-report
33
body: 'System has been successfully provisioned.'
34
35
36
Copied!

yum module

Installs, upgrade, downgrades, removes, and lists packages and groups with the yum package manager.
1
---
2
#Sample Ansible yum-playbook.yml
3
-
4
name: Install package(s) using yum
5
hosts: centos
6
become: yes
7
tasks:
8
- name: Install the latest version of Apache
9
yum:
10
name: httpd
11
state: latest
12
13
- name: Install apache >= 2.4
14
yum:
15
name: httpd>=2.4
16
state: present
17
18
- name: Install a list of packages (suitable replacement for 2.11 loop deprecation warning)
19
yum:
20
name: Install apache and postgresql
21
- httpd
22
- postgresql
23
- postgresql-server
24
state: present
Copied!
Always check the ansible official documents for the latest changes.

firewall module

This module allows for addition or deletion of services and ports (either TCP or UDP) in either running or permanent firewalld rules.
1
---
2
#Sample Ansible Playbook-firewalld.yml
3
-
4
name: Set Firewall Configurations
5
hosts: centos
6
become: yes
7
tasks:
8
- firewalld:
9
service: https
10
permanent: true
11
state: enabled
12
13
- firewalld:
14
port: 8080/tcp
15
permanent: true
16
state: disabled
17
18
- firewalld:
19
port: 162-162/udp
20
permanent: true
21
state: disabled
22
23
- firewalld:
24
source: 192.168.100.0/24
25
zone: internal
26
state: enabled
Copied!
Run ansible-doc <module-name> to get more information about any module you would like, it also gives you some examples!

Custom Modules

Ansible modules are in fact python programs which are located on /usr/lib/pythonX.Y/dist-packages/ansible/modules. You can write down any custom program in python langiage and place it there and use it. Check ansible github web page for default modules (https://github.com/ansible/ansible/tree/devel/lib/ansible/modules) but that's more advanced topic.
that's all.
.
.
.
With the special thanks of mumshad mannambeth.
.
Last modified 2mo ago