Skip to main content

Installation and configuration


Chapter objectives

  • Install Ansible on different systems
  • Configure the ansible.cfg file
  • Establish SSH connectivity
  • Validate the installation

1 - Prerequisites

Control Node (Ansible machine)

SystemSupport
Linux✅ Full
macOS✅ Full
Windows⚠️ Via WSL only

Managed Nodes (targets)

PrerequisiteDescription
Python 2.7+ or 3.5+To run the modules
SSH ServerRemote access
User with sudoFor privileged tasks

2 - Installation on Linux

Ubuntu/Debian

# Méthode 1: Via apt (version stable)
sudo apt update
sudo apt install ansible

# Méthode 2: Via PPA (dernière version)
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible

# Méthode 3: Via pip (recommandé)
sudo apt install python3-pip
pip3 install ansible

RHEL/CentOS/Rocky

# RHEL 8+
sudo dnf install ansible-core

# CentOS 7
sudo yum install epel-release
sudo yum install ansible

# Via pip
pip3 install ansible

Arch Linux

sudo pacman -S ansible

3 - Installation on macOS

# Via Homebrew
brew install ansible

# Via pip
pip3 install ansible

4 - Installation on Windows (WSL)

# 1. Installer WSL (PowerShell admin)
wsl --install

# 2. Redémarrer Windows

# 3. Dans WSL Ubuntu
sudo apt update
sudo apt install ansible

5 - Verifying the installation

# Version d'Ansible
ansible --version

# Sortie attendue
ansible [core 2.15.0]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/user/.ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
ansible collection location = /home/user/.ansible/collections
executable location = /usr/bin/ansible
python version = 3.10.12

# Lister les modules disponibles
ansible-doc -l | head -20

# Documentation d'un module
ansible-doc apt

6 - SSH configuration

Connection architecture

Generate an SSH key

# Générer une paire de clés
ssh-keygen -t ed25519 -C "ansible@control-node"

# Ou avec RSA (compatibilité)
ssh-keygen -t rsa -b 4096 -C "ansible@control-node"

# Accepter les valeurs par défaut (Enter)
# La clé sera dans ~/.ssh/id_ed25519

Distribute the public key

# Copier la clé vers les serveurs cibles
ssh-copy-id user@server1
ssh-copy-id user@server2
ssh-copy-id user@server3

# Ou manuellement
cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Test the connection

# Connexion sans mot de passe
ssh user@server1

# Test Ansible
ansible all -i "server1," -m ping

7 - Ansible configuration

Hierarchy of configuration files

Create ansible.cfg

# ansible.cfg - Configuration recommandée
[defaults]
# Inventaire par défaut
inventory = ./inventory/hosts.ini

# Désactiver la vérification des clés host (dev uniquement!)
host_key_checking = False

# Utilisateur distant par défaut
remote_user = ansible

# Nombre de connexions parallèles
forks = 10

# Timeout de connexion
timeout = 30

# Fichier de log
log_path = ./ansible.log

# Ne pas créer de fichiers .retry
retry_files_enabled = False

# Format de sortie
stdout_callback = yaml

# Collecter les facts au début
gathering = smart

[privilege_escalation]
# Devenir root par défaut
become = True
become_method = sudo
become_user = root
become_ask_pass = False

[ssh_connection]
# Optimisation SSH
pipelining = True
control_path = /tmp/ansible-%%h-%%p-%%r

Important options

OptionDescriptionRecommended value
inventoryPath to the inventory./inventory/
host_key_checkingVerify SSH keysFalse (dev), True (prod)
forksParallelism10-50 depending on network
pipeliningSSH optimizationTrue
becomePrivilege escalationTrue

Create the structure

# Créer le projet
mkdir -p ansible-project/{inventory,playbooks,roles,group_vars,host_vars,files,templates}
cd ansible-project

# Créer les fichiers de base
touch ansible.cfg
touch inventory/hosts.ini
touch playbooks/site.yml

Example of a complete structure

ansible-project/
├── ansible.cfg
├── inventory/
│ ├── hosts.ini # Inventaire principal
│ ├── production/ # Inventaire prod
│ │ └── hosts.ini
│ └── staging/ # Inventaire staging
│ └── hosts.ini
├── playbooks/
│ ├── site.yml # Playbook principal
│ ├── webservers.yml
│ └── databases.yml
├── roles/
│ ├── common/ # Rôle commun à tous
│ ├── nginx/ # Rôle serveur web
│ └── postgresql/ # Rôle base de données
├── group_vars/
│ ├── all.yml # Variables pour tous
│ ├── webservers.yml # Variables groupe web
│ └── databases.yml # Variables groupe db
├── host_vars/
│ └── web-01.yml # Variables spécifiques
├── files/ # Fichiers statiques
│ └── config.conf
└── templates/ # Templates Jinja2
└── nginx.conf.j2

9 - First test

Create the minimal inventory

# inventory/hosts.ini
[local]
localhost ansible_connection=local

[webservers]
web-01 ansible_host=192.168.1.10
web-02 ansible_host=192.168.1.11

[databases]
db-01 ansible_host=192.168.1.20

[all:vars]
ansible_user=ansible
ansible_python_interpreter=/usr/bin/python3

Test the connectivity

# Ping localhost
ansible localhost -m ping

# Ping tous les hôtes
ansible all -m ping

# Ping un groupe spécifique
ansible webservers -m ping

# Avec plus de détails
ansible all -m ping -v

Expected result

localhost | SUCCESS => {
"changed": false,
"ping": "pong"
}
web-01 | SUCCESS => {
"changed": false,
"ping": "pong"
}

10 - Troubleshooting

Common problems

Diagnostic commands

# Test SSH manuel
ssh -v user@target-host

# Vérifier la configuration Ansible
ansible-config dump

# Mode verbose
ansible all -m ping -vvv

# Vérifier l'inventaire
ansible-inventory --list

# Graphique de l'inventaire
ansible-inventory --graph

Resolve SSH errors

# Erreur: Host key verification failed
# Solution 1: Ajouter la clé
ssh-keyscan -H target-host >> ~/.ssh/known_hosts

# Solution 2: Désactiver la vérification (dev uniquement)
# Dans ansible.cfg: host_key_checking = False

# Erreur: Permission denied (publickey)
# Vérifier que la clé est copiée
ssh-copy-id user@target-host

# Vérifier les permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

Summary

Key points
  • Install Ansible on the Control Node only
  • Configure SSH with keys (no password)
  • Use one ansible.cfg per project
  • Test with ansible all -m ping before continuing

Practical exercises

  1. Install Ansible on your machine
  2. Create a project structure
  3. Configure SSH to a VM or container
  4. Validate with an Ansible ping

← Introduction | Inventory →