Skip to content

Ansible_introduction

Ansible is an open-source automation tool for configuration management, application deployment, and task automation. It uses SSH to execute tasks on remote servers.

┌─────────────────────────────────────────────────────────────────────────────┐
│ Ansible Overview │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌───────────────────────────────────────────────────────────────────┐ │
│ │ Ansible │ │
│ │ │ │
│ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │
│ │ │ Config │ │ App │ │ Task │ │ │
│ │ │ Management │ │ Deployment │ │ Automation │ │ │
│ │ └─────────────┘ └─────────────┘ └─────────────┘ │ │
│ │ │ │
│ │ ✓ Agentless ✓ Idempotent ✓ YAML-based │ │
│ │ │ │
│ └───────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│ Ansible Key Features │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ Agentless │
│ ┌───────────────────────────────────────────────────────────────────┐ │
│ │ │ │
│ │ Control Node Managed Nodes │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ Ansible │────────▶│ Server │ │ Server │ │ Server │ │ │
│ │ │ │ SSH │ 1 │ │ 2 │ │ 3 │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │
│ │ │ │
│ │ No agents needed on managed nodes │ │
│ └───────────────────────────────────────────────────────────────────┘ │
│ │
│ Idempotent │
│ ┌───────────────────────────────────────────────────────────────────┐ │
│ │ Running the same playbook multiple times produces same result │ │
│ └───────────────────────────────────────────────────────────────────┘ │
│ │
│ YAML-based │
│ ┌───────────────────────────────────────────────────────────────────┐ │
│ │ Playbooks written in easy-to-read YAML format │ │
│ └───────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│ Ansible Architecture │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ Control Node │ │
│ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ │
│ │ │ Ansible │ │ Inventory │ │ Playbooks │ │ │
│ │ │ CLI │ │ File │ │ (.yml) │ │ │
│ │ └─────────────┘ └─────────────┘ └─────────────┘ │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────────────────┐ │ │
│ │ │ Ansible Modules │ │ │
│ │ │ yum │ apt │ copy │ file │ service │ user │ git │ ... │ │ │
│ │ └──────────────────────────────────────────────────────────┘ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │ │
│ │ SSH │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ Managed Nodes │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ Server │ │ Server │ │ Server │ │ Server │ │ │
│ │ │ 1 │ │ 2 │ │ 3 │ │ 4 │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │
│ │ │ │
│ │ No agents required - just SSH and Python │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
  • Defines hosts and groups
  • Can be static file or dynamic
  • Defines automation tasks
  • Written in YAML
  • Contains plays
  • Unit of work
  • Executed on managed nodes
  • Thousands of built-in modules
  • Reusable collection of tasks
  • Can be shared via Ansible Galaxy
Terminal window
# macOS with Homebrew
brew install ansible
# Ubuntu/Debian
sudo apt update
sudo apt install ansible
# CentOS/RHEL
sudo yum install epel-release
sudo yum install ansible
# Verify
ansible --version
  • SSH access
  • Python 2.7+ or 3.5+
inventory.ini
[webservers]
web1.example.com
web2.example.com
web3.example.com
[databases]
db1.example.com
db2.example.com
[all:vars]
ansible_user=ubuntu
ansible_python_interpreter=/usr/bin/python3
playbook.yml
---
- name: Install and start nginx
hosts: webservers
become: yes
tasks:
- name: Install nginx
apt:
name: nginx
state: present
when: ansible_os_family == "Debian"
- name: Start nginx service
service:
name: nginx
state: started
enabled: yes
Terminal window
# Test connection to hosts
ansible all -m ping
# Run playbook
ansible-playbook -i inventory.ini playbook.yml
# Run with verbose output
ansible-playbook -i inventory.ini playbook.yml -v
┌─────────────────────────────────────────────────────────────────────────────┐
│ Ansible vs Puppet vs Chef │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ Feature Ansible Puppet Chef │
│ ────────────────────────────────────────────────────────────────────── │
│ Type Agentless Agent Agent │
│ Language YAML DSL (Ruby) DSL (Ruby) │
│ Learning Curve Low High High │
│ Idempotent Yes Yes Yes │
│ Config Format Push-based Pull-based Pull-based │
│ Master/Agent Not required Required Required │
│ Enterprise Ansible Tower Puppet Enterprise Chef Automate │
│ │
└─────────────────────────────────────────────────────────────────────────────┘

In this chapter, you learned:

  • What is Ansible and its key features
  • Ansible architecture
  • Components (Inventory, Playbooks, Modules, Roles)
  • Installing Ansible
  • Basic example