Ten Useful Ansible Modules

Tosin Akinosho
4 min readDec 27, 2017

--

Here are some ansible modules that I have found useful. Some of these modules may not be as popular as well. But for clean playbooks I believe they are worth the look.

The File Module

File module sets attributes of files, symlinks, and directories, it can also remove file, symlinks or directories.

Examples

- file:
path: /etc/example.conf
owner: exampleowner
group: examplegroup
mode: 0755
- file:
src: /file/to/link/to
dest: /path/to/symlink
owner: exampleowner
group: examplegroup
state: link

The SELinux Module

This module will allow you to configure the SELinux module on linux.

Examples

# Enable SELinux
- selinux:
policy: targeted
state: enforcing

# Put SELinux in permissive mode, logging actions that would be blocked.
- selinux:
policy: targeted
state: permissive

# Disable SELinux
- selinux:
state: disabled

The SEBoolean Module

SEBoolean Module toggles selinux booleans in your environment. This is helpful do to the fact that you just have to use selinux boolean name and modify the state or persistents in the playbook.

Example

- seboolean:
name: allow_httpd_anon_write
state: yes
persistent: yes

Listing Booleans

The RAW Module

If you are new to ansible raw module is for you it creates a low-down dirty command via ssh. This is useful and should be done in the following cases.

  1. installing python-simplejson on older Python 2.4 and before since there are many dependencies that are needed most core modules
  2. Connecting to devices that do not have python installed.

Example:

- name: Bootstrap a legacy python 2.4 host
raw: yum -y install python-simplejson

- name: Run a command that uses non-posix shell-isms (in this example /bin/sh doesn't handle redirection and wildcards together but bash does)
raw: cat < /tmp/*txt
args:
executable: /bin/bash

The Mount Module

This module controls mount points pointed to /etc/fstab. The is useful do to the fact that you can specify the type, path state and other property's right from Ansible.

Example

- name: Mount DVD read-only
mount:
path: /mnt/dvd
src: /dev/sr0
fstype: iso9660
opts: ro
state: present

The User Module

The module manages users accounts with there attributes. This is handy do to the fact that the user properties and attributes can all be configured from this Ansible module.

Example

# Add the user 'exampleuser' with a specific uid and a primary group of 'admin'
- user:
name: exampleuser
comment: "Example User"
uid: 1040
group: admin
# Remove the user 'exampleuser'
- user:
name: exampleuser
state: absent
remove: yes

The Group Module

The module allows you to manage groups on a host. The modules have all the defined properties need to modify groups on a system.

Example

 # Example command for creating a group in this ansible module
- group:
name: testgroup
state: present

The Mail Module

This module is useful for sending emails. Whether you plan to use them for Error notifications or completion notification. It is a useful addition to the ansible module toolkit.

Example

# Example playbook sending mail to root
- mail:
subject: 'System {{ ansible_hostname }} has been successfully provisioned.'
delegate_to: localhost

# Sending an e-mail using Gmail SMTP servers
- mail:
host: smtp.gmail.com
port: 587
username: username@gmail.com
password: mysecret
to: Example Uesr<example.user@example.com>
subject: Ansible-report
body: 'System {{ ansible_hostname }} has been successfully provisioned.'
delegate_to: localhost

The Git Module

This module will allow you to manage git checkouts of repositories to deploy software. This is very useful if you organization is using CI/CD since ansible could provision the servers and then configure the servers with source code via Git.

Example

# Example git checkout from Ansible Playbooks
- git:
repo: 'https://testserver.example.org/path/to/repo.git'
dest: /srv/checkout
version: release-0.22

# Example read-write git checkout from github
- git:
repo: ssh://git@github.com/mylogin/hello.git
dest: /home/mylogin/hello

# Example just ensuring the repo checkout exists
- git:
repo: 'https://testserver.example.org/path/to/repo.git'
dest: /srv/checkout
update: no

The Script Module

This module takes the script name followed by space-delimited arguments. The script that runs will be copy locally to the remote system and ran automatically. This is a great feature because the other way to run a script on ansible is to use the copy module to machine with the correct permissions. Then call the script with the command or shell module. The script command takes care of all of that automatically. You can also set the return to the register and debug modules.

Example

- script: /some/local/sample_script.sh --some-arguments testvar
args:
creates: /the/created/sample.txt

Try linuxacademy.com they have a great course on Ansible called Using Ansible for Configuration Management and Deployments.

--

--

Tosin Akinosho

Associate Principal Solution Architect @RedHat. Cloud & DevOps Enthusiast. AI Integrator. Passionate about sharing knowledge and driving innovation.