Passing a fact to another play

25 views
Skip to first unread message

tentacle_bonzoid

unread,
Feb 22, 2017, 4:50:52 AM2/22/17
to Ansible Project
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
      .....

Pieter Voet

unread,
Feb 22, 2017, 6:26:07 AM2/22/17
to Ansible Project
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.



tentacle_bonzoid

unread,
Feb 22, 2017, 3:38:25 PM2/22/17
to Ansible Project

On Wednesday, February 22, 2017 at 12:26:07 PM UTC+1, Pieter Voet wrote:

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


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.
 
Reply all
Reply to author
Forward
0 new messages