Ansible Playbooks Explained | Automate NGINX & Docker Setup Easily
Raees Qazi | DevOps Engineer | Learner | Mentor | Creator
Today, I’m going to explain what Ansible Playbooks are and how they work — in the simplest possible way.
🧠 What is a Playbook?
A Playbook is a YAML (.yml) file that contains a list of tasks you want to run on your target machines (servers).
These tasks are executed sequentially, one after another, in the order they’re written.
Playbooks make automation easy — instead of manually running commands on each server, you just write them once in a playbook and let Ansible handle everything.
⚙️ Before You Start
Before moving to practical examples, let’s assume you already have:
- One Ansible master/control node (your main machine)
- Two managed servers connected to your master node
Now let’s get hands-on!
🧩 1️⃣ First Playbook — Show Date & Uptime
We’ll start with a simple playbook that shows the current date and server uptime.
mkdir playbooks # Create a directory for playbooks
cd playbooks # Enter the directory
vim show_detail.yml # Create a playbook filePlaybook: show_detail.yml
---
- name: Show date and uptime
hosts: server
tasks:
- name: Show current date
command: date- name: Show server uptime
command: uptime
Save and exit (:wq).
🧾 Explanation:
This playbook connects to the host (named server) and runs two commands:
date— shows the current date and timeuptime— shows how long the server has been running
▶️ Run the Playbook
ansible-playbook show_detail.ymlYou’ll see the output showing both date and uptime.
If you want to see the detailed process:
ansible-playbook show_detail.yml -vThe -v flag shows what’s happening behind the scenes.
🌐 2️⃣ Second Playbook — Install and Enable NGINX
Now, let’s create a playbook to install and start the NGINX web server on another machine.
Playbook: install_nginx.yml
---
- name: Install and enable NGINX
hosts: server2
become: yes # Run tasks as sudotasks:
- name: Update apt cache
apt:
update_cache: yes
- name: Install latest version of NGINX
apt:
name: nginx
state: latest
- name: Ensure NGINX is running and enabled at boot
service:
name: nginx
state: started
enabled: yes
🧾 Explanation:
aptmodule handles package management (install, update, remove).servicemodule ensures NGINX is running and enabled on startup.
▶️ Run the Playbook
ansible-playbook install_nginx.ymlOnce it finishes, open your browser and paste the public IP address of server2.
If everything went well, you’ll see the default NGINX welcome page 🎉
🐳 3️⃣ Third Playbook — Install Docker
Now let’s write a playbook to install Docker on server2.
Playbook: install_docker.yml
---
- name: Install Docker and enable it
hosts: server2
become: yestasks:
- name: Install Docker
apt:
name: docker.io
state: latest
- name: Start and enable Docker service
service:
name: docker
state: started
enabled: yes
🧾 Explanation:
This playbook installs Docker, starts the service, and makes sure it starts automatically after a reboot.
▶️ Run the Playbook
ansible-playbook install_docker.ymlTo verify Docker installation:
ansible -a "docker --version" server2You should see the installed Docker version.
⚙️ Bonus — Adding Conditions in Playbooks
You can also add conditions to make playbooks smarter.
For example, only install Docker if the system is Debian or Ubuntu.
Playbook with condition:
---
- name: Install Docker (Debian/Ubuntu only)
hosts: server2
become: yestasks:
- name: Install Docker
apt:
name: docker.io
state: latest
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'
- name: Start and enable Docker
service:
name: docker
state: started
enabled: yes
when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'
🧪 Useful Ansible Commands
CommandDescriptionansible-playbook --syntax-check install_docker.ymlChecks if the playbook syntax is correctansible-playbook install_docker.yml -CRuns a “dry run” — shows what will happen without making actual changes
🏁 Conclusion
In this blog, we covered:
- What Ansible Playbooks are
- How to create and run playbooks
- Installing NGINX and Docker automatically
- Adding conditions and verifying syntax
I hope this blog helps you understand Ansible Playbooks more clearly.
If you found it useful, please share, comment, and subscribe for more DevOps content! 💻🚀
🌐 Online References
- LinkedIn: https://www.linkedin.com/in/raees-yaqoob-qazi-ryqs/
- Medium Blog: https://medium.com/@raeesyaqubqazi
- Blogspot: https://brillertechnologies.blogspot.com/
- Facebook Page: https://web.facebook.com/profile.php?id=61553548371216
- YouTube Channel: https://www.youtube.com/@RaeesQ.
- TikTok: https://www.tiktok.com/@mrryqs?_t=ZS-8y7t0fQfJKu&_r=1
Comments
Post a Comment