# Random "'ansible\_hostname' is undefined" errors

**URL:** <https://forum.ansible.com/t/random-ansible-hostname-is-undefined-errors/21180>\
**Category:** Ansible Project\
**Created:** [April 15, 2016, 11:20pm UTC](https://forum.ansible.com/t/random-ansible-hostname-is-undefined-errors/21180 "2016-04-15T23:20:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guy\_Knights](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Guy\_Knights](https://forum.ansible.com/u/Guy_Knights)\
**Post date:** [April 15, 2016, 11:20pm UTC](https://forum.ansible.com/t/random-ansible-hostname-is-undefined-errors/21180/1 "2016-04-15T23:20:57Z")

</div>

I have a weird issue where occasionally when I run a playbook that has a task which uses the {{ ansible\_hostname }} variable in it, I get an error “‘ansible\_hostname’ is undefined”. In most cases, if I re-run the playbook again right after I see the errors, it works fine and I don’t see these errors.

Does anyone know why this might be happening?

Thanks,  
Guy

---

<div class="post-metadata">

**Author:** ![Greg\_Chavez](https://avatars.discourse-cdn.com/v4/letter/g/b2d939/32.png) [@Greg\_Chavez](https://forum.ansible.com/u/Greg_Chavez)\
**Post date:** [April 16, 2016, 9:01pm UTC](https://forum.ansible.com/t/random-ansible-hostname-is-undefined-errors/21180/2 "2016-04-16T21:01:18Z")

</div>

Guy, no, but the next time this happens to you, gather as much information you can before you run again when, presumably, the playbook with run without the error. Present as much as the information as you can share here.

Thanks  
–Greg Chavez

---

<div class="post-metadata">

**Author:** ![Alex\_Leonhardt](https://avatars.discourse-cdn.com/v4/letter/a/ec9cab/32.png) [@Alex\_Leonhardt](https://forum.ansible.com/u/Alex_Leonhardt)\
**Post date:** [April 16, 2016, 10:32pm UTC](https://forum.ansible.com/t/random-ansible-hostname-is-undefined-errors/21180/3 "2016-04-16T22:32:13Z")

</div>

Only ever seen that when the play didn’t gather facts… Which must be explicitly disabled with ‘gather\_facts: no’ or similar… Maybe that’s somewhere in your playbooks?

---

<div class="post-metadata">

**Author:** ![Phonthip\_Namkaew](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@Phonthip\_Namkaew](https://forum.ansible.com/u/Phonthip_Namkaew)\
**Post date:** [April 17, 2016, 10:56am UTC](https://forum.ansible.com/t/random-ansible-hostname-is-undefined-errors/21180/4 "2016-04-17T10:56:02Z")

</div>

I have a similar issue with a playbook which runs once a day using a cron job on the Ansible control host. Probably once a week, I get the follwoing error for a random server:

(…)

The following task failed for host [ejpdxannnn-mgt.example.com](http://ejpdxannnn-mgt.example.com):

template: {“dest”: “/tmp/fragments.d/SLES.frag”, “src”: “std-templates/std-playbook-make-inventory-by-os.j2”, “group”: “admin2”, “mode”: “0400”, “owner”: “admin2”}

with the following message:

AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘ansible\_hostname’

A complete dump of the error:

{“changed”: false, “failed”: true, “msg”: “AnsibleUndefinedVariable: ‘dict object’ has no attribute ‘ansible\_hostname’”}

(…)

Ansible version is 2.0.1.0. The playbook runs on 600 hosts with forks=20. The Ansible control host has 16 GB RAM and if I run the playbook manually there is no memory shortage (no swapping on Ansible control host) and I can not reproduce the error.  
It seems that every now and then, Ansible does not gather the facts for all 600 hosts or the facts get lost on the Ansible control host due to some unknown resource constraints. As far as I understand the concept, Ansible  
keeps all the facts in the control host’s memory while the playbook is run. For 600 hosts that is a lot of data.

The playbook is:

admin2@xxxxx:/data/ansible/playbooks\> cat std-playbook-make-inventory-by-os.yml

---

<div class="post-metadata">

**Author:** ![j.r.hawkesworth](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@j.r.hawkesworth](https://forum.ansible.com/u/j.r.hawkesworth)\
**Post date:** [April 18, 2016, 11:09am UTC](https://forum.ansible.com/t/random-ansible-hostname-is-undefined-errors/21180/5 "2016-04-18T11:09:25Z")

</div>

I think ansible\_hostname is a property of the ssh connection. I’m pretty certain it is not present when you use, for example, wirnm connection.

This is a complete guess but I wonder if in your case sometimes it is failing to set up ssh connection and then falling back to another connection type (say paramiko) which perhaps doesn’t have ansible\_hostname defined.

Hope this helps,

Jon

---

<div class="post-metadata">

**Author:** ![toshywoshy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/toshywoshy/32/7855_2.png) [@toshywoshy](https://forum.ansible.com/u/toshywoshy)\
**Post date:** [April 18, 2016, 1:30pm UTC](https://forum.ansible.com/t/random-ansible-hostname-is-undefined-errors/21180/6 "2016-04-18T13:30:55Z")

</div>

> I have a similar issue with a playbook which runs once a day using a  
> cron job on the Ansible control host. Probably once a week, I get the  
> follwoing error for a random server:
> 
> (..)
> 
> The following task failed for host ejpdxannnn-mgt.example.com:
> 
> template:{"dest": "/tmp/fragments.d/SLES.frag", "src":  
> "std-templates/std-playbook-make-inventory-by-os.j2", "group": "admin2",  
> "mode": "0400", "owner": "admin2"}
> 
> with the following message:
> 
> AnsibleUndefinedVariable: 'dict object' has no attribute 'ansible\_hostname'
> 
> A complete dump of the error:
> 
> {"changed": false, "failed": true, "msg": "AnsibleUndefinedVariable:  
> 'dict object' has no attribute 'ansible\_hostname'"}

Remenber that all variables staring with 'ansible\_\*' are gathered at the beginning of a play section in a playbook.  
Your playbook contains several plays, each play will initiate fact gathering by using the setup module, unless the play section specifies no fact gathering, as you do.  
If memory serves me well, the default behaviour for fact gathering between plays in one playbook changed a while ago.  
I use the 'smart' gathering, so new hosts get facts gathered, while old ones don't

[defaults]  
gathering = smart

[http://docs.ansible.com/ansible/intro\_configuration.html#gathering](http://docs.ansible.com/ansible/intro_configuration.html#gathering)

> (..)
> 
> Ansible version is 2.0.1.0. The playbook runs on 600 hosts with  
> forks=20. The Ansible control host has 16 GB RAM and if I run the  
> playbook manually there is no memory shortage (no swapping on Ansible  
> control host) and I can not reproduce the error.  
> It seems that every now and then, Ansible does not gather the facts for  
> all 600 hosts or the facts get lost on the Ansible control host due to  
> some unknown resource constraints. As far as I understand the concept,  
> Ansible  
> keeps all the facts in the control host's memory while the playbook is  
> run. For 600 hosts that is a lot of data.

I am guessing and presuming you are doing this for speed.  
It might be worth looking at setting up your ansible host with fact caching in redis

For a redis setup that would be as following  
[defaults]  
fact\_caching = redis  
fact\_caching\_timeout = 3600  
fact\_caching\_connection = localhost:6379:0

I have used this in the case of a +/- 500 host setup with a mgmt host of 4GB works fine, and reduces ram, while speeding up most operations

If you want them on your disk you could setup file based caching, however for 600 hosts that might be slow.  
For a file setup that would be as following  
[defaults]  
fact\_caching = jsonfile  
fact\_caching\_timeout = 3600  
fact\_caching\_connection = /tmp/ansible/facts/cache

> The playbook is:
> 
> admin2@xxxxx:/data/ansible/playbooks\> cat  
> std-playbook-make-inventory-by-os.yml  
> ---
> 
> - hosts: all  
> &nbsp;&nbsp;&nbsp;gather\_facts: True  
> &nbsp;&nbsp;&nbsp;tasks:
> 
> &nbsp;&nbsp;&nbsp;- set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path\_to\_fragments: /tmp/fragments.d  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;file\_inventory: /data/ansible/inventory/inventory.os
> 
> - hosts: all[0]  
> &nbsp;&nbsp;&nbsp;gather\_facts: False

Here is you problem
