# How to update host variables

**URL:** <https://forum.ansible.com/t/how-to-update-host-variables/17303>\
**Category:** Ansible Project\
**Created:** [January 18, 2015, 12:51am UTC](https://forum.ansible.com/t/how-to-update-host-variables/17303 "2015-01-18T00:51:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Mooshammer](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@Peter\_Mooshammer](https://forum.ansible.com/u/Peter_Mooshammer)\
**Post date:** [January 18, 2015, 12:51am UTC](https://forum.ansible.com/t/how-to-update-host-variables/17303/1 "2015-01-18T00:51:29Z")

</div>

Hi there,

I do the following on gce:

- name: Launch instances  
gce:  
instance\_names: abc  
network: dfe  
machine\_type: “{{ machine\_type }}”  
image: “{{ image }}”  
zone: “{{ zone }}”  
tags: test  
register: gce

- name: run python script to make ip static  
script: python script that may or may not change the ip  
register: res  
failed\_when: “res.rc == 1”  
changed\_when: “res.rc == 0”  
with\_items: gce.instance\_data

- name: Add instance to local host group  
local\_action: lineinfile dest=hosts insertafter=“[gce]” line=“{{ item.public\_ip }} ansible\_ssh\_private\_key\_file=~/.ssh/google\_compute\_engine” state=present  
with\_items: gce.instance\_data

However, the public\_ip may have changed so how can I update it before I try to add the item to the “hosts” file?

thanks

Peter

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [January 19, 2015, 12:57am UTC](https://forum.ansible.com/t/how-to-update-host-variables/17303/2 "2015-01-19T00:57:39Z")

</div>

Have you looked into using the GCE dynamic inventory?

[http://docs.ansible.com/guide\_gce.html#gce-dynamic-inventory](http://docs.ansible.com/guide_gce.html#gce-dynamic-inventory)

---

<div class="post-metadata">

**Author:** ![Peter\_Mooshammer](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@Peter\_Mooshammer](https://forum.ansible.com/u/Peter_Mooshammer)\
**Post date:** [January 19, 2015, 5:35am UTC](https://forum.ansible.com/t/how-to-update-host-variables/17303/3 "2015-01-19T05:35:08Z")

</div>

I wanted to write a play that sets up and provisions a node in one sweep, so I thought add\_host is necessary (In the example I showed the lineinfile, which isn’t really necessary.

But this is also a more general question, How can I update host information.

thanks

Peter

---

<div class="post-metadata">

**Author:** ![Dag\_Wieers](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Dag\_Wieers](https://forum.ansible.com/u/Dag_Wieers)\
**Post date:** [January 19, 2015, 4:10pm UTC](https://forum.ansible.com/t/how-to-update-host-variables/17303/4 "2015-01-19T16:10:58Z")

</div>

Simply run the "setup" module again, eg:

&nbsp;&nbsp;- name: Update host facts  
&nbsp;&nbsp;&nbsp;&nbsp;action: setup

This will only work if the changes have been activated.

---

<div class="post-metadata">

**Author:** ![Peter\_Mooshammer](https://avatars.discourse-cdn.com/v4/letter/p/c5a1d2/32.png) [@Peter\_Mooshammer](https://forum.ansible.com/u/Peter_Mooshammer)\
**Post date:** [January 20, 2015, 9:05pm UTC](https://forum.ansible.com/t/how-to-update-host-variables/17303/5 "2015-01-20T21:05:02Z")

</div>

Hello Dag,

thank you for your input. I added the step you mentioned to my role. But it will not update the IP of the instance and therefore the next step (ssh) times out.  
Note: although there are loops, I only run with 1 instance

thanks and any other idea would reatly appreciated

Peter

- name: Launch instances  
gce:  
instance\_names: abc  
network: dfe  
machine\_type: “{{ machine\_type }}”  
image: “{{ image }}”  
zone: “{{ zone }}”  
tags: test  
register: gce

- name: run python script to make ip static  
script: python script that may or may not change the ip  
register: res  
failed\_when: “res.rc == 1”  
changed\_when: “res.rc == 0”  
with\_items: gce.instance\_data

- name: Update host facts  
action: setup

- name: Wait for SSH to come up  
wait\_for: host={{ item.public\_ip }} port=22 delay=10 timeout=60  
with\_items: gce.instance\_data
