# Calling a dictionary based on an existing variable

**URL:** <https://forum.ansible.com/t/calling-a-dictionary-based-on-an-existing-variable/23729>\
**Category:** Ansible Project\
**Created:** [February 14, 2017, 7:13pm UTC](https://forum.ansible.com/t/calling-a-dictionary-based-on-an-existing-variable/23729 "2017-02-14T19:13:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicholas\_Santiago](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@Nicholas\_Santiago](https://forum.ansible.com/u/Nicholas_Santiago)\
**Post date:** [February 14, 2017, 7:13pm UTC](https://forum.ansible.com/t/calling-a-dictionary-based-on-an-existing-variable/23729/1 "2017-02-14T19:13:05Z")

</div>

I have a playbook (ldapconfig.yml) with a variables file (ldapconfig\_vars.yml), and am hoping to use an inventory var to specify which dictionary of the below to call.

So, if env=PVS, it calls the ldapserver\_pvs dictionary, if env=PROD, it calls the ldapserver\_prod dict, and so on.

> ldapserver\_pvs:
> 
> ldapbindDN: cn=serviceaccount,ou=specialAccounts,o=ldap.company.loc
> 
> ldaphost: p-ldap.sys.company.loc
> 
> ldapSSLport: 1234
> 
> ldapport: 5678
> 
> ldapserver\_stage:
> 
> ldapbindDN: cn=serviceaccount,ou=specialAccounts,o=ldap.company.loc
> 
> ldaphost: s-ldap.sys.company.loc
> 
> ldapSSLport: 1234
> 
> ldapport: 5678
> 
> ldapserver\_prod:
> 
> ldapbindDN: cn=serviceaccount,ou=specialAccounts,o=ldap.company.loc
> 
> ldaphost: ldap.sys.company.loc
> 
> ldapSSLport: 1234
> 
> ldapport: 5678

I can’t seem to find a clean way to do so, any suggestions on best practices to proceed?

Nick

---

<div class="post-metadata">

**Author:** ![Stankovic\_Marko](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Stankovic\_Marko](https://forum.ansible.com/u/Stankovic_Marko)\
**Post date:** [February 17, 2017, 11:56am UTC](https://forum.ansible.com/t/calling-a-dictionary-based-on-an-existing-variable/23729/2 "2017-02-17T11:56:11Z")

</div>

Hi,

How about having three files in vars directory (eg. pvs\_vars.yml, stage\_vars.yml, prod\_vars.yml) with the same variable in it (ldapserver) and using include\_vars module this way:  
- include\_vars: "{{ env }}\_vars.yml"

More info:  
[http://docs.ansible.com/ansible/include\_vars\_module.html](http://docs.ansible.com/ansible/include_vars_module.html)

Cheers,  
Marko  
CONFIDENTIALITY NOTICE: This message is the property of International Game Technology PLC and/or its subsidiaries and may contain proprietary, confidential or trade secret information. This message is intended solely for the use of the addressee. If you are not the intended recipient and have received this message in error, please delete this message from your system. Any unauthorized reading, distribution, copying, or other use of this message or its attachments is strictly prohibited.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Santiago](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@Nicholas\_Santiago](https://forum.ansible.com/u/Nicholas_Santiago)\
**Post date:** [February 20, 2017, 4:34pm UTC](https://forum.ansible.com/t/calling-a-dictionary-based-on-an-existing-variable/23729/3 "2017-02-20T16:34:47Z")

</div>

Hi, Marko –

> How about having three files in vars directory (eg. pvs\_vars.yml, stage\_vars.yml, prod\_vars.yml) with the same variable in it (ldapserver) and using include\_vars module this way:
> 
> - include\_vars: “{{ env }}\_vars.yml”

That sounds like a considerably easier and more sane approach. Thanks for bringing it up!

Nick
