Ansible "The conditional check ... failed" undefined variable in CI
A when condition could not be evaluated because a variable in it is undefined in this run. Ansible reports the exact conditional and the underlying undefined error, and the task fails rather than being silently skipped.
What this error means
A task fails with "The conditional check 'some_var is defined and some_var' failed. The error was: ... 'some_var' is undefined" instead of running or skipping.
fatal: [app1]: FAILED! => {"msg": "The conditional check 'deploy_env == \"prod\"'
failed. The error was: error while evaluating conditional (deploy_env == \"prod\"):
'deploy_env' is undefined"}Common causes
A variable used in when is never set in CI
The variable is defined for some inventories or passed interactively, but the CI run never sets it, so the conditional cannot evaluate.
A typo or wrong scope for the variable
The variable exists under a different name or scope (host_vars vs group_vars), so the reference in when resolves to undefined.
How to fix it
Guard the variable in the condition
- Add an
is definedguard before using the variable. - Provide a default so the condition has a value in every run.
- Re-run to confirm the task skips or runs as intended.
when: deploy_env is defined and deploy_env == "prod"Define the variable for CI
Pass the variable explicitly so the condition can evaluate in the pipeline.
ansible-playbook site.yml -e "deploy_env=prod"How to prevent it
- Add
is definedguards to conditions on optional variables. - Provide defaults in
defaults/main.ymlfor role variables. - Pass environment-selecting variables explicitly in CI.