Ansible Common Modules
A module is a ready-made tool that performs one job on a managed node. Ansible ships with thousands of them. Think of a workshop wall with labeled tools: a hammer for nails, a screwdriver for screws. Each module fits one purpose, and a playbook is a list of instructions that names the right tool for each step. A small set of about ten modules covers most daily work.
Diagram: Modules by Job
┌──────────────┐
│ Playbook │
└──────┬───────┘
┌──────────┬───────────┼───────────┬───────────┐
▼ ▼ ▼ ▼ ▼
┌─────────┐ ┌──────────┐ ┌─────────┐ ┌────────┐ ┌─────────┐
│ Software│ │ Files │ │ Services│ │ Users │ │ Commands│
├─────────┤ ├──────────┤ ├─────────┤ ├────────┤ ├─────────┤
│ package │ │ copy │ │ service │ │ user │ │ command │
│ apt │ │ file │ │ systemd │ │ group │ │ shell │
│ dnf │ │lineinfile│ │ │ │ │ │ script │
└─────────┘ └──────────┘ └─────────┘ └────────┘ └─────────┘
Software Modules
The ansible.builtin.package module installs software on any Linux family. Use apt for Debian and Ubuntu or dnf for Red Hat systems when you need distribution specific options.
- name: Install git and curl
ansible.builtin.package:
name:
- git
- curl
state: present
The state option accepts present, absent, and latest.
File Modules
The copy module sends a file from the control node to a managed node. The file module creates folders, sets permissions, and removes items. The lineinfile module edits a single line, and the blockinfile module manages a group of lines.
- name: Create an application folder
ansible.builtin.file:
path: /opt/shop
state: directory
owner: appuser
mode: '0755'
- name: Copy the welcome page
ansible.builtin.copy:
src: files/index.html
dest: /var/www/html/index.html
- name: Turn off root SSH login
ansible.builtin.lineinfile:
path: /etc/ssh/sshd_config
regexp: '^PermitRootLogin'
line: 'PermitRootLogin no'
Service Modules
The service module starts, stops, restarts, and enables services at boot. The systemd module adds options such as daemon_reload.
- name: Start nginx and enable it at boot
ansible.builtin.service:
name: nginx
state: started
enabled: true
User Modules
- name: Create a group
ansible.builtin.group:
name: developers
- name: Create a user in that group
ansible.builtin.user:
name: maya
groups: developers
shell: /bin/bash
state: present
Command Modules
Three modules run commands. The command module runs a program directly and ignores pipes and redirects. The shell module passes the line to a shell and supports pipes. The script module sends a local script to the node and runs it. Prefer a dedicated module whenever one exists, because dedicated modules report changes accurately.
- name: Check disk space
ansible.builtin.command: df -h /
register: disk_result
changed_when: false
Download and Archive Modules
- name: Download a release file
ansible.builtin.get_url:
url: https://example.com/app-1.4.tar.gz
dest: /tmp/app-1.4.tar.gz
- name: Unpack the release
ansible.builtin.unarchive:
src: /tmp/app-1.4.tar.gz
dest: /opt/shop
remote_src: true
Read Module Documentation
The command ansible-doc shows full documentation offline. Run ansible-doc ansible.builtin.copy to see every option, defaults, and sample tasks. Run ansible-doc -l to list every installed module.
Choose the Right Module
| Goal | Best Module | Avoid |
|---|---|---|
| Install software | package | shell with apt-get |
| Change one line in a file | lineinfile | shell with sed |
| Restart a service | service | command with systemctl |
| Create an account | user | shell with useradd |
Try This
Write a playbook that creates a user named trainee, creates the folder /opt/practice owned by that user, and copies a text file into it. Run the playbook twice. The second run reports zero changes, which proves each module works in an idempotent way.
Summary
Modules do the real work in every playbook. Software, file, service, user, command, and download modules cover most daily automation. Dedicated modules report changes correctly and keep runs repeatable, while command and shell serve cases without a specialist module. The ansible-doc tool provides offline documentation for each module.
