Calling a dictionary based on an existing variable

31 views
Skip to first unread message

Nicholas Santiago

unread,
Feb 14, 2017, 2:13:05 PM2/14/17
to Ansible Project
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

Stankovic, Marko

unread,
Feb 17, 2017, 6:56:25 AM2/17/17
to Ansible Project
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.

Nicholas Santiago

unread,
Feb 20, 2017, 11:34:48 AM2/20/17
to Ansible Project, marko.s...@igt.com
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 
Reply all
Reply to author
Forward
0 new messages