Notes

Check an Ansible inventory before changing hosts

Use a project-local inventory, test SSH and Python connectivity, then preview a small privileged playbook. Keep package refreshes separate from upgrades.

Ansible runs modules over a connection such as SSH. Before giving it a task that changes packages, establish which hosts it will target and whether it can authenticate. A local inventory makes that scope visible beside the playbook instead of hiding it in a machine-wide configuration.

This example assumes Ansible is available on the control machine, SSH keys work for the selected account, and the Debian or Ubuntu target hosts have Python 3. It does not install or upgrade Ansible itself.

Define the inventory explicitly

Create inventory.ini in a scratch project directory. Replace the example hostnames and account with your lab’s real values before running it:

[rpis]
kn-1
kn-2
kn-3

[rpis:vars]
ansible_user=kuser
ansible_python_interpreter=/usr/bin/python3

Check how Ansible interprets it:

ansible-inventory -i inventory.ini --graph
ansible rpis -i inventory.ini --list-hosts
ansible rpis -i inventory.ini -m ansible.builtin.ping

The Ansible ping module is not an ICMP network ping. A pong response verifies that Ansible connected, transferred its module and ran it with Python. A host can answer ordinary network pings while this test fails because SSH, the account or the Python interpreter is wrong.

Use normal SSH host-key verification. If password authentication is required, --ask-pass requests the connection password; transport requirements depend on the installed Ansible version. Do not disable host verification to make an inventory test pass.

Preview a bounded task

Create refresh-apt.yml:

- name: Refresh package metadata on the lab nodes
  hosts: rpis
  become: true
  serial: 1
  tasks:
    - name: Refresh the APT cache when it is older than one day
      ansible.builtin.apt:
        update_cache: true
        cache_valid_time: 86400

This task refreshes repository metadata. It does not upgrade installed packages. The old draft mixed those operations and used a YAML boolean for the module’s string-valued upgrade choice. Keep an upgrade as a separately reviewed operation, with its own reboot and rollout policy.

Run syntax validation and inspect a limited preview:

ansible-playbook -i inventory.ini refresh-apt.yml --syntax-check
ansible-playbook -i inventory.ini refresh-apt.yml \
  --check --diff --limit kn-1 --ask-become-pass

--ask-become-pass requests the sudo password and is distinct from the SSH password prompt. Omit it when the chosen account’s privilege setup does not require one.

Check mode depends on the module and cannot promise that every real run will succeed. It is still a useful chance to spot the wrong host, wrong variables or unexpected task scope before changing anything.

Apply deliberately

Once the preview matches your intention, remove --check for the selected lab host. Broaden --limit only after checking that result. A successful second run may report no changes while the APT cache is still younger than cache_valid_time; idempotence means the task converges on the requested state rather than repeating a change unconditionally.

The official inventory guide, ping module and APT module cover the connection and module contracts.