Skip to main content

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 AgentWithout Agent (Ansible)
Agent to install on each serverStandard SSH connection
Agent maintenanceNothing to maintain
Resource consumptionNo overhead
Additional portsSSH port only

2 - Ansible architecture

Overview

Components

ComponentDescription
Control NodeMachine where Ansible is installed
Managed NodesServers managed by Ansible
InventoryList of hosts to manage
PlaybookYAML file describing the tasks
ModuleUnit of work (e.g. apt, copy, file)
RoleOrganized 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

StateColorMeaning
ok🟢 GreenTask succeeded, no change
changed🟡 YellowTask succeeded, change made
failed🔴 RedTask failed
skipped🔵 CyanTask skipped (condition not met)
unreachable🔴 RedHost 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

CriterionAnsiblePuppetChefTerraform
TypePushPullPullPush
AgentNoYesYesNo
LanguageYAMLDSLRubyHCL
CurveEasyMediumHardMedium
FocusConfigConfigConfigInfra

When to use Ansible?

Use caseAnsibleAlternative
Server config✅ Excellent-
App deployment✅ Excellent-
Cloud provisioning✅ GoodTerraform (better)
K8s orchestration⚠️ PossibleHelm, ArgoCD
CI/CD⚠️ PossibleJenkins, GitLab CI

7 - Ansible ecosystem

Red Hat products

ProductDescriptionLicense
Ansible CoreAutomation engineOpen Source
Ansible GalaxyCommunity role hubFree
AWXWeb interfaceOpen Source
Automation PlatformEnterprise solutionCommercial

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

  1. Explain the difference between agentless and agent-based
  2. What is idempotence and why is it important?
  3. List 3 use cases of Ansible in your context
  4. Compare Ansible and Terraform: when to use each?

← Table of contents | Installation →