Skip to content
Latchkey

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.

ansible
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

  1. Add an is defined guard before using the variable.
  2. Provide a default so the condition has a value in every run.
  3. Re-run to confirm the task skips or runs as intended.
site.yml
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.

.github/workflows/ci.yml
ansible-playbook site.yml -e "deploy_env=prod"

How to prevent it

  • Add is defined guards to conditions on optional variables.
  • Provide defaults in defaults/main.yml for role variables.
  • Pass environment-selecting variables explicitly in CI.

Frequently asked questions

What causes ""The conditional check ... failed""?
The variable is defined for some inventories or passed interactively, but the CI run never sets it, so the conditional cannot evaluate.
How do I fix "The conditional check ... failed"?
Guard the variable in the condition

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card