Introduction to Ansible
Chapter objectives
- Understand what Ansible is
- Discover the fundamental concepts
- Compare Ansible to other tools
- Identify use cases
1 - What is Ansible?
Definition
Ansible is an open-source IT automation tool that allows you to:
- Configure servers
- Deploy applications
- Orchestrate workflows
- Manage infrastructure as code
Key characteristics
Why "Agentless"?
| With Agent | Without Agent (Ansible) |
|---|---|
| Agent to install on each server | Standard SSH connection |
| Agent maintenance | Nothing to maintain |
| Resource consumption | No overhead |
| Additional ports | SSH port only |
2 - Ansible architecture
Overview
Components
| Component | Description |
|---|---|
| Control Node | Machine where Ansible is installed |
| Managed Nodes | Servers managed by Ansible |
| Inventory | List of hosts to manage |
| Playbook | YAML file describing the tasks |
| Module | Unit of work (e.g. apt, copy, file) |
| Role | Organized collection of playbooks |
3 - Fundamental concepts
Idempotence
Idempotence guarantees that a task produces the same result, whether it is run 1 or 100 times.
Example:
# Cette tâche installe nginx SI il n'est pas déjà installé
- name: Installer nginx
apt:
name: nginx
state: present
- 1st run: nginx installed ✅
- 2nd run: "ok" (nothing to do)
Declarative vs Imperative
With Ansible, you describe the desired final state, not the steps to reach it.
4 - Ansible workflow
Execution flow
Task states
| State | Color | Meaning |
|---|---|---|
| ok | 🟢 Green | Task succeeded, no change |
| changed | 🟡 Yellow | Task succeeded, change made |
| failed | 🔴 Red | Task failed |
| skipped | 🔵 Cyan | Task skipped (condition not met) |
| unreachable | 🔴 Red | Host unreachable |
5 - Use cases
Configuration Management
- Package installation
- Service configuration
- User management
- File deployment
Application Deployment
Cloud Provisioning
Orchestration
6 - Comparison with other tools
Ansible vs others
| Criterion | Ansible | Puppet | Chef | Terraform |
|---|---|---|---|---|
| Type | Push | Pull | Pull | Push |
| Agent | No | Yes | Yes | No |
| Language | YAML | DSL | Ruby | HCL |
| Curve | Easy | Medium | Hard | Medium |
| Focus | Config | Config | Config | Infra |
When to use Ansible?
| Use case | Ansible | Alternative |
|---|---|---|
| Server config | ✅ Excellent | - |
| App deployment | ✅ Excellent | - |
| Cloud provisioning | ✅ Good | Terraform (better) |
| K8s orchestration | ⚠️ Possible | Helm, ArgoCD |
| CI/CD | ⚠️ Possible | Jenkins, GitLab CI |
7 - Ansible ecosystem
Red Hat products
| Product | Description | License |
|---|---|---|
| Ansible Core | Automation engine | Open Source |
| Ansible Galaxy | Community role hub | Free |
| AWX | Web interface | Open Source |
| Automation Platform | Enterprise solution | Commercial |
Popular modules
8 - First example
Structure of a playbook
---
# site.yml - Mon premier playbook
- name: Configuration serveurs web
hosts: webservers
become: yes
tasks:
- name: Mettre à jour le cache apt
apt:
update_cache: yes
- name: Installer nginx
apt:
name: nginx
state: present
- name: Démarrer nginx
service:
name: nginx
state: started
enabled: yes
- name: Copier la page d'accueil
copy:
src: index.html
dest: /var/www/html/index.html
Execution
# Exécuter le playbook
ansible-playbook -i inventory.ini site.yml
# Résultat
PLAY [Configuration serveurs web] ****
TASK [Mettre à jour le cache apt] ****
ok: [web-01]
ok: [web-02]
TASK [Installer nginx] ****
changed: [web-01]
changed: [web-02]
TASK [Démarrer nginx] ****
changed: [web-01]
changed: [web-02]
PLAY RECAP ****
web-01 : ok=4 changed=2 failed=0
web-02 : ok=4 changed=2 failed=0
Summary
Key points
- Ansible is agentless: it uses SSH
- Playbooks are in YAML: readable by everyone
- Idempotence guarantees safe executions
- Push-based: you control when to run
Practical exercises
- Explain the difference between agentless and agent-based
- What is idempotence and why is it important?
- List 3 use cases of Ansible in your context
- Compare Ansible and Terraform: when to use each?