- Back to the Cloud
- Posts
- 🧑💻 How to Write Your First Ansible Playbook — Step by Step
🧑💻 How to Write Your First Ansible Playbook — Step by Step

Welcome back to “Back to the cloud” — your no-BS guide to mastering DevOps, Cloud, and AI workflows.
In the last issue, we covered:
➡ 5 Practical Ansible Use Cases for DevOps Engineers.
If you missed it, catch up here.
🔥 Today’s Goal:
Write and run your first Ansible playbook — even if you’ve never automated anything before.
🧠 What is an Ansible Playbook?
A Playbook is a simple YAML file where you:
Define which machines to target.
Describe tasks you want to automate.
Execute them in order — repeatable and error-free.
No Python required. No fancy syntax.
💻 Step 1: Define Your Inventory
First, list your servers in a file called hosts.ini
:
[webservers]
192.168.1.10
192.168.1.11
This tells Ansible where to run your tasks.
📜 Step 2: Create Your Playbook File
Save this as install_apache.yml
:
---
- name: Install Apache on Web Servers
hosts: webservers
become: yes
tasks:
- name: Ensure Apache is installed
apt:
name: apache2
state: present
This playbook does exactly what it says:
➡ Ensures Apache is installed on all webservers
.
⚡ Step 3: Run the Playbook
Open your terminal and run:
ansible-playbook -i hosts.ini install_apache.yml
Ansible will:
SSH into each server.
Check if Apache is installed.
Install it if missing.
💡 What’s Happening Behind the Scenes?
Ansible connects via SSH.
No agents, no daemons.
It simply reads your YAML file and executes the defined tasks, reporting success or failure.
🧠 Bonus Tip: Idempotency
Ansible is designed to be idempotent —
Run the same playbook 10 times, and if nothing’s changed, nothing gets re-applied.
This ensures: ✅ Safe re-runs.
✅ Predictable infrastructure.
✅ Clean, auditable changes.
🎯 Next Week:
“Breaking Down Ansible Roles: Why and How to Use Them for Clean Code.”
If you want to avoid spaghetti-style playbooks, you don’t want to miss this one!
💬 Question for You:
What would be your first automation project using Ansible?
Reply and let me know — I might turn it into a future tutorial!