Skip to content
Latchkey

Ansible "'dict object' has no attribute X" Jinja error in CI

A Jinja expression accessed an attribute or key that is not present on the dictionary, so templating raised an undefined error. The data did not have the shape the template assumed, often because a fact or registered result is missing in CI.

What this error means

A templated task fails with "fatal: [host]: FAILED! => ... 'dict object' has no attribute 'X'" or "AnsibleUndefinedVariable".

ansible
fatal: [web1]: FAILED! => {"msg": "The task includes an option with an
undefined variable. The error was: 'dict object' has no attribute 'address'"}

Common causes

The key is absent on this host or platform

A fact or variable that exists in one environment is missing in CI, so accessing .address on it fails.

A registered result is referenced before it is set

The dict came from a task that was skipped or not yet run, leaving the attribute undefined.

How to fix it

Guard the access with a default

  1. Identify the dict and the missing key from the error.
  2. Use default() or a when guard so missing data does not crash templating.
  3. Re-run to confirm the task handles the absent key.
site.yml
# template: tolerate a missing key
ip: "{{ ansible_default_ipv4.address | default('0.0.0.0') }}"

Gather the facts the template needs

If the attribute should exist, ensure gather_facts runs or the producing task completes before the template uses it.

site.yml
- hosts: web
  gather_facts: true

How to prevent it

  • Default optional dictionary lookups in templates.
  • Ensure facts are gathered before templates that use them.
  • Guard tasks with when so skipped results are not dereferenced.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →