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 file

Playbook: 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 time
  • uptime — shows how long the server has been running

▶️ Run the Playbook

ansible-playbook show_detail.yml

You’ll see the output showing both date and uptime.
 If you want to see the detailed process:

ansible-playbook show_detail.yml -v

The -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 sudo
  tasks:
- 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:

  • apt module handles package management (install, update, remove).
  • service module ensures NGINX is running and enabled on startup.

▶️ Run the Playbook

ansible-playbook install_nginx.yml

Once 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: yes
  tasks:
- 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.yml

To verify Docker installation:

ansible -a "docker --version" server2

You 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: yes
  tasks:
- 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


Comments

Popular posts from this blog

📘 Understanding Prometheus in a Simple Way-Part 3 (For DevOps Beginners)

Grafana Setup & Dashboard Creation (Part-5)— Explained by Raees Yaqoob Qazi

My First Python Program: A Simple Calculator