Ansible — A Complete Beginner Guide

🚀 𝐀𝐛𝐨𝐮𝐭 𝐌𝐞 "Hi, I'm Ali masiu zama, a DevOps student passionate about cloud computing and automation. I'm currently learning AWS, Linux, Docker, and CI/CD pipelines, with a focus on automating workflows and building scalable solutions !
Table of contents
Show more
Ansible is one of the most powerful automation tools every DevOps engineer should know. Whether you want to install software on 100+ servers, configure systems, manage deployments, or automate daily tasks — Ansible makes everything super fast and simple.
You now will understand:
What is Ansible
Why use Ansible
Control node & managed nodes
Inventory file and common fields
Ad-hoc commands and examples
Modules and when to use them
What is Ansible? → Why to use it? → Architecture → Installation → Inventory → Ad-Hoc Commands, and finally hands-on examples.
What is Ansible?
Ansible is an open-source automation tool used for:
Installing packages
Managing configurations
Running commands on multiple servers
Deploying applications
Orchestrating DevOps pipelines
Think of Ansible as a remote control for your servers. You write instructions → Ansible executes them on all your machines.
Why Ansible?
Here’s why it’s loved in DevOps:
No agent required (uses SSH)
Easy to learn (human-readable YAML)
Works everywhere (cloud, containers, on-prem)
Scales from small to very large
Free & open-source
Ansible Architecture (Simple Explanation)

Ansible has only two components:
Control Node (Master) — The machine where you install Ansible and run commands/playbooks.
Managed Nodes (Clients) — The servers Ansible controls (via SSH).
Flow: You → Ansible CLI → SSH → Target Server → Action executed
Ansible Setup on Ubuntu (Control Node)
1. nstall Ansible
sudo add-apt-repository ppa:ansible/ansible
sudo apt update -y
sudo apt install ansible -y
2. Setup SSH Key
cd ~/.ssh
vim ansible_key
chmod 700 ~/.ssh
chmod 600 ~/.ssh/ansible_key
Add the same public key to your server’s authorized_keys.
3. Test SSH connection
ssh -i ~/.ssh/ansible_key ubuntu@<server-ip>
# Fix errore if not connect servers
export ANSIBLE_HOST_KEY_CHECKING=False
Inventory File (Very Important)

Default inventory location:
/etc/ansible/hosts
Example hosts file:
[servers]
server1 ansible_host=3.145.29.59
server2 ansible_host=3.150.114.25
[all:vars]
ansible_user=ubuntu
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/.ssh/ansible_key
Inventory fields quick reference
Field | Meaning | Example |
| IP or hostname used to connect |
|
| Remote user to login as |
|
| Path to private key on control node |
|
| SSH port if not 22 |
|
group names | Logical grouping of hosts |
|
Verify inventory
ansible-inventory --list -i hosts
Test Connection with Ping Module
ansible all -m ping -i hosts
If you see pong, your setup is perfect. 🎉
Ansible Ad-Hoc Commands (Cheat Sheet)
Ad-hoc commands let you run quick commands without playbooks.
Common ad-hoc tasks table
Task | Command example | Notes |
Memory usage |
| Quick check across hosts |
Disk usage |
| Useful before installs |
Uptime |
| See CPU/load averages |
Install package |
| Use |
Service state |
| Manage services with module |
Install Nginx (Examples)
ansible server1 -b -a "apt-get update"
ansible server1 -b -a "apt-get install nginx -y"
ansible server1 -b -a "systemctl stop nginx" -i hosts
ansible server1 -b -a "apt-get remove nginx -y" -i hosts
Install Docker (Examples)
ansible server1 -b -a "apt-get update"
ansible server1 -b -a "apt-get install docker.io -y"
ansible server1 -b -a "systemctl status docker" -i hosts
ansible server1 -b -a "apt-get remove docker.io -y"
Package Management (Module vs Shell)
Using module (recommended):
ansible all -m apt -a "name=curl,git state=present update_cache=yes" -b
Ad-hoc shell (less ideal):
ansible all -a "apt-get install -y curl git" -b
Quick comparison table: Module vs Ad-hoc shell
Factor | Module (e.g., | Ad-hoc shell ( |
Idempotence | ✅ Ensures desired state | ❌ May not be idempotent |
Readability | ✅ Structured arguments | ⚠️ Free-form commands |
Error handling | ✅ Better (Ansible knows failure) | ⚠️ Harder to parse failures |
Best use | Playbooks & automation | Quick one-off tasks |
Service Management (Module examples)
ansible all -m ansible.builtin.service -a "name=nginx state=started" -b
ansible all -m ansible.builtin.service -a "name=nginx state=restarted" -b
ansible all -a "systemctl is-active nginx" -b
File & Log Commands
ansible all -a "du -sh /home/*" -b -i hosts
ansible all -a "tail -n 50 /var/log/syslog" -b
Common Ansible Modules (Handy Table)
Module | Purpose | Short example |
| Manage apt packages (Debian/Ubuntu) |
|
| Manage yum packages (RHEL/CentOS) |
|
| Start/stop/restart services |
|
| Copy files to remote hosts |
|
| Jinja2 templates rendering |
|
| Manage file attributes & permissions |
|
| Run commands (no shell) |
|
| Run shell commands (with shell) |
|
Notes & Best Practices
Always use
-bwhen you need root privileges.Prefer modules over raw shell commands.
Keep inventory organized with groups.
Test changes on a staging group before production.




