Ansible_introduction
Chapter 41: Ansible Introduction
Section titled “Chapter 41: 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.
What is Ansible?
Section titled “What is Ansible?”┌─────────────────────────────────────────────────────────────────────────────┐│ Ansible Overview │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌───────────────────────────────────────────────────────────────────┐ ││ │ Ansible │ ││ │ │ ││ │ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ ││ │ │ Config │ │ App │ │ Task │ │ ││ │ │ Management │ │ Deployment │ │ Automation │ │ ││ │ └─────────────┘ └─────────────┘ └─────────────┘ │ ││ │ │ ││ │ ✓ Agentless ✓ Idempotent ✓ YAML-based │ ││ │ │ ││ └───────────────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘Key Features
Section titled “Key Features”┌─────────────────────────────────────────────────────────────────────────────┐│ 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
Section titled “Ansible Architecture”┌─────────────────────────────────────────────────────────────────────────────┐│ 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 │ ││ └─────────────────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘Ansible Components
Section titled “Ansible Components”Inventory
Section titled “Inventory”- Defines hosts and groups
- Can be static file or dynamic
Playbook
Section titled “Playbook”- Defines automation tasks
- Written in YAML
- Contains plays
Module
Section titled “Module”- Unit of work
- Executed on managed nodes
- Thousands of built-in modules
- Reusable collection of tasks
- Can be shared via Ansible Galaxy
Installing Ansible
Section titled “Installing Ansible”Control Node
Section titled “Control Node”# macOS with Homebrewbrew install ansible
# Ubuntu/Debiansudo apt updatesudo apt install ansible
# CentOS/RHELsudo yum install epel-releasesudo yum install ansible
# Verifyansible --versionManaged Nodes Requirements
Section titled “Managed Nodes Requirements”- SSH access
- Python 2.7+ or 3.5+
Basic Example
Section titled “Basic Example”Inventory File
Section titled “Inventory File”[webservers]web1.example.comweb2.example.comweb3.example.com
[databases]db1.example.comdb2.example.com
[all:vars]ansible_user=ubuntuansible_python_interpreter=/usr/bin/python3Simple Playbook
Section titled “Simple Playbook”---- 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: yesRunning Ansible
Section titled “Running Ansible”# Test connection to hostsansible all -m ping
# Run playbookansible-playbook -i inventory.ini playbook.yml
# Run with verbose outputansible-playbook -i inventory.ini playbook.yml -vAnsible vs Other Tools
Section titled “Ansible vs Other Tools”┌─────────────────────────────────────────────────────────────────────────────┐│ 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 ││ │└─────────────────────────────────────────────────────────────────────────────┘Summary
Section titled “Summary”In this chapter, you learned:
- What is Ansible and its key features
- Ansible architecture
- Components (Inventory, Playbooks, Modules, Roles)
- Installing Ansible
- Basic example