Tasks and Modules
Tasks are the individual steps in a playbook. Each task calls an Ansible module – a reusable unit of work that performs a specific function, like installing a package or copying a file.
Common Modules
Here are some essential modules you’ll use frequently:
- command / shell: Runs arbitrary commands (avoid shell if possible).
- copy: Copies files from control node to managed nodes.
- file: Manages file attributes, directories, symlinks.
- apt (Debian/Ubuntu) / yum (RHEL): Package management.
- service / systemd: Start, stop, restart services.
- user: Manage user accounts.
- git: Clone or update Git repositories.
Example: Install Nginx and Start Service
A playbook with multiple tasks:
---
- name: Setup web server
hosts: webservers
become: yes
tasks:
- name: Install nginx
apt:
name: nginx
state: present
update_cache: yes
- name: Start nginx service
service:
name: nginx
state: started
enabled: yesbecome: yes elevates privileges (sudo) for all tasks in the play.Task Results: changed vs ok
When you run a playbook, each task returns a status:
- ok: No changes needed (already in desired state).
- changed: The module made a change.
- failed: Error – play stops.
Using Command Module (with caution)
The
command module is not idempotent. It always runs the command. Use it only when no dedicated module exists.- name: Run a one-off command
command: echo "hello"Getting Help on Modules
View module documentation:
ansible-doc apt
ansible-doc -l # list all modulesTwo Minute Drill
- Tasks are units of work; each uses a module.
- Common modules:
apt,copy,service,file,user. - Use
become: yesfor tasks requiring sudo. - Check module docs with
ansible-doc module.
Need more clarification?
Drop us an email at career@quipoinfotech.com
