Back to Blog
Tutorial

Ansible Complete Tutorial 2026 - Configuration Management

Firoz Ahmad
Jan 2026
10 min read

Ansible Complete Tutorial 2026: Configuration Management

Imagine you need to install Nginx, create users, copy configuration files, and start services on 200 servers. Doing this manually would take days and be riddled with inconsistencies. Ansible does it in minutes — and it is idempotent, meaning you can run it 10 times and the result is always the same, no duplicate entries or errors.

Ansible's killer feature: it is agentless. Unlike Puppet or Chef, Ansible requires no software on the managed servers — it uses SSH, which is already running everywhere. This makes it far easier to set up and maintain.

Installing Ansible

# On Ubuntu/Debian (control node - your machine)
sudo apt update
sudo apt install -y ansible

# Verify installation
ansible --version

# Or install via pip for the latest version
pip3 install ansible

Inventory: Defining Your Servers

Create an inventory.ini file listing the servers you want to manage:

[webservers]
web1.example.com
web2.example.com
web3.example.com

[databases]
db1.example.com ansible_user=ubuntu
db2.example.com ansible_user=ubuntu

[production:children]
webservers
databases

[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=~/.ssh/id_rsa

Test connectivity:

ansible all -i inventory.ini -m ping
ansible webservers -i inventory.ini -m command -a "uptime"

Your First Playbook

A playbook is a YAML file that defines what tasks to run on which hosts. Create install-nginx.yml:

---
- name: Install and configure Nginx web servers
  hosts: webservers
  become: true    # run as sudo
  vars:
    nginx_port: 80
    app_user: www-data

  tasks:
    - name: Update apt cache
      apt:
        update_cache: yes
        cache_valid_time: 3600

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

    - name: Copy Nginx configuration
      template:
        src: templates/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
        owner: root
        group: root
        mode: '0644'
      notify: Restart Nginx

    - name: Create web root directory
      file:
        path: /var/www/html
        state: directory
        owner: "{{ app_user }}"
        mode: '0755'

    - name: Start and enable Nginx
      systemd:
        name: nginx
        state: started
        enabled: yes

  handlers:
    - name: Restart Nginx
      systemd:
        name: nginx
        state: restarted

Run the playbook:

# Dry run (check mode - no changes made)
ansible-playbook -i inventory.ini install-nginx.yml --check

# Run the playbook
ansible-playbook -i inventory.ini install-nginx.yml

# Run with verbose output
ansible-playbook -i inventory.ini install-nginx.yml -v

Ansible Roles: Organising Large Playbooks

As playbooks grow, roles keep things organised. A role is a directory structure with a standard layout:

roles/
  nginx/
    tasks/
      main.yml         # main task list
    handlers/
      main.yml         # handlers (restart, reload)
    templates/
      nginx.conf.j2    # Jinja2 templates
    files/
      index.html       # static files
    vars/
      main.yml         # role variables
    defaults/
      main.yml         # default values (overridable)
    meta/
      main.yml         # role metadata, dependencies

Create a playbook that uses roles:

---
- name: Configure production web servers
  hosts: webservers
  become: true
  roles:
    - common        # basic setup, security hardening
    - nginx         # web server
    - nodejs        # application runtime
    - monitoring    # node_exporter for Prometheus

Ansible Vault: Managing Secrets

Never store passwords or API keys in plain text. Use Ansible Vault to encrypt sensitive data:

# Create an encrypted variables file
ansible-vault create secrets.yml

# Edit an encrypted file
ansible-vault edit secrets.yml

# Encrypt an existing file
ansible-vault encrypt vars/database.yml

# Run playbook with vault password
ansible-playbook site.yml --ask-vault-pass

# Or use a vault password file (for CI/CD)
ansible-playbook site.yml --vault-password-file .vault_pass

Your secrets.yml might look like:

db_password: "super_secret_password_123"
api_key: "sk-abc123xyz789"
ssl_cert_passphrase: "cert_pass_456"

Useful Ansible Modules

Module Purpose Example
apt / yum Package management Install Docker, Nginx
copy / template File management Deploy config files
systemd / service Service management Start/stop/enable services
user / group User management Create app users
docker_container Manage Docker containers Deploy containerised apps
aws_ec2 / aws_s3 AWS resource management Provision cloud resources

Ansible is a practical, high-value skill for any DevOps engineer. Our training program includes real Ansible labs where you configure multi-tier application environments, automate server hardening, and integrate Ansible into CI/CD pipelines. Explore the curriculum →

Share this article:TwitterLinkedInFacebook

Ready to Start Your DevOps Career?

Join our comprehensive DevOps course and get job-ready in 56 days

Enroll Now - Limited Seats