# Techniques in sharing variables between components of the same playbooks

**URL:** <https://forum.ansible.com/t/techniques-in-sharing-variables-between-components-of-the-same-playbooks/20972>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [March 23, 2016, 4:52pm UTC](https://forum.ansible.com/t/techniques-in-sharing-variables-between-components-of-the-same-playbooks/20972 "2016-03-23T16:52:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joseph\_DJOMEDA](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Joseph\_DJOMEDA](https://forum.ansible.com/u/Joseph_DJOMEDA)\
**Post date:** [March 23, 2016, 4:52pm UTC](https://forum.ansible.com/t/techniques-in-sharing-variables-between-components-of-the-same-playbooks/20972/1 "2016-03-23T16:52:16Z")

</div>

Hello Guys,

I have hit a major blocker probably because I have little knowledge on how to use special variables like hostvars and groupvars etc while using dynamic inventory especially with AWS.

I had a playbook for creating vpc, networking stuff, creating of various AWS services like RDS, EC2 , ELB etc and later run various nginx, tomcat,fail2ban etc roles to configure them. So I register some of the variables and use set\_facts on registered variables and get them later on hostvars for example at the end of the playbook when I am adding nodes to ELB for example. So it worked great etc.

This time around I had a constraint of having DBs, a mongo replicaset and RDS all offline, So ansible controller was launched inside AWS. I could run the same all-in-one playbook inside that new aws controller but I became adept of re-usability. So I split the playbook into logical sub files and only run the playbook that actually configures installs on nodes inside the aws controller.

That said, since I am using ec2.py file for dynamic inventory, I would not want to hard code anything line box private\_dns\_name for example so I use tag\_Name\_XXX to configure boxes with Name tags but have no clue how to get the private ips of those nodes in order to add them to the ELB.

for example:

- hosts: tag\_Name\_PersonalAppLiveD  
tasks:

- name: get facts metadata  
action: ec2\_facts  
register: app\_facts\_d

- name: setting facts for Personal AppLiveD for other playbooks component  
set\_fact:  
app\_facts\_d: “{{ app\_facts\_d }}” ## set facts so I can have info about it in order to use below

- hosts: localhost  
connection: local

- name: Putting Personal nodes under ELB  
ec2\_elb:  
ec2\_elbs: “{{ Personal\_elb[‘name’] }}”  
region: “{{ Personal\_vpc[‘vpc’][‘region’] }}”  
state: present  
wait: yes  
wait\_timeout: 400  
instance\_id: “{{ item }}”  
with\_items:

- “{{ hostvars[‘tag\_Name\_PersonalAppLiveB’][‘app\_facts\_b’][‘ansible\_facts’][‘ansible\_ec2\_ami\_id’] }}”

- “{{ hostvars[‘tag\_Name\_PersonalAppLiveC’][‘app\_facts\_c’][‘ansible\_facts’][‘ansible\_ec2\_ami\_id’] }}”

- “{{ hostvars[‘tag\_Name\_PersonalAppLiveD’][‘app\_facts\_d’][‘ansible\_facts’][‘ansible\_ec2\_ami\_id’] }}”

Full snippet is available at [pastebin.com](http://pastebin.com/RE8ZKZv9) but from this example, you would notice that I am treating tag name as host which should not be the case (got this understanding on the IRC chat) wanted to try use groupvars but I got stuck as I don’t know how to use it without actual inventory files.

Basically I would be grateful if someone could shed some light on how to get facts on these hosts groups we use “tag\_Name” for in the playbook so I can access them inside the ec2\_elb module.

Best Regards,

---

<div class="post-metadata">

**Author:** ![Dejay\_Clayton](https://avatars.discourse-cdn.com/v4/letter/d/6de8d8/32.png) [@Dejay\_Clayton](https://forum.ansible.com/u/Dejay_Clayton)\
**Post date:** [March 23, 2016, 7:04pm UTC](https://forum.ansible.com/t/techniques-in-sharing-variables-between-components-of-the-same-playbooks/20972/2 "2016-03-23T19:04:53Z")

</div>

In my opinion, you would have a much better chance of getting a decent reply to your question if you could reduce your example to the simplest possible playbook that attempts to accomplish your goal.

It makes my head spin that I have to learn about your entire infrastructure in order to even figure out what your question is.

---

<div class="post-metadata">

**Author:** ![Joseph\_DJOMEDA](https://avatars.discourse-cdn.com/v4/letter/j/13edae/32.png) [@Joseph\_DJOMEDA](https://forum.ansible.com/u/Joseph_DJOMEDA)\
**Post date:** [March 23, 2016, 9:52pm UTC](https://forum.ansible.com/t/techniques-in-sharing-variables-between-components-of-the-same-playbooks/20972/3 "2016-03-23T21:52:13Z")

</div>

Hello Dejay,

Thanks for the concern. Basically I want to access set\_facts parameters set in - hosts: tag\_Name\_PersonalAppLiveD inside -hosts: localost.

Thanks
