Junior — Middle
How can you ensure that the execution of an Ansible role does not change the system state upon re-runs?
sobes.tech AI
Answer from AI
To ensure that executing an Ansible role does not change the system state during repeated runs, it is necessary to ensure the role's idempotency. Idempotency means that running the role again will not change the system if it is already in the desired state.
How to do this:
- Use Ansible modules that are idempotent by default (e.g.,
file,package,service). - Check conditions before making changes using parameters like
creates,when, and others. - Test the role multiple times and verify that no changes occur in subsequent runs (the Ansible output should not show
changed: true).
Example:
- name: Install nginx
apt:
name: nginx
state: present
If nginx is already installed, rerunning this task will not change the system or show any changes.