SECURITY WARNING: Never run commands you don't understand. Always review code before execution. Use at your own risk.
Ansible New Added 10 September 2026

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.

Quick fix
# 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.

  1. Run ansible -m ping all before running any playbook. It isolates connectivity and privilege escalation from your task logic.
  2. Raise verbosity progressively: -v shows results, -vvv shows the SSH command line, -vvvv shows connection debugging. Most unreachable errors are solved at -vvv.
  3. Use --check --diff to see what a play would change without changing it. This turns a destructive debugging loop into a safe one.
  4. For undefined-variable errors, dump the actual scope with a debug task (var: hostvars[inventory_hostname]) rather than reasoning about precedence from the docs.
  5. 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 ping
  • ansible-playbook --check --diff
  • ansible-lint
  • ssh -vvv

Authoritative references

Primary documentation for this error, worth reading before applying any fix in production.

docs.ansible.com

Related Ansible errors

See all 10 Ansible errors →

Browse other categories

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.