Calling a dictionary based on an existing variable

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

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

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.

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