Is there a way to "access" to each host specific variables outside it's scope?

Hey all

I’m having difficulties to find out how to perform this task which hopefully will be simple:

I am running a playbook over a set of servers, then after a while I need to construct a command that will use variables from all the hosts involved in the playbook,
For example:

Let’s assume i have 5 applicationServers and one centralize configuration server. I would like to install a package on all of the 5 application servers, and then, configure all the application servers which were succeeded on the single configuration server. I need a way to construct a command that will hold all the servers that were involved in the first stage of the playbook.

-hosts: applicationServers (I have 5)
-name: install a package
yum: pkg=customPackage state=installed

-hosts: configurationServer (I have only 1)
-name: Configure all application servers that were involved in previous task
customAnsibleConfigurationModule servers={{ ??? }} args=“foo bar”

So hopefully my custom Ansible module will receive a list of servers that it has to configure.
Is there a way to “access” to each host specific variables outside it’s scope?

Thanks !
Uri

I’m not sure what you mean by scope here.

Did you mean access the variables defined for another host? If so, yes.

Let us know and we can proceed further with answers.

Hi Michael,

To be more precise, I need to install package X on servers A,B,C

And then configure A,B,C on different single configuration server, on one single command

Something like this:

  • hosts: WebServers

  • name: install something

custom_installation_module: serverName={{ serverName }} bladeName={{ bladeName }}

  • hosts: configurationServer

  • name: configure all servers

custom_configuration_module: bladeAndServerPairs={{ ??? }}

assume that this playbook run over 3 WebServers and one configuration server. Each server has serverName and bladeName variables configured on host_vars

Configuration server need to receive as a parameter all the pairs of blades and servers that custom_installation_module were succeeded installation in the first task.

The actual parameters needed by the custom configuration module would look something like: bladeAndServerPairs=blade_A,server_A;blade_B,server_B;blade_C;server_C

To sum up – I need to access the WebServers variables serverName and bladeName outside their scope, and I also need to receive a list that involves only the hosts which were succeeded in the first task where they were involved in

I hope this clarify my query,

Thanks in advance,

You could do something like have a hash/dictionary of the servers that went with the blades (or vice versa, if I got that backwards)

blade_map:

  • name: blade1
    attached_servers: [ a, b, c ]
  • name: blade2
    attached_servers: …

And then, do something like:

  • hosts: blades

tasks:

  • foo: xyz=1234

  • bar: blade={{ item }}
    with_items: blade_map[inventory_hostname]
    delegate_to: item

Which uses the loop of one group to move across the other.

You can of course access variables for another host at any time like:

{{ hostvars[hostname][variable_name] }}

See http://docs.ansible.com/playbooks_variables.html#id33

Hope this helps!