Skip to content
System

Ansible on Linux: automate a first task without modifying the entire infrastructure

Avatar illustré de Julien Macé 3 min read
Tux devant deux serveurs Linux reliés pour une tâche Ansible limitée à un hôte de test
Ansible Linux première tâche sur hôte de test

When the same check needs to be performed on multiple machines, the reflex is often to copy a SSH command. It works until the day a server is forgotten. To install Ansible on Linux without risking the entire environment, start with a control machine and a single lab host.

The first task should produce a result that is easy to read and easy to erase. Here, Ansible creates a temporary folder on the test host. You first validate the connection, then the predictable change using --check.

Tux in front of two connected Linux servers for a task limited to a test host
An initial Ansible automation should target a test host, not the entire inventory.

Install Ansible on the control machine

On Debian 12 or Ubuntu 24.04, install Ansible on the workstation or server from which you will launch the actions. The remote host mainly needs SSH and Python, as the module ansible.builtin.ping performs a remote check. It does not measure ICMP latency.

sudo apt update
sudo apt install ansible
ansible --version

The final command should display the version of Ansible and the path to its configuration. If you are already managing your keys with an SSH agent, keep that habit. A protected key prevents placing a password in an inventory. The guide on ssh-agent on Linux covers this point.

Limit the inventory to a lab server

Create a working directory, then an inventory containing only a single group name and a single host. Replace the address and account with those of your test environment. The address 192.0.2.10 is reserved for documentation purposes, it should not be copied as is.

mkdir -p ~/ansible-lab
cd ~/ansible-lab
nano inventory.ini
[lab]
srv-test ansible_host=192.0.2.10 ansible_user=admin

Before running Ansible, check that SSH can reach the host with the expected key or account. Incorrect DNS resolution or a password prompt will also block automation.

ansible-inventory -i inventory.ini --graph
ssh -o BatchMode=yes -o ConnectTimeout=5 admin@server-test true

The first display should contain the group lab and its sole host. The SSH command should return without prompting for a password. If it fails, fix SSH before running a playbook.

Test the connection before any write

The Ansible Ping module checks the connection, authentication, and Python execution on the remote host. A pong response confirms that the control path works.

ansible -i inventory.ini lab -m ansible.builtin.ping

Do not replace lab with all during this trial. I would keep a separate inventory as long as the procedure has not been validated, as a variable or privilege error becomes visible on a single machine.

Anticipate the change with a control mode playbook

Create the file premiere-tache.yml. The task uses become: true to create a folder in /var/tmp. This path avoids touching any service, package, or production configuration.

---
- name: Prepare a temporary folder
  hosts: lab
  become: true
  gather_facts: false
  tasks:
    - name: Create the test folder
      ansible.builtin.file:
        path: /var/tmp/ansible-lab
        state: directory
        mode: '0755'

The control mode simulates the modules that support it. Ansible documentation details the limitations of check mode and diff mode, so do not confuse it with a complete audit of each possible command.

ansible-playbook -i inventory.ini premiere-tache.yml --check --diff

On a host where the folder does not exist, the summary typically reports changed=1 without creating the folder. If the target is correct, run the same command without --check.

ansible-playbook -i inventory.ini premiere-tache.yml

Check the result and remove the test folder

A successful Ansible execution does not replace a remote check. The stat module should return exists: true and the expected path.

ansible -i inventory.ini lab -b 
  -m ansible.builtin.stat 
  -a 'path=/var/tmp/ansible-lab'

The folder can then be deleted with an equivalent task in state absent. Keep this rollback phase for a test directory. A package installation, a network modification, or an application deployment requires a separate playbook, reviewed variables, and an appropriate window.

To automate Debian patches, the subject unattended-upgrades brings another level of risk: the scope, logs, and reboots must be decided before expanding the Ansible inventory.

Share Facebook X WhatsApp LinkedIn Pinterest Reddit Email