Understanding Ansible Modules

 As I continued learning Ansible, one of the most important concepts I came across was Modules.

They are the core of how Ansible performs automation. Once I understood modules clearly, writing playbooks and running ad-hoc commands became much easier and more meaningful.

In this blog, I’m sharing what Ansible modules are and how I understood them through hands-on practice.


What Are Ansible Modules? (In My Own Words)

Modules are the actual actions Ansible performs on a server.
If I want to:

  • install a package

  • create a file

  • add a user

  • start a service

  • copy a configuration

Ansible uses a module to do it.

So for me:

“Modules are the building blocks of every automation task in Ansible.”

Every ad-hoc command and every task inside a playbook uses a module.


How I Understand the Working of Modules

Whenever I run Ansible:

  1. Ansible connects to the server through SSH

  2. The selected module runs on the remote machine

  3. It performs the action I defined

  4. It returns the output back to Ansible in JSON

  5. Ansible shows the task result (changed, ok, failed, skipped)

I don’t need to install anything on the target server — modules run automatically when executed.


Types of Modules I Found Most Useful

1. Package Management Modules

Used for installing or removing packages.

  • apt → Ubuntu/Debian

  • yum → CentOS/RHEL

Example:

- name: Install nginx apt: name: nginx state: present

2. Service Modules

Used for managing services.

service: name: nginx state: started enabled: true

3. File & Directory Modules

These helped me with permissions, copying files, and modifying config files.

copy

copy: src: index.html dest: /var/www/html/index.html

file

file: path: /tmp/demo.txt mode: '0644' state: touch

lineinfile

lineinfile: path: /etc/sysconfig line: "ENV=production"

4. User Module

For managing users:

user: name: devuser state: present

5. Command & Shell Modules

When I needed to run basic Linux commands:

command

command: uptime

shell

shell: "df -h | grep /dev"

(Shell supports pipes, redirects, etc.)


How I Used Modules in Ad-Hoc Commands

These commands helped me learn modules quickly without writing a playbook.

Ping

ansible all -m ping

Install a package

ansible web -m apt -a "name=nginx state=present"

Copy a file

ansible web -m copy -a "src=demo.txt dest=/tmp/demo.txt"

Modules made it easy to run small tasks instantly.


How I Used Modules in Playbooks

Every task inside a playbook uses a module.

Example playbook:

- name: Setup Nginx Web Server hosts: web become: yes tasks: - name: Install nginx apt: name: nginx state: present - name: Start nginx service: name: nginx state: started - name: Deploy homepage copy: src: index.html dest: /var/www/html/index.html

Here I used three modules: apt, service, and copy.

This made automation smooth and predictable.


Why Modules Are Important (My Realisation)

After working with multiple modules, one thing became very clear:

Modules are the heart of Ansible.
Without modules, there is no automation.

They define what Ansible does, how it does it, and how consistent the results are.


Final Thoughts

Understanding modules helped me a lot in building confidence with Ansible. Once the concept clicked, writing tasks and playbooks became much easier and more enjoyable.

Next, I’ll write about:

👉 Ansible Installation & EC2 Setup
(where I installed Ansible and connected it to my EC2 instances)

Comments

Popular posts from this blog

Ansible Playbooks – How I Learned to Automate Complete Tasks

“What is Ansible? – My Understanding as a DevOps Learner”