How do you make roles behave well in check mode?
IntermediateProductionTesting, CI/CD and Quality
Answer
A --check run is only useful if it completes and tells the truth. Rules we follow:
- Read-only commands that later tasks depend on get
check_mode: false, so they actually run and register data. - Tasks that depend on something created earlier in the same run guard themselves with
when: not ansible_check_mode, or useignore_errors: "{{ ansible_check_mode }}". - Prefer modules over
command/shell, because modules report a real predicted change; shell tasks are just skipped in check mode.
- name: Get installed app version
ansible.builtin.command: /opt/app/bin/app --version
register: app_ver
changed_when: false
check_mode: false
failed_when: false
We run --check --diff in CI against staging so a role that crashes in check mode is caught before anyone relies on it in production.
More on Testing, CI/CD and Quality
What does a testing pyramid look like for Ansible code?BeginnerHow do you roll out `ansible-lint` profiles across a team without blocking everyone?IntermediateWhen do you use `skip_list`, `warn_list` and `# noqa`?IntermediateHow do you set up pre-commit hooks for an Ansible repo?BeginnerHow is a Molecule scenario laid out, and what does `molecule test` actually run?IntermediateDocker, Podman or real VMs as the Molecule driver: how do you choose?Advanced