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

**URL:** <https://forum.ansible.com/t/is-there-a-way-to-access-to-each-host-specific-variables-outside-its-scope/15331>\
**Category:** Ansible Developer\
**Created:** [May 26, 2014, 6:45am UTC](https://forum.ansible.com/t/is-there-a-way-to-access-to-each-host-specific-variables-outside-its-scope/15331 "2014-05-26T06:45:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Uri\_Miller](https://avatars.discourse-cdn.com/v4/letter/u/e99b99/32.png) [@Uri\_Miller](https://forum.ansible.com/u/Uri_Miller)\
**Post date:** [May 26, 2014, 6:45am UTC](https://forum.ansible.com/t/is-there-a-way-to-access-to-each-host-specific-variables-outside-its-scope/15331/1 "2014-05-26T06:45:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 27, 2014, 8:31pm UTC](https://forum.ansible.com/t/is-there-a-way-to-access-to-each-host-specific-variables-outside-its-scope/15331/2 "2014-05-27T20:31:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Uri\_Miller](https://avatars.discourse-cdn.com/v4/letter/u/e99b99/32.png) [@Uri\_Miller](https://forum.ansible.com/u/Uri_Miller)\
**Post date:** [May 28, 2014, 6:05am UTC](https://forum.ansible.com/t/is-there-a-way-to-access-to-each-host-specific-variables-outside-its-scope/15331/3 "2014-05-28T06:05:36Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 28, 2014, 1:33pm UTC](https://forum.ansible.com/t/is-there-a-way-to-access-to-each-host-specific-variables-outside-its-scope/15331/4 "2014-05-28T13:33:28Z")

</div>

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](http://docs.ansible.com/playbooks_variables.html#id33)

Hope this helps!
