How to update host variables

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

Have you looked into using the GCE dynamic inventory?

http://docs.ansible.com/guide_gce.html#gce-dynamic-inventory

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

Simply run the "setup" module again, eg:

  - name: Update host facts
    action: setup

This will only work if the changes have been activated.

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