AUTOMATE
EVERYWHERE

// Configuration management without the complexity.

ANSIBLE CHANGED HOW WE THINK ABOUT SERVERS.

Imagine managing thousands of servers with a simple, human-readable language—no agents to install, no complex infrastructure. Ansible connects to your servers over SSH and ensures they're configured exactly as specified.

WHY ANSIBLE?

Ansible is agentless. You install it on one machine, and it manages all your servers via SSH. No daemon, no agent, no additional ports—just SSH and Python. Simple, powerful, and declarative.

BEGIN YOUR JOURNEY

// Your Training Path

Click a lesson to begin

LESSON 01

Introduction to Ansible

What is Ansible? Installing and your first commands.

Beginner
LESSON 02

Inventory & Ad-Hoc

Managing inventory and running ad-hoc commands.

Beginner
LESSON 03

Playbooks Basics

YAML syntax, tasks, and your first playbook.

Beginner
LESSON 04

Modules Deep Dive

file, copy, command, shell, service modules.

Intermediate
LESSON 05

Variables & Facts

Variables, facts, and registered variables.

Intermediate
LESSON 06

Conditionals & Loops

When conditions and loops in playbooks.

Intermediate
LESSON 07

Roles

Creating reusable roles and Ansible Galaxy.

Intermediate
LESSON 08

Templates

Jinja2 templating and variables in templates.

Intermediate
LESSON 09

Vault & Security

Encrypting data and security best practices.

Advanced
LESSON 10

Error Handling

Blocks, rescue, always, and debugging.

Advanced
LESSON 11

Testing

molecule, ansible-lint, and CI/CD integration.

Advanced
LESSON 12

Enterprise Patterns

AWX, collections, and production architectures.

Advanced

// Why Ansible

Ansible was created by Michael DeHaan in 2012 and later acquired by Red Hat. It has become one of the most popular automation tools due to its simplicity and agentless architecture.

Unlike Puppet or Chef, Ansible doesn't require agents on managed nodes. It uses SSH to connect to servers and push configurations, making it incredibly easy to get started.

Ansible uses YAML for playbooks, making them human-readable and easy to understand. This "batteries included" approach means you can start simple and scale up.

Automation for everyone. Simple, powerful, agentless.

// Tools & References

Ansible Docs

Official Documentation

docs.ansible.com

Ansible Galaxy

Community Roles

galaxy.ansible.com

AWX

Ansible Automation Platform

github.com/ansible/awx

Ansible Playground

Learn Ansible Online

ansible.school

// Lesson 01: Introduction to Ansible

×

What is Ansible?

Ansible is an open-source automation tool that handles configuration management, application deployment, and IT orchestration. It's designed to be simple, powerful, and agentless.

Key Principles

  • Agentless: No agents to install on managed nodes
  • Idempotent: Running the same playbook multiple times produces the same result
  • Declarative: You describe the desired state, Ansible makes it so
  • YAML-based: Playbooks are written in easy-to-read YAML

How Ansible Works

  1. Control Node: The machine where Ansible is installed
  2. Managed Nodes: The servers you want to manage
  3. Inventory: A list of managed nodes
  4. Modules: Units of work that Ansible executes
  5. Playbooks: YAML files defining automation tasks

Installing Ansible

# On Ubuntu/Debian
sudo apt update
sudo apt install ansible

# On RHEL/CentOS
sudo yum install ansible

# Check version
ansible --version

Quiz

1. What makes Ansible agentless?

Show Answers
  1. SSH

// Lesson 02: Inventory & Ad-Hoc Commands

×

Inventory

The inventory is a list of hosts that Ansible manages. It can be a simple file or a dynamic script.

Static Inventory File

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

[databases]
db1.example.com
db2.example.com

[all:vars]
ansible_user=ubuntu

Ad-Hoc Commands

Quick one-liner commands for testing:

# Ping all hosts
ansible all -m ping

# Check disk space
ansible all -a "df -h"

# Copy file to all webservers
ansible webservers -m copy -a "src=/tmp/file dest=/tmp/"

# Install package
ansible all -m apt -a "name=vim state=present"

Quiz

1. What module pings hosts?

Show Answers
  1. ping

// Lesson 03: Playbooks Basics

×

Your First Playbook

Playbooks are YAML files that define your automation tasks:

---
- name: My first playbook
  hosts: webservers
  become: yes
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present

    - name: Start nginx service
      service:
        name: nginx
        state: started
        enabled: yes

Running a Playbook

ansible-playbook site.yml

Key Concepts

  • --- YAML document start
  • name: Descriptive name for play/task
  • hosts: Target inventory hosts
  • become: Escalate privileges (sudo)
  • tasks: List of actions to perform

Quiz

1. What directive runs tasks with sudo?

Show Answers
  1. become

// Lesson 04: Modules Deep Dive

×

Common Modules

Ansible ships with thousands of modules. Here are the most common:

file Module

- name: Create a directory
  file:
    path: /tmp/mydir
    state: directory
    mode: '0755'

- name: Create a symlink
  file:
    src: /tmp/file
    dest: /tmp/link
    state: link

copy Module

- name: Copy file with permissions
  copy:
    src: /local/file.conf
    dest: /etc/app/file.conf
    owner: app
    group: app
    mode: '0644'

command & shell

- name: Run command
  command: /usr/bin/myapp --version

- name: Run shell command
  shell: cat /etc/os-release

Quiz

1. What module creates files, directories, and symlinks?

Show Answers
  1. file

// Lesson 05: Variables & Facts

×

Defining Variables

---
- name: Using variables
  hosts: all
  vars:
    app_port: 8080
    app_user: myapp
  tasks:
    - name: Create user
      user:
        name: "{{ app_user }}"

    - name: Configure app
      template:
        src: app.conf.j2
        dest: /etc/app/config.conf

Facts

Facts are system information collected by Ansible:

# Display all facts
ansible all -m setup

# Filter facts
ansible all -m setup -a "filter=ansible_*_mb"

# Access in playbook
- debug:
    msg: "{{ ansible_distribution }}"

Registered Variables

- name: Run command and register output
  command: /usr/bin/myapp status
  register: app_status

- name: Display output
  debug:
    msg: "{{ app_status.stdout }}"

Quiz

1. What stores command output for later use?

Show Answers
  1. register

// Lesson 06: Conditionals & Loops

×

Conditionals (when)

Run tasks conditionally based on variables or facts:

- name: Install Apache on Debian
  apt:
    name: apache2
    state: present
  when: ansible_os_family == "Debian"

- name: Install Apache on RedHat
  yum:
    name: httpd
    state: present
  when: ansible_os_family == "RedHat"

Loops (with_items)

- name: Create multiple users
  user:
    name: "{{ item }}"
    state: present
  with_items:
    - alice
    - bob
    - charlie

- name: Install multiple packages
  apt:
    name: "{{ item }}"
    state: present
  with_items:
    - vim
    - git
    - curl

Quiz

1. What keyword makes a task conditional?

Show Answers
  1. when

// Lesson 07: Roles

×

What are Roles?

Roles organize playbooks into reusable components with a standard directory structure:

Role Directory Structure

roles/
  common/
    defaults/
      main.yml
    handlers/
      main.yml
    tasks/
      main.yml
    templates/
    files/

Using a Role

---
- name: Deploy webserver
  hosts: webservers
  roles:
    - common
    - nginx
    - php

Role defaults/main.yml

---
nginx_port: 80
nginx_user: www-data
nginx_workers: 4

Quiz

1. Where are default role variables defined?

Show Answers
  1. defaults

// Lesson 08: Templates

×

Jinja2 Templates

Ansible uses Jinja2 for templating. Templates end in .j2:

Template Example

server {
    listen {{ nginx_port }};
    server_name {{ domain_name }};
    user {{ nginx_user }};
    worker_processes {{ nginx_workers }};

    {% if ssl_enabled %}
    ssl_certificate {{ ssl_cert_path }};
    ssl_certificate_key {{ ssl_key_path }};
    {% endif %}

    location / {
        root {{ document_root }};
        index index.html index.htm;
    }
}

Using Templates in Playbook

- name: Configure nginx
  template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf
  notify: Restart nginx

Template Filters

{{ my_variable | default('fallback') }}
{{ my_string | upper }}
{{ my_string | lower }}
{{ my_list | join(',') }}

Quiz

1. What file extension do Ansible templates use?

Show Answers
  1. .j2

// Lesson 09: Vault & Security

×

Ansible Vault

Ansible Vault encrypts sensitive data like passwords and keys:

Creating Encrypted Files

# Create new encrypted file
ansible-vault create secrets.yml

# Encrypt existing file
ansible-vault encrypt secrets.yml

# Decrypt file
ansible-vault decrypt secrets.yml

# Edit encrypted file
ansible-vault edit secrets.yml

# Change password
ansible-vault rekey secrets.yml

Using Vault in Playbooks

# Require vault password at runtime
ansible-playbook site.yml --ask-vault-pass

# Use vault password file
ansible-playbook site.yml --vault-password-file ~/.vault_pass

Best Practices

  • Never commit encrypted files to git
  • Use --ask-vault-pass for interactive runs
  • Use password files only in secure environments
  • Rotate vault passwords regularly

Quiz

1. What command creates an encrypted file?

Show Answers
  1. ansible-vault create

// Lesson 10: Error Handling

×

Blocks

Blocks group tasks and allow error handling:

- name: Handle errors
  block:
    - name: Try this
      debug:
        msg: "This might fail"

    - name: This too
      command: /usr/bin/failing-command
  rescue:
    - name: Handle failure
      debug:
        msg: "Something went wrong"
  always:
    - name: Always run
      debug:
        msg: "This always runs"

Ignoring Errors

- name: This might fail
  command: /usr/bin/risky-command
  ignore_errors: yes

Debug Module

- debug:
    msg: "Hello"

- debug:
    var: my_variable

- debug:
    msg: "{{ ansible_failed_task.name }} failed"

Quiz

1. What runs even if a block fails?

Show Answers
  1. always

// Lesson 11: Testing

×

molecule

Molecule tests Ansible roles in containers:

# Install molecule
pip install molecule

# Initialize new role with molecule
molecule init role my-role

# Run tests
cd my-role
molecule test

# Run different scenarios
molecule converge
molecule verify

ansible-lint

Lints playbooks for best practices:

# Install ansible-lint
pip install ansible-lint

# Lint a playbook
ansible-lint site.yml

# With custom rules
ansible-lint site.yml -R rules/

CI/CD Integration

# GitHub Actions example
- name: Test Ansible
  runs-on: ubuntu-latest
  steps:
    - uses: actions/checkout@v2
    - name: Lint
      run: ansible-lint site.yml
    - name: Test
      run: molecule test

Quiz

1. What tests Ansible roles in containers?

Show Answers
  1. molecule

// Lesson 12: Enterprise Patterns

×

AWX / Ansible Automation Platform

AWX provides a web UI and REST API for Ansible:

  • Web-based interface
  • Role-based access control
  • Job scheduling
  • Inventory management
  • Credential management

Collections

Collections organize Ansible content:

# Install collection
ansible-collection install community.postgresql

# Use in playbook
- name: Install PostgreSQL
  community.postgresql.postgresql_db:
    name: mydb
    state: present

Production Best Practices

  • Use version control for all playbooks
  • Test with molecule before production
  • Use roles for reusability
  • Separate dev/staging/production inventories
  • Use CI/CD for automation
  • Implement proper secret management

Quiz

1. What provides a web UI for Ansible?

Show Answers
  1. AWX