AnsibleUndefinedVariable: 'dict object' has no attribute

SUMMARY

jinja2 template issues with hostvars

ISSUE TYPE- Bug Report
COMPONENT NAME

jinja2 template

ANSIBLE VERSION

ansible 2.7.2
config file = /home/feisa/ansible-linux/ansible.cfg
configured module search path = [u’/home/feisa/ansible-linux/library’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /usr/bin/ansible
python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]

CONFIGURATION

jinja2 template

{% for host in groups['servers_production'] %}
   {{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }}
{% endfor %}

- name: Template Knownhosts
  template: src=./ssh.j2 dest=/tmp/temp.conf

OS / ENVIRONMENT

Centos 7.6

STEPS TO REPRODUCE

run normal ansible playblook

EXPECTED RESULTS

playbook runs succesfully

ACTUAL RESULTS

{“changed”: false, “msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘eth0’”}

Hello Freddie,

So it depends on the host and how they are named and was really a test case. The one I’m really concerned about is this one now

{% for host in groups[‘servers_production’] %}
{{ hostvars[host][‘ssh_host_key_ecdsa_public’] }}
{% endfor %}

I think you mean: hostvars[host]*['ansible_facts']*['ssh_host_key_ecdsa_public']

This works for me and prints the key.---
- hosts: all

tasks:
- debug:
msg: "{% for host in groups['all'] %} {{ hostvars[host]['ansible_facts']['ssh_host_key_ecdsa_public'] }} {% endfor %}"

Hugo G.

I had tried but still receive

fatal: [server]: FAILED! => {“changed”: false, “msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘ssh_host_key_ecdsa_public’”}

I had tried but still receive

fatal: [server]: FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'ssh_host_key_ecdsa_public'"}

Please post the play you're using, or at least the relevant task and template.

This is what I’m running

My role

  • name: Template Knownhosts
    template: src=./ssh_key.j2 dest=/tmp/temp.conf

My template

{% for host in groups[‘all’] %}
“{{ hostvars[host][‘ansible_facts’][‘ssh_host_key_ecdsa_public’] }}”
{% endfor %}

My playbook

Got to be something else. Are these linux hosts?

I tried this and it works:

playbook:

They keys are there in facts

I just ran the same thing you ddi with the same issue I had.

What version are you running? The linux host is centos7.6

Verified in my facts

“ansible_ssh_host_key_ecdsa_public”: “AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBGpMZqjhIzo6gGjz4eczxnatrAgmPxdWVYf0zY29MDngkKuOzjB0bMrR5sQm1X6leGgYowv3wNloWOZVbhwPU2A=”,

Your version has double quotes around the second line. The known working version does not. Could that be an issue?

Regards, K.

I removed it with the same issue

{% for host in groups[‘all’] %}
{{ hostvars[host][‘ansible_facts’][‘ssh_host_key_ecdsa_public’] }}
{% endfor %}

Hello Freddie

template:

{% for host in groups[‘all’] %}
{{ hostvars[host][‘ansible_facts’][‘ssh_host_key_ecdsa_public’] }}
{% endfor %}

This template also works for me, and the keys are different. So you might want to try it:

{% for host in groups[‘all’] %}
{{ hostvars[host][‘ansible_ssh_host_key_ecdsa_public’] }}
{% endfor %}

Ill test today

No luck so far

Same error, can you send full playbook details over you are testing?

localhost]: FAILED! => {“changed”: false, “msg”: “AnsibleUndefinedVariable: ‘ansible.vars.hostvars.HostVarsVars object’ has no attribute ‘ansible_ssh_host_key_ecdsa_public’”}

Can you try removing the "

ignore_errors: yes
"you had before? Maybe something in failing in the facts gathering and, naturally, the variables did not get populated.

Here’s the full playbook and template I used to test (this does not even need to escalate privileges):

Getting this now.

localhost]: FAILED! => {“changed”: false, “msg”: “AnsibleUndefinedVariable: ‘ansible.vars.hostvars.HostVarsVars object’ has no attribute ‘ansible_ssh_host_key_ecdsa_public’”}

I debug the facts and see it