Docs / Cloud & DevOps / Getting Started with Ansible for Server Automation

Getting Started with Ansible for Server Automation

By Admin · Feb 25, 2026 · Updated Apr 25, 2026 · 30 views · 1 min read

What is Ansible?

Ansible is an agentless automation tool that configures servers over SSH. You write "playbooks" in YAML that describe the desired state of your infrastructure, and Ansible makes it happen.

Installation

sudo apt update
sudo apt install -y ansible

Inventory File

Create /etc/ansible/hosts or a local inventory.ini:

[webservers]
web1 ansible_host=198.51.100.10
web2 ansible_host=198.51.100.11

[databases]
db1 ansible_host=198.51.100.20

[all:vars]
ansible_user=root
ansible_ssh_private_key_file=~/.ssh/id_ed25519

Your First Playbook

Create setup-webserver.yml:

---
- name: Configure web servers
  hosts: webservers
  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes
        cache_valid_time: 3600

    - name: Install Nginx
      apt:
        name: nginx
        state: present

    - name: Enable and start Nginx
      systemd:
        name: nginx
        enabled: yes
        state: started

    - name: Copy site config
      template:
        src: templates/nginx-site.conf.j2
        dest: /etc/nginx/sites-available/default
      notify: Reload Nginx

  handlers:
    - name: Reload Nginx
      systemd:
        name: nginx
        state: reloaded

Run the Playbook

ansible-playbook -i inventory.ini setup-webserver.yml

Key Concepts

  • Idempotent — running a playbook twice produces the same result
  • Handlers — triggered only when a task reports a change
  • Templates — Jinja2 files with variables for dynamic configuration
  • Roles — reusable collections of tasks, templates, and variables

Was this article helpful?