Undefined variable error in Ansible check mode when referencing ansible_facts without credentials
0 reputation · 26 Oct 2022, 15:05 UTC
When running an Ansible playbook in check mode (--check) without supplying remote credentials, the intent is to validate syntax and logic without making any changes. However, if the playbook contains Jinja2 templates or variable expressions that reference facts such as {{ ansible_facts['distribution'] }}, check mode still attempts to evaluate those expressions. Because fact gathering cannot proceed without a valid connection, the variables remain undefined, causing the playbook to fail with an “undefined variable” error even though no task would modify the remote system.
This behavior raises uncertainty about whether check mode can be relied upon for dry‑run validation when credentials are intentionally omitted. It also prompts the question of how to supply placeholder facts or suppress fact gathering solely to allow template evaluation during a check‑run without exposing sensitive data.
Can fact gathering be disabled or bypassed for check mode while still permitting Jinja2 templates to reference fact‑based variables?
Is there a supported method to inject local dummy facts (e.g., via vars or fact cache) to satisfy template evaluation without requiring remote credentials?