# AnsibleUndefinedVariable: 'dict object' has no attribute

**URL:** <https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360>\
**Category:** Ansible Project\
**Created:** [January 3, 2019, 6:04pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360 "2019-01-03T18:04:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 3, 2019, 6:04pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/1 "2019-01-03T18:04:12Z")

</div>

##### 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

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

```

```auto
- 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’”}

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [January 3, 2019, 9:31pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/2 "2019-01-03T21:31:27Z")

</div>

Hello Freddie,

---

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 3, 2019, 9:34pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/3 "2019-01-03T21:34:47Z")

</div>

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 %}

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [January 3, 2019, 10:23pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/4 "2019-01-03T22:23:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 3, 2019, 10:26pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/5 "2019-01-03T22:26:58Z")

</div>

I had tried but still receive

fatal: [server]: FAILED! =\> {“changed”: false, “msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘ssh\_host\_key\_ecdsa\_public’”}

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [January 3, 2019, 10:31pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/6 "2019-01-03T22:31:45Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 3, 2019, 10:33pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/7 "2019-01-03T22:33:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [January 3, 2019, 11:06pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/8 "2019-01-03T23:06:04Z")

</div>

Got to be something else. Are these linux hosts?

I tried this and it works:

playbook:

---

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 3, 2019, 11:07pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/9 "2019-01-03T23:07:01Z")

</div>

They keys are there in facts

---

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 3, 2019, 11:20pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/10 "2019-01-03T23:20:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 3, 2019, 11:23pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/11 "2019-01-03T23:23:22Z")

</div>

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

Verified in my facts

“ansible\_ssh\_host\_key\_ecdsa\_public”: “AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBGpMZqjhIzo6gGjz4eczxnatrAgmPxdWVYf0zY29MDngkKuOzjB0bMrR5sQm1X6leGgYowv3wNloWOZVbhwPU2A=”,

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [January 3, 2019, 11:38pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/12 "2019-01-03T23:38:36Z")

</div>

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

Regards, K.

---

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 3, 2019, 11:40pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/13 "2019-01-03T23:40:44Z")

</div>

I removed it with the same issue

{% for host in groups[‘all’] %}  
{{ hostvars[host][‘ansible\_facts’][‘ssh\_host\_key\_ecdsa\_public’] }}  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [January 4, 2019, 6:18pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/14 "2019-01-04T18:18:07Z")

</div>

Hello Freddie

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [January 4, 2019, 6:47pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/15 "2019-01-04T18:47:03Z")

</div>

> 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 %}

---

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 7, 2019, 3:40pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/16 "2019-01-07T15:40:16Z")

</div>

Ill test today

---

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 7, 2019, 4:27pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/17 "2019-01-07T16:27:50Z")

</div>

No luck so far

---

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 7, 2019, 4:30pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/18 "2019-01-07T16:30:40Z")

</div>

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’”}

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [January 7, 2019, 4:53pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/19 "2019-01-07T16:53:09Z")

</div>

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):

---

<div class="post-metadata">

**Author:** ![Freddie\_Eisa](https://avatars.discourse-cdn.com/v4/letter/f/ecae2f/32.png) [@Freddie\_Eisa](https://forum.ansible.com/u/Freddie_Eisa)\
**Post date:** [January 7, 2019, 6:49pm UTC](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360/20 "2019-01-07T18:49:14Z")

</div>

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

[Next page](https://forum.ansible.com/t/ansibleundefinedvariable-dict-object-has-no-attribute/29360.md?page=2)
