Interview Prep/Ansible/Testing, CI/CD and Quality

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 use ignore_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.

All Ansible questions Roadmap: Ansible

More on Testing, CI/CD and Quality