ansiblerackspace

Can you set variables on the machine that runs ansible, not the host?


(Be forewarned: total newbie question follows):

I am currently debugging a playbook that creates two new hosts, removes two old hosts from a load balancer, then adds the two new hosts to the load balancer. We have separated many of these plays into their own playbook files and included them in a top-level play.

I want to be able to set a list of the two new hosts earlier in one play and have it persist on the machine running ansible between these nested plays, so that later on I can run a play that grabs the list of the old hosts currently in the load balancer, then use the list of the new hosts I set previously to add them to the LB, then use the list of old hosts to remove them from the LB. This is, as far as I can tell, the best way to do this, since Rackspace's rax_clb_nodes module makes changes to the load balancer from localhost.

A.) Is this the best way to achieve this?

B.) What is the best way to do this?


Solution

  • Facts set by set_fact are persistent between Plays.
    So you can set fact for localhost and then use it later as {{ hostvars['localhost']['my_custom_fact'] }}.

    Please read about Magic Variables – e.g. if you put your new hosts into separate inventory group, you can loop through this group.