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