Online courses

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

Infrastructure

Course Intermediate Free

Terraform & Ansible — The course

Describe infrastructure with Terraform and configure it with Ansible: providers, playbooks, roles and idempotence.

  • 3 guides
  • 146 concepts
Start reading

Guides

  1. 07 Guide Ansible glossary Automation vocabulary and concepts 46 concepts
  2. 08 Guide Ansible commands Installation, inventories, playbooks, Vault and diagnostics 49 concepts
  3. 09 Guide Ansible configuration and business cases YAML playbooks and field scenarios 51 concepts

Suggested path

  1. 07. Understand the architecture, the inventory and the playbooks
  2. 08. Check the hosts then simulate the tasks before running
  3. 09. Adapt Ansible business cases and validate their result

Sources and references

Linux (Debian/Ubuntu) teaching examples for authorised labs. The 192.0.2.x addresses are fictitious; rights and prerequisites must be adapted. Protect access and secrets.

Example files

inventory.ini
[web]
web1 ansible_host=192.0.2.11
web2 ansible_host=192.0.2.12

[web:vars]
ansible_user=deployer
ansible_python_interpreter=/usr/bin/python3

[local]
localhost ansible_connection=local
ansible.cfg
[defaults]
inventory = inventory.ini
host_key_checking = True
interpreter_python = auto_silent
requirements.yml
collections:
  - name: ansible.posix
  - name: community.docker
  - name: kubernetes.core
01-preparer-serveurs.yml
---
- name: Preparations communes des serveurs Ubuntu
  hosts: web
  become: true
  gather_facts: true
  tasks:
    - name: Verifier le systeme d'exploitation
      ansible.builtin.assert:
        that:
          - ansible_facts['os_family'] == 'Debian'
        fail_msg: "Ce cas vise Debian et Ubuntu."
    - name: Mettre a jour le cache des paquets
      ansible.builtin.apt:
        update_cache: true
        cache_valid_time: 3600
    - name: Installer les outils d'exploitation
      ansible.builtin.apt:
        name: [curl, git, htop, ca-certificates]
        state: present
    - name: Creer un dossier applicatif
      ansible.builtin.file:
        path: /opt/portail
        state: directory
        mode: '0755'
02-deployer-nginx.yml
---
- name: Installer et configurer le serveur web
  hosts: web
  become: true
  vars:
    site_hostname: demo.example.test
  tasks:
    - name: Installer nginx
      ansible.builtin.apt:
        name: nginx
        state: present
        update_cache: true
    - name: Deployer la page d'accueil
      ansible.builtin.copy:
        src: files/index.html
        dest: /var/www/html/index.html
        owner: root
        group: root
        mode: '0644'
    - name: Generer le virtual host
      ansible.builtin.template:
        src: templates/site.conf.j2
        dest: /etc/nginx/sites-available/demo
        owner: root
        group: root
        mode: '0644'
      notify: Recharger nginx
    - name: Activer le site
      ansible.builtin.file:
        src: /etc/nginx/sites-available/demo
        dest: /etc/nginx/sites-enabled/demo
        state: link
      notify: Recharger nginx
    - name: Valider la configuration nginx
      ansible.builtin.command: nginx -t
      changed_when: false
    - name: Demarrer nginx au boot
      ansible.builtin.service:
        name: nginx
        state: started
        enabled: true
  handlers:
    - name: Recharger nginx
      ansible.builtin.service:
        name: nginx
        state: reloaded
02-site.conf.j2
server {
    listen 80;
    server_name {{ site_hostname }};
    root /var/www/html;
    index index.html;
    location / { try_files $uri $uri/ =404; }
}
02-index.html
<!doctype html>
<html lang="fr"><meta charset="utf-8">
<title>Atelier Ansible</title><h1>Deploiement Ansible OK</h1>
03-acces-ssh.yml
---
- name: Creer un compte de deploiement
  hosts: web
  become: true
  tasks:
    - name: Creer le compte applicatif
      ansible.builtin.user:
        name: deploiement
        shell: /bin/bash
        create_home: true
        state: present
    - name: Installer sa cle publique
      ansible.posix.authorized_key:
        user: deploiement
        key: "{{ lookup('file', 'files/deploiement.pub') }}"
        state: present
04-maintenance.yml
---
- name: Maintenir les serveurs successivement
  hosts: web
  become: true
  serial: 1
  tasks:
    - name: Installer les correctifs disponibles
      ansible.builtin.apt:
        update_cache: true
        upgrade: safe
    - name: Detecter un redemarrage demande par Ubuntu
      ansible.builtin.stat:
        path: /var/run/reboot-required
      register: reboot_requis
    - name: Redemarrer uniquement si necessaire
      ansible.builtin.reboot:
        reboot_timeout: 600
      when: reboot_requis.stat.exists
05-docker-compose.yml
---
- name: Deployer une application Docker Compose
  hosts: web
  become: true
  tasks:
    - name: Creer le dossier du projet
      ansible.builtin.file:
        path: /opt/portail
        state: directory
        mode: '0755'
    - name: Publier compose.yaml
      ansible.builtin.copy:
        src: files/compose.yaml
        dest: /opt/portail/compose.yaml
        mode: '0644'
    - name: Lancer la stack Compose
      community.docker.docker_compose_v2:
        project_src: /opt/portail
        state: present
05-compose.yaml
services:
  web:
    image: nginx:stable-alpine
    ports:
      - "8080:80"
    restart: unless-stopped
06-kubernetes.yml
---
- name: Publier une ressource sur Kubernetes
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Appliquer le manifeste YAML via l'API
      kubernetes.core.k8s:
        state: present
        definition: "{{ lookup('file', 'manifests/web.yaml') | from_yaml }}"
06-web.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: demo-ansible
  namespace: default
data:
  MESSAGE: "Configuration geree par Ansible"
07-audit.yml
---
- name: Observer la conformite des serveurs
  hosts: web
  become: true
  tasks:
    - name: Collecter les services
      ansible.builtin.service_facts:
    - name: Verifier que nginx est actif
      ansible.builtin.assert:
        that:
          - "'nginx.service' in ansible_facts.services"
          - "ansible_facts.services['nginx.service'].state == 'running'"
        fail_msg: "Nginx ne fonctionne pas sur cet hote."
    - name: Donner le bilan
      ansible.builtin.debug:
        msg: "Nginx actif sur {{ inventory_hostname }}"

In the same series Step 1 of 2

  1. 1 Course Terraform & Ansible — The course You are here
  2. 2 Lab Terraform & Ansible labs

Share this article

In the same category

Course

Infrastructure · Intermediate

Docker & Docker Compose — The course

Images, containers, volumes, networks and Compose: vocabulary, essential commands and good practice.

3 guides · 132 concepts In the same series

Course

Infrastructure · Intermediate

Kubernetes — The course

Pods, deployments, services, configuration and storage: orchestrating containers, from theory to kubectl.

3 guides · 159 concepts In the same series

Lab

Infrastructure · Intermediate

Docker & Docker Compose labs

Three progressive labs: build, configure then expose a multi-service application with Compose.

3 labs · 53 steps In the same series