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

Ansible: the inventory (static, dynamic, groups and variables)

Understand and master the Ansible inventory: INI and YAML files, groups, host and group variables...

Ansible: ad-hoc commands

Run immediate actions on a fleet of servers with ansible: the ping, command, shell, copy, apt, se...

Ansible: structuring your code with roles

Create, organize and reuse Ansible roles: directory layout, defaults, tasks, handlers, templates,...

Ansible: managing security (Vault, SSH, privileges and secrets)

Securing Ansible: Ansible Vault, SSH key authentication, become and sudo, no_log, hardening the c...

Ansible: driving your automations with a graphical interface (AWX, Semaphore, AAP)

Compare and install Ansible's graphical interfaces: AWX, Ansible Automation Platform, Semaphore a...

Ansible: generating dynamic files with Jinja2 and facts

Create dynamic configuration files: Jinja2 templates, variables, facts, filters, loops, condition...

Ansible: going further (testing, quality, CI/CD and performance)

Industrialize Ansible: ansible-lint, yamllint, Molecule, continuous integration, collections, exe...

Ansible best practices: structure, idempotence, security and quality

Ansible best practices: project organization, naming, FQCN, idempotence, modules rather than shel...

Ansible: managing your infrastructure with playbooks (Infrastructure as Code)

Write production playbooks: project structure, plays, handlers, tags, serial strategy, error hand...

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!