Online courses

Free courses, labs and MOOCs, with your progress saved.

Terraform & Ansible — The course

Guide 07 / 3

Ansible glossary

Decode how Ansible works and read a playbook without mixing up host, task, module and role.

Expected outcome :

Tell the control node from the managed hosts, explain idempotence and read a playbook's organisation.

Reading
0%

01 · Understanding automation

Ansible automates technical operations by describing the expected state: it favours repeatable tasks over manual handling.

Ansible

A tool for automating and configuring systems, applications and infrastructure. It runs tasks on targets described in an inventory.

Exemple métier : préparer 12 serveurs web de la même manière.
Remember :

Ansible is not a container orchestrator: it complements Docker and Kubernetes.

Control node

The machine from which you run commands and playbooks. It has Ansible and the project files.

Ordinateur du formateur ou VM Linux de gestion.
Remember :

Managed Linux servers generally do not need the Ansible program installed.

Managed node

A server or device on which Ansible acts, often over SSH. It is described in the inventory.

vm-web-01 est configurée depuis le poste de contrôle.
Remember :

For Python modules, a compatible version of Python must be available on the target.

Agentless

A model where no permanent Ansible service is required on the target Linux hosts; tasks use SSH, among other things.

Une connexion SSH suffit dans un cas Linux habituel.
Remember :

A reachable network, valid credentials and sufficient rights are still indispensable.

Infrastructure as Code (IaC)

An approach consisting of expressing infrastructure as versioned, reproducible files.

Le dépôt Git contient inventory.ini et playbooks/web.yml.
Remember :

Review and validate changes like application code.

Desired state

The result to achieve on the target: package installed, file present, service started.

state: present demande qu’un paquet soit installé.
Remember :

Declaring the result is often more reliable than running a series of shell commands.

Idempotence

The property of a repeated operation that no longer changes the system once the requested state has been reached.

Relancer « nginx installé » ne réinstalle pas nginx sans raison.
Remember :

A module is idempotent depending on its function and the parameters used; “shell” is not automatically so.

Configuration management

Keeping a set of equipment in a defined configuration, despite interventions and changes.

S’assurer que chrony est installé sur tous les serveurs.

02 · Inventory and connection

Before automating, you must know which machines are targeted and how Ansible reaches them.

Inventory

A file or dynamic source listing the machines and their groups, possibly with connection parameters.

[web]
web1 ansible_host=192.0.2.11
web2 ansible_host=192.0.2.12
Remember :

192.0.2.0/24 is an example range; replace it with the lab's IPs.

Host

An inventory entry that designates a machine and serves as an identifier in Ansible commands.

web1 peut pointer vers 192.0.2.11 via ansible_host.

Group

A named set of machines for running tasks across a functional scope.

[web], [db], [prod] et [lab].

Host pattern

An expression selecting the targets of a play or a command.

hosts: web ; ansible web -i inventory.ini -m ping
Remember :

all targets every declared target; avoid all for risky operations.

ansible_host

A variable setting the DNS name or IP used to reach a host.

web1 ansible_host=192.0.2.11

ansible_user

A variable specifying the account used for the remote connection.

ansible_user=deployer
Remember :

This account's rights determine which operations are possible.

ansible_connection

The transport mode to the target (ssh, local, winrm depending on the environment).

ansible_connection=local pour un laboratoire local.

SSH key

A key pair that lets you authenticate without sending a password on each connection.

ssh -i ~/.ssh/id_ed25519 deployer@192.0.2.11
Remember :

Do not store any private key in the Git repository.

group_vars / host_vars

Conventional variable folders, respectively shared by a group or specific to a host.

group_vars/web.yml : http_port: 8080
Remember :

A value defined in several places follows precedence rules.

Dynamic inventory

A plugin or source that automatically produces a list of machines from a cloud or a management system.

Découvrir les VM d’un projet cloud sans modifier un fichier INI.

03 · Building a playbook

A playbook is a YAML scenario: it links targets, tasks and parameters. It reads from top to bottom.

Playbook

A YAML file containing a list of plays; it is the automation procedure.

ansible-playbook -i inventory.ini site.yml

Play

A block that combines a selection of hosts, an execution policy and tasks.

- name: Serveurs web
  hosts: web
  become: true
  tasks: ...

Task

A named step asking Ansible to run a module with its arguments on the target.

- name: Installer nginx
  ansible.builtin.apt:
    name: nginx
    state: present

Module

An Ansible functional unit that performs an operation: package, file, service, user or API request.

ansible.builtin.copy copie un fichier sur une cible.
Remember :

Prefer a dedicated module to a shell command where possible.

FQCN

A module's full name with its namespace and collection, to avoid ambiguity.

ansible.builtin.service plutôt que service.

Collection

A package distributing modules, plugins and sometimes roles for a particular technology.

community.docker ou kubernetes.core
Remember :

Some collections must be installed separately from ansible-core.

Role

A reusable folder structure that groups tasks, handlers, variables, templates and files.

roles/nginx/tasks/main.yml
Remember :

Useful for sharing a procedure between several projects.

Handler

A deferred task, triggered by notify when a task has reported a change.

Recharger nginx seulement si sa configuration a changé.
Remember :

Handlers normally run at the end of the play or on a flush.

notify

A keyword used to alert a handler following a real change.

notify: Recharger nginx

become

Privilege escalation on the target, for example via sudo, to install a package or write in /etc.

become: true
Remember :

It does not mean “log in directly as root”.

gather_facts

A step that collects system information, enabled by default on many plays.

gather_facts: true ; ansible_facts["os_family"]

04 · Variables, logic and reuse

Variables make the same playbook reusable across several groups or environments.

Variable

A name giving access to a piece of configuration data without duplicating the playbook.

http_port: 8080 ; {{ http_port }}

Jinja2

A templating engine used to inject variables, expressions and conditions into files or parameters.

server_name {{ site_hostname }};
Remember :

Keep expressions simple; review the rendered values.

Template (.j2)

A file model transformed with the variables before being installed on the host.

templates/site.conf.j2 → /etc/nginx/sites-available/site

when condition

A condition deciding whether a task should run depending on a value or on facts.

when: ansible_facts["os_family"] == "Debian"

Loop (loop)

Repeats the same task over a list, without copying the YAML.

loop: ["curl", "git", "htop"]

register

Stores a task's result so it can be analysed in the following tasks.

register: resultat_nginx

changed / ok / failed / skipped

The states returned for each task: change made, already compliant, failure or skipped task.

changed=0 lors d’une seconde exécution idempotente.
Remember :

A summary with no errors does not on its own prove that the service responds correctly.

Tags

Labels for running only part of a playbook.

tags: [configuration] ; --tags configuration

Variable precedence

Rules that determine which value wins when a variable exists in several places.

Une variable -e peut écraser une variable group_vars.
Remember :

Avoid defining the same parameter in several places.

05 · Control, security and operations

Useful automation must be verifiable, limited to a scope and never expose its secrets.

Check mode

A simulation estimating the changes without applying them, when the modules allow it.

ansible-playbook site.yml --check
Remember :

Some modules do not predict all changes: the simulation is not absolute proof.

Diff mode

Displays before/after differences for compatible modules, notably templates.

ansible-playbook site.yml --check --diff
Remember :

The output may contain sensitive data; protect it.

Ansible Vault

A mechanism for encrypting variables or files at rest within an Ansible project.

ansible-vault encrypt group_vars/prod/vault.yml
Remember :

Vault does not automatically protect secrets after they are decrypted nor in logs.

no_log

An option that hides a task's usual output, useful when it handles secrets.

no_log: true
Remember :

Replaces neither encryption nor proper permission management.

Least privilege

The principle of granting only the rights needed to accounts and automations.

sudo uniquement pour installer les paquets requis.

Serial / rolling update

Runs the play on limited batches of machines rather than simultaneously.

serial: 1 pour mettre à jour un serveur à la fois.
Remember :

Plan for availability and remaining capacity before taking a node out of service.

ansible.cfg

The Ansible project's configuration file: default inventory, behaviours, paths, etc.

[defaults]
inventory = inventory.ini
Remember :

Check the configuration file actually loaded with ansible --version.

Ansible Galaxy

A command and repository for fetching, installing and organising collections or roles.

ansible-galaxy collection install community.docker