Skip to main content

Command Palette

Search for a command to run...

Ansible — A Complete Beginner Guide

Updated
5 min read
Ansible  — A Complete Beginner Guide
A

🚀 𝐀𝐛𝐨𝐮𝐭 𝐌𝐞 "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.

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?ArchitectureInstallationInventoryAd-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

FieldMeaningExample
ansible_hostIP or hostname used to connectansible_host=3.145.29.59
ansible_userRemote user to login asansible_user=ubuntu
ansible_ssh_private_key_filePath to private key on control node/home/ubuntu/.ssh/ansible_key
ansible_portSSH port if not 22ansible_port=2222
group namesLogical grouping of hosts[webservers], [db]

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

TaskCommand exampleNotes
Memory usageansible all -a "free -h" -i hostsQuick check across hosts
Disk usageansible all -a "df -h" -i hostsUseful before installs
Uptimeansible all -a "uptime" -i hostsSee CPU/load averages
Install packageansible server1 -b -a "apt-get install nginx -y" -i hostsUse -b to sudo
Service stateansible all -m service -a "name=nginx state=started" -b -i hostsManage 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

FactorModule (e.g., apt)Ad-hoc shell (-a)
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 usePlaybooks & automationQuick 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)

ModulePurposeShort example
aptManage apt packages (Debian/Ubuntu)-m apt -a "name=nginx state=present"
yumManage yum packages (RHEL/CentOS)-m yum -a "name=httpd state=present"
serviceStart/stop/restart services-m service -a "name=nginx state=started"
copyCopy files to remote hosts-m copy -a "src=local dest=/tmp/"
templateJinja2 templates rendering-m template -a "src=site.j2 dest=/etc/nginx/conf.d/site.conf"
fileManage file attributes & permissions-m file -a "path=/tmp/foo state=touch mode=0644"
commandRun commands (no shell)-m command -a "/usr/bin/uptime"
shellRun shell commands (with shell)-m shell -a "echo $HOME"

Notes & Best Practices

  • Always use -b when you need root privileges.

  • Prefer modules over raw shell commands.

  • Keep inventory organized with groups.

  • Test changes on a staging group before production.

More from this blog

DevOpsAlimasiuzama

30 posts