RousselTMLEARNING

TP · Ansible: from discovery to expertise

Lab: Install Ansible and prepare the machines with correction

The correction is integrated into the TP (See the green boxes)

What you will learn in this TP
  • Install Ansible with pip
  • Install Ansible with the package manager
  • Create an automation account
  • Validate the SSH connection
Mode :

All steps and instructions are shown. Recommended for this lab: guided

Introduction #

Context. Before automating anything, you need a control node and machines that accept it. You prepare the course "env_0" environment.

Prerequisites: the cloned test environment (lab "Test environment setup"), VirtualBox and Vagrant  ·  Estimated time: 40 min

Ansible installed on two machines and a "pong" from every host, as the ansible user with sudo.

Setup #

Guided steps, with their solution.

  1. 1. Start the environment

    Why? The three virtual machines are described in the Vagrantfile: one command creates them identically.

    Start env_0 and note the name and IP address of each machine.
    See the correction
    cd tp/env_0
    vagrant up
    vagrant status
    for vm in $(vagrant status --machine-readable | awk -F, '$3=="state" {print $2}'); do
      echo "$vm : $(vagrant ssh $vm -c 'hostname -I' 2>/dev/null)"
    done
  2. 2. Install with pip

    Why? pip gives the latest Ansible version; the virtual environment isolates it from system Python packages.

    On rousseltm-ansible-v02, install Ansible with pip in a virtual environment and check the version.
    See the correction
    vagrant ssh rousseltm-ansible-v02
    # Debian / Ubuntu : sudo apt-get update && sudo apt-get install -y python3-venv
    python3 -m venv ~/ansible-venv
    . ~/ansible-venv/bin/activate
    pip install --upgrade pip ansible
    ansible --version | head -1
  3. 3. Identify the system

    Why? The package manager depends on the system family: apt for Debian and Ubuntu, dnf for Red Hat, Rocky and Alma.

    Identify the system family and version of rousseltm-ansible-v01.
    See the correction
    vagrant ssh rousseltm-ansible-v01 -c 'grep -E "^(ID|ID_LIKE|VERSION_ID)=" /etc/os-release'
  4. 4. Install with the package manager

    Why? The system package is updated with the rest of the machine: it is the simplest choice to maintain.

    Install Ansible on rousseltm-ansible-v01 with the command suited to its family.
    See the correction
    vagrant ssh rousseltm-ansible-v01
    # Debian / Ubuntu
    sudo apt-get update && sudo apt-get install -y ansible
    # Red Hat / Rocky / Alma
    sudo dnf install -y epel-release && sudo dnf install -y ansible-core
    ansible --version | head -1
  5. 5. Create the ansible user

    Why? A dedicated automation account is easy to trace and revoke, without touching personal accounts.

    On each of the three machines, create the ansible user and give it passwordless sudo.
    See the correction
    sudo useradd -m -s /bin/bash ansible
    echo 'ansible ALL=(ALL) NOPASSWD:ALL' | sudo tee /etc/sudoers.d/ansible
    sudo chmod 440 /etc/sudoers.d/ansible
    sudo visudo -cf /etc/sudoers.d/ansible      # parsed OK
  6. 6. Distribute an SSH key

    Why? Ansible connects over SSH: a key avoids passwords and allows unattended automation.

    On the control node, generate a key for ansible, then add the public key to the ansible account authorized_keys on the other machines.
    See the correction
    # Nœud de contrôle
    sudo -iu ansible ssh-keygen -t ed25519 -N '' -f /home/ansible/.ssh/id_ed25519
    sudo cat /home/ansible/.ssh/id_ed25519.pub
    # Sur chaque machine cible (remplacer <clé publique>)
    sudo -u ansible mkdir -p /home/ansible/.ssh
    echo '<clé publique>' | sudo -u ansible tee -a /home/ansible/.ssh/authorized_keys
    sudo chmod 700 /home/ansible/.ssh && sudo chmod 600 /home/ansible/.ssh/authorized_keys
  7. 7. First ping

    Why? The ping module checks the whole chain: SSH, user, Python on the target. -b also checks sudo.

    As ansible on the control node, write an inventory with the noted addresses, then run a ping and a command with privilege escalation.
    See the correction
    sudo -iu ansible
    cat > hosts.ini <<'EOF'
    [cibles]
    192.168.56.11
    192.168.56.12
    EOF
    ansible all -i hosts.ini -m ping                    # "ping": "pong"
    ansible all -i hosts.ini -b -m command -a 'id -u'     # 0

Validate your work

Check that each goal above is reached and that you get the result described in the introduction.

Difficulty level : ●●○○○ (2/5)

My progress

  • ⏱ Estimated time : ≈ 40 min

🧪 Launch the lab environment ↗

Sign in to track your progress and take notes. Sign in

⭐ Rate this lesson

4.0 initial rating from the teaching team

Sign in to rate this lesson →

💬 Questions about this lesson

No questions yet: be the first to ask for help or share your configuration.

Ask a question about this lesson →

Course Glossary

Ansible

An open source IT automation tool (IaC) enabling configuration management, application deployment, and orchestration, operating agentlessly via SSH.

Recommended Articles

Identities and access on OVHcloud: account, IAM, OpenStack users and keys

Secure access to OVHcloud: two-factor authentication on the account, OVHcloud IAM (identities, po...

Infrastructure as Code and GitOps: Terraform, Ansible and Argo CD

Manage infrastructure as code: declarative or imperative, Terraform (providers, state, plan, modu...

Automation and infrastructure as code for Elasticsearch: API, Terraform, Ansible and CI/CD

Automate Elasticsearch administration: REST API and clients, the elasticstack Terraform provider ...

Visualizing and automating Zabbix: dashboards, Grafana and API

Build dashboards and maps in Zabbix, use Grafana's Zabbix plugin, and automate configuration with...

Kibana: role, installation and first steps

What Kibana is in the Elastic suite, how to install it with Elasticsearch (start-local, Docker), ...

Compute on Azure: virtual machines, App Service, Functions and containers

Choose and use Azure compute: virtual machines and scale sets (VMSS), App Service, Azure Function...

Vagrant multi-machine: deploying a ready-to-use Docker Swarm cluster

Use a single Vagrantfile to build a three-node Docker Swarm cluster (one manager, two workers) wi...

Containers, Kubernetes and cloud native in a DevOps approach

The role of containers and Kubernetes in DevOps: reproducible images, multi-stage Dockerfiles, re...

Sharing a Vagrant environment: ngrok and the vagrant-share plugin

Make an application running in a Vagrant machine accessible to a colleague or a client: port forw...

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!