Skip to main content
  1. Posts/

Infrastructure as Code: Ansible Playbooks for System Deployment

·235 words·2 mins
Automation Ansible Infrastructure Devops
Table of Contents

Why Infrastructure as Code Matters
#

Manual server configuration is time-consuming, error-prone, and impossible to scale. After rebuilding systems for the third time, I committed to automating everything with Ansible.

The Ansible Advantage
#

Declarative Configuration
#

Instead of writing complex shell scripts, Ansible lets you describe the desired state:

- name: Install security packages
  package:
    name:
      - fail2ban
      - ufw
      - aide
    state: present

- name: Configure firewall rules
  ufw:
    rule: allow
    port: "{{ item }}"
    proto: tcp
  loop:
    - 22
    - 80
    - 443

Idempotent Operations
#

Run the same playbook multiple times without side effects. Ansible checks current state and only makes necessary changes.

My Standard Server Playbook
#

Every new server gets:

  1. Security hardening - SSH keys, fail2ban, firewall
  2. Monitoring setup - Node exporter, log forwarding
  3. Backup configuration - Automated snapshots and offsite storage
  4. Application deployment - Docker containers with proper networking

Real-World Example
#

Here’s a snippet from my web server playbook:

- name: Deploy web application
  docker_container:
    name: webapp
    image: "myapp:{{ app_version }}"
    ports:
      - "8080:8080"
    env:
      DATABASE_URL: "{{ vault_db_url }}"
    restart_policy: unless-stopped
    networks:
      - name: webapp_network

Results
#

What used to take 2-3 hours of manual configuration now takes 5 minutes of automated deployment. More importantly, every server is configured identically, reducing debugging time and improving reliability.

The key is starting simple and iterating. Begin with basic package installation, then gradually add complexity as you learn Ansible’s capabilities.