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

How do you make roles fail fast on bad input?

IntermediateProductionTesting, CI/CD and Quality
Answer

Validate inputs before the role changes anything, so a typo fails in the first second instead of leaving a host half configured.

  • meta/argument_specs.yml declares types, required options and choices; Ansible checks them automatically when the role starts.
  • ansible.builtin.assert covers rules a schema cannot express, such as cross-field checks.
- name: Validate inputs
  ansible.builtin.assert:
    that:
      - app_port | int > 1024
      - app_env in ['dev', 'staging', 'prod']
      - not (app_env == 'prod' and app_debug | bool)
    fail_msg: "Invalid inputs for app role: check app_port, app_env and app_debug"
    quiet: true

Put the validation at the top of tasks/main.yml and give clear fail_msg text. The person who sees it at 2 AM did not write the role.

All Ansible questions Roadmap: Ansible

More on Testing, CI/CD and Quality