Ansible: The conditional check 'result.rc == 0' failed
The when expression could not be evaluated, usually because the variable it names does not exist on this host: the registering task was skipped, ran on a different host, or the play failed before reaching it. The message continues with the Jinja error, and 'dict object' has no attribute is the tell that the fact was never set.
Quick fix
Read the commands before running them. Anything that restarts a service, deletes data or changes permissions should be tried on a non-production system first.
# See what the variable actually holds on the failing host
ansible-playbook site.yml -l web01 -vvv
- debug: var=result
# A registered variable from a skipped task is still defined, but has no rc
- name: Check the service
command: systemctl is-active nginx
register: result
when: ansible_facts['os_family'] == 'RedHat'
changed_when: false
failed_when: false
- name: Act on it
debug: msg="running"
when: result.rc is defined and result.rc == 0
# Defaults keep the expression evaluable everywhere
when: (result.rc | default(1)) == 0
# Do not wrap the whole condition in braces: when is already an expression
# when: "{{ result.rc == 0 }}" wrong
# when: result.rc == 0 right
How to diagnose Ansible errors
Ansible errors split cleanly between transport problems (it cannot reach or authenticate to the host) and execution problems (it reached the host but the play failed). The message header tells you which: UNREACHABLE! is transport, FAILED! is execution. Transport issues are SSH issues wearing an Ansible hat and should be debugged with plain ssh first.
If the quick fix above does not resolve it, work through these steps. They apply to this whole class of error, not just to this one message, which is usually what saves the time.
- Run
ansible -m ping allbefore running any playbook. It isolates connectivity and privilege escalation from your task logic. - Raise verbosity progressively:
-vshows results,-vvvshows the SSH command line,-vvvvshows connection debugging. Most unreachable errors are solved at-vvv. - Use
--check --diffto see what a play would change without changing it. This turns a destructive debugging loop into a safe one. - For undefined-variable errors, dump the actual scope with a
debugtask (var: hostvars[inventory_hostname]) rather than reasoning about precedence from the docs. - Remember variable precedence order: extra vars (
-e) beat everything, role defaults lose to everything. Surprising values are nearly always a precedence surprise.
Tools worth reaching for
ansible -m pingansible-playbook --check --diffansible-lintssh -vvv
Authoritative references
Primary documentation for this error, worth reading before applying any fix in production.
Related Ansible errors
- Ansible: 'variable' is undefinedA variable used in a playbook or template was not defined in inventory, group_vars…
- Ansible: Host unreachableCannot connect to target host via SSH. Network issues, wrong IP, SSH not running, or key…
- Ansible: Jinja2 template errorA Jinja2 template in a playbook or template file has a syntax error such as unclosed braces…
- Ansible: Module not foundSpecified module doesn't exist or isn't installed. Wrong module name or collection not…
- Ansible: Playbook syntax errorYAML syntax error in playbook. Indentation issues, invalid YAML, or missing required fields.
- Ansible: Privilege escalation password requiredTask requires sudo but password not provided. Target user needs sudo password for privilege…
- Ansible: SSH connection timeoutAnsible timed out trying to establish an SSH connection to the target host. The host may be…
- Ansible: Vault password not providedA playbook uses encrypted vault variables but no vault password was supplied at runtime.
Browse other categories
- HTTP 494xx client errors, 5xx server errors, redirects, headers and protocol problems.
- JavaScript 42npm resolution, async pitfalls, hydration, memory limits and runtime type…
- Database 41Connections, deadlocks, constraints, replication and memory limits.
- AI 35Rate limits, context windows, GPU memory and model-serving failures.
- Network 35Refused connections, timeouts, resets, MTU problems and port exhaustion.
- Python 35Imports, virtual environments, encoding, concurrency and dependency conflicts.
- Kubernetes 34CrashLoopBackOff, ImagePullBackOff, OOMKilled, RBAC, scheduling and storage.
- Docker 27Daemon connectivity, disk space, image pulls, ports and architecture mismatches.
- System 26Disk space, systemd units, file descriptors, OOM killer and scheduled jobs.
- Cloud 25IAM permissions, quotas, service limits and credential failures.
- Security 25JWT validation, CSRF, OAuth grants, SELinux, SSH host keys and CSP.
- TLS 24Untrusted authorities, expiry, hostname mismatch, chains and cipher negotiation.
Something missing or wrong?
This entry is maintained by hand. If the fix is out of date, incomplete, or you have a better one, email a correction and it will be reviewed.