SynfraCore
Synfracore
Start Learning
Navigation

Academies

Platform

RoadmapsLabsCertificationsInterviewPYQsAI AssistantCareer
Start Learning Free🗺️ Learning Roadmaps
Blog/DevOps

Your First Ansible Playbook: Automate Server Setup in 15 Minutes

SynfraCore·April 2025·10 min read

The Problem Ansible Solves

You have 10 new Ubuntu servers. Each needs nginx, a deploy user, firewall rules, and app config. Manually: 2 hours. With Ansible: 3 minutes, identically on all 10.

setup-webserver.yml

yaml
---
- name: Set up web servers
  hosts: webservers
  become: true
  vars:
    app_port: 8080
    deploy_user: deploy
  tasks:
    - name: Install nginx
      apt: { name: nginx, state: present, update_cache: yes }
    - name: Start nginx
      service: { name: nginx, state: started, enabled: yes }
    - name: Create deploy user
      user: { name: "{{ deploy_user }}", shell: /bin/bash, groups: sudo }
    - name: Allow ports
      ufw: { rule: allow, port: "{{ item }}", proto: tcp }
      loop: ["22", "80", "443", "{{ app_port }}"]
    - name: Copy nginx config
      template: { src: templates/nginx.conf.j2, dest: /etc/nginx/conf.d/myapp.conf }
      notify: Reload nginx
  handlers:
    - name: Reload nginx
      service: { name: nginx, state: reloaded }

Inventory File

ini
[webservers]
server1 ansible_host=192.168.1.10 ansible_user=ubuntu
server2 ansible_host=192.168.1.11 ansible_user=ubuntu
server3 ansible_host=192.168.1.12 ansible_user=ubuntu

Run It

bash
ansible-playbook -i inventory.ini setup-webserver.yml --check  # Dry run
ansible-playbook -i inventory.ini setup-webserver.yml           # Run

Key Concepts

Idempotency: Run 10 times, same result as once. nginx being 'present' is checked, not installed blindly.

Handlers: Only run when triggered by notify. Nginx only reloads if the config file actually changed.

Loops: loop runs the same task with different values — cleaner than copy-pasting 4 identical tasks.

Next: Ansible Intermediate covers roles, Vault (encrypted secrets), and dynamic cloud inventory.

Found this useful? Share it:

Twitter / X LinkedIn WhatsApp Telegram

Weekly DevOps & Cloud digest

Every Sunday — tutorials, interview questions, tips, and what changed in DevOps and Cloud this week.

Join our Community
Daily tips, job alerts, interview help — join engineers learning together
← All articlesStart Learning DevOps
Your First Ansible Playbook: Automate Server Setup in 15 Minutes — Blog | SynfraCore