Passing a fact to another play

Imagine I have multiple hadoop clusters, each with a master node and a varying amount of slave nodes connected to it.
I want to dynamically discover the slaves from the master, and then perform actions on them in serial mode (only impact one node at a time to minimize downtime).

What’s the right way to do this?

An almost-working approach is below, but I have a problem at “xxxxx”. If I hardcode a master node name there, everything works. But I don’t want to since that’s silly and
there are lots of clusters. But I don’t know how I can pass the master node name across the plays?

  • hosts : hadoop_master_nodes # run this on all master nodes
    tasks:

  • name: find targets
    shell: list_all_my_hadoop_nodes.sh # show list of nodes connected to this master
    register: target_nodes

  • set_fact:
    target_nodes: “{{ target_nodes.stdout_lines }}” # set a fact so we can call it from the next step

  • hosts: “{{ hostvars[‘xxxxxxxx’][‘target_nodes’] }}” # call the list of target_nodes discovered above
    serial: 1
    tasks:

  • name: do whatever # “do whatever” one at a time across all the target nodes

Hi,

this scenario looks like the one I addressed in my pull request : https://github.com/ansible/ansible/pull/20849

Passing the ‘xxxxx’ value as an ‘–extra-var’ value is an option, but still requires the value to be known upfront
and hence will not solve your problem.
That’s why I created a module where a play can set an ‘extra variable’ with a runtime discovered value. Since the
‘extra variable’ is accessible thru all plays, this is a way to pass information between plays.

The pull request is waiting on community review, so if you like, go ahead…

Pieter.

Cool, I’ll take a look at that. In the meantime I think I have found a workaround, using add_host to create a group and then using that in my second hosts: bit. Seems to work, though it feels hackey.