How to update host variables

101 views
Skip to first unread message

Peter Mooshammer

unread,
Jan 17, 2015, 7:51:30 PM1/17/15
to ansible...@googlegroups.com
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 

James Martin

unread,
Jan 18, 2015, 7:57:39 PM1/18/15
to ansible...@googlegroups.com
Have you looked into using the GCE dynamic inventory?

http://docs.ansible.com/guide_gce.html#gce-dynamic-inventory
Message has been deleted

Peter Mooshammer

unread,
Jan 19, 2015, 12:35:08 AM1/19/15
to ansible...@googlegroups.com


On Sunday, January 18, 2015 at 4:57:39 PM UTC-8, James Martin wrote:
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

Dag Wieers

unread,
Jan 19, 2015, 11:11:08 AM1/19/15
to ansible...@googlegroups.com
Simply run the "setup" module again, eg:

- name: Update host facts
action: setup

This will only work if the changes have been activated.

--
Dag

Peter Mooshammer

unread,
Jan 20, 2015, 4:05:02 PM1/20/15
to ansible...@googlegroups.com
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
Reply all
Reply to author
Forward
0 new messages