Techniques in sharing variables between components of the same playbooks

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 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,

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.

Hello Dejay,

Thanks for the concern. Basically I want to access set_facts parameters set in - hosts: tag_Name_PersonalAppLiveD inside -hosts: localost.

Thanks