# Ability to use 'template' (or other modules) on nested vars?

**URL:** <https://forum.ansible.com/t/ability-to-use-template-or-other-modules-on-nested-vars/12683>\
**Category:** Ansible Project\
**Created:** [April 15, 2013, 12:08pm UTC](https://forum.ansible.com/t/ability-to-use-template-or-other-modules-on-nested-vars/12683 "2013-04-15T12:08:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![seanosh](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@seanosh](https://forum.ansible.com/u/seanosh)\
**Post date:** [April 15, 2013, 12:08pm UTC](https://forum.ansible.com/t/ability-to-use-template-or-other-modules-on-nested-vars/12683/1 "2013-04-15T12:08:26Z")

</div>

Hello all,

Given the following, let’s say part of a host\_vars or group\_vars definition file (YAML):

ssh\_listen\_port: 22  
other\_generic\_var: alpha  
sites:

- name: the\_domain  
repository: [git@github.com](mailto:git@github.com)…project.git  
instances:  
production:  
alpha: bravo  
charlie: 123  
db:  
database: the\_domain  
user: the\_domain  
pass: the\_domain\_db\_pw  
secrets:  
separate\_file\_a:  
secret1: ! ‘’  
secret2: ! ‘’  
separate\_file\_b:  
secret1: ! ‘’  
secret2: ! ‘’

- name: another\_domain  
…  
… and so on …

How can I use separate templates for writing out vars contained within ‘secrets’, or can I? Searched high and low, tried a variety of different things - it needs to write the “secrets” data to separate files (not a requirement of mine so I can’t get around it) and logically it makes sense, in the data structure, to keep it as a property of the “site” itself. I’m working on a custom module for it now, if that’s the best way to go then I’ve already got a head start (although my implementation around reading the data feels brittle), but wondering if there was an easier/better way…

Thanks!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [April 15, 2013, 1:25pm UTC](https://forum.ansible.com/t/ability-to-use-template-or-other-modules-on-nested-vars/12683/2 "2013-04-15T13:25:26Z")

</div>

“How can I use separate templates for writing out vars contained within ‘secrets’, or can I?”

You can in fact reference other variables that get evaluated lazily on demand.

For instance, assume you have:

vars\_files:

- common.yml
- /path/to/secrets/secret.yml

In common.yml you can actually reference variables in secret.yml

secrets:  
blarg: “{{ variable\_from\_other\_file }}”

Notice this works even if the type of variable\_from\_other\_file is not a basic string or number (you can include lists, even), but you’ll still have to quote it.

---

<div class="post-metadata">

**Author:** ![seanosh](https://avatars.discourse-cdn.com/v4/letter/s/9fc29f/32.png) [@seanosh](https://forum.ansible.com/u/seanosh)\
**Post date:** [April 15, 2013, 2:51pm UTC](https://forum.ansible.com/t/ability-to-use-template-or-other-modules-on-nested-vars/12683/3 "2013-04-15T14:51:51Z")

</div>

Hmm… essentially the idea then is to make the nested var a top level var? Thank you. That does put me a little bit closer, the only issue with that is that in order to refer to that variable (that exists within a list) I’d have to refer to it by index, e.g.:

# secrets.yml
