# Having troubles accessing hostvars for new host

**URL:** <https://forum.ansible.com/t/having-troubles-accessing-hostvars-for-new-host/18529>\
**Category:** Ansible Project\
**Created:** [June 17, 2015, 10:34am UTC](https://forum.ansible.com/t/having-troubles-accessing-hostvars-for-new-host/18529 "2015-06-17T10:34:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [June 17, 2015, 10:34am UTC](https://forum.ansible.com/t/having-troubles-accessing-hostvars-for-new-host/18529/1 "2015-06-17T10:34:33Z")

</div>

I have some tasks like:

`

- debug: var=hostvars[item]  
with\_items: groups.rabbitmq\_server

- name: Update /etc/hosts with rabbitmq-server hostnames  
lineinfile:  
dest: /etc/hosts  
regexp: “^{{hostvars[item][‘ansible\_default\_ipv4’][‘address’]}} {{hostvars[item][‘ansible\_hostname’]}}$”  
line: “{{hostvars[item][‘ansible\_default\_ipv4’][‘address’]}} {{hostvars[item][‘ansible\_hostname’]}}”  
state: present  
with\_items: groups.rabbitmq\_server

`

When the second runs I get:

TASK: [Update /etc/hosts with rabbitmq-server hostnames] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
fatal: [10.0.136.70] =\> One or more undefined variables: ‘dict object’ has no attribute ‘ansible\_default\_ipv4’  
fatal: [10.0.137.195] =\> One or more undefined variables: ‘dict object’ has no attribute ‘ansible\_default\_ipv4’

But just in the debug above I have:

`  
“ansible\_default\_ipv4”: {  
“address”: “10.0.137.195”,  
“alias”: “eth0”,  
“gateway”: “10.0.137.1”,  
“interface”: “eth0”,  
“macaddress”: “0e:ab:ba:38:c4:a1”,  
“mtu”: 9001,  
“netmask”: “255.255.255.0”,  
“network”: “10.0.137.0”,  
“type”: “ether”  
},

`

( I used to reference the hostvars like hostvars[item].ansible\_default\_ipv4.address but tried using just out of frustration. )

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [June 17, 2015, 10:35am UTC](https://forum.ansible.com/t/having-troubles-accessing-hostvars-for-new-host/18529/2 "2015-06-17T10:35:30Z")

</div>

Oh, I should have mentioned, this only happens when I have created the ec2 instances in the same play run. On a second run I don’t ever seem to get this error.

And this is on 1.9.2

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 18, 2015, 4:08am UTC](https://forum.ansible.com/t/having-troubles-accessing-hostvars-for-new-host/18529/3 "2015-06-18T04:08:03Z")

</div>

did you gather facts after creating the instances?

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [June 18, 2015, 4:22am UTC](https://forum.ansible.com/t/having-troubles-accessing-hostvars-for-new-host/18529/4 "2015-06-18T04:22:57Z")

</div>

Yes, the play explicitly has gather\_facts:true. The debug just above the failure clearly shows the attribute is present, that’s what is confusing.

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 18, 2015, 5:29am UTC](https://forum.ansible.com/t/having-troubles-accessing-hostvars-for-new-host/18529/5 "2015-06-18T05:29:34Z")

</div>

gather\_facts happens before other tasks, my question was if you  
gathered facts AFTER creating the new hosts

---

<div class="post-metadata">

**Author:** ![Barry\_Kaplan](https://avatars.discourse-cdn.com/v4/letter/b/48db29/32.png) [@Barry\_Kaplan](https://forum.ansible.com/u/Barry_Kaplan)\
**Post date:** [June 18, 2015, 4:30pm UTC](https://forum.ansible.com/t/having-troubles-accessing-hostvars-for-new-host/18529/6 "2015-06-18T16:30:23Z")

</div>

The flow is:

provision.yml:------------------

`

- name: Provision rabbitmq cluster  
hosts: localhost  
connection: local  
gather\_facts: yes  
tags: [provision, rabbitmq\_cluster]

tasks:

- name: Create rabbitmq-server instance  
ec2:  
…

- include: …/…/configure/rabbitmq-server.yml  
tags: [configure, rabbitmq\_cluster]  
`

rabbitmq-server.yml

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [June 18, 2015, 4:49pm UTC](https://forum.ansible.com/t/having-troubles-accessing-hostvars-for-new-host/18529/7 "2015-06-18T16:49:25Z")

</div>

So the new hosts don't get added to the rabbitmq\_server group?

I assume this will happen automatically next time the inventory script  
runs (if tagged), but that won't happen until you run ansible again,  
normally you use add\_hosts to temporarily add them for the 'current  
run'.
