# variable expansion by template

**URL:** <https://forum.ansible.com/t/variable-expansion-by-template/11638>\
**Category:** Ansible Project\
**Created:** [July 2, 2012, 1:45pm UTC](https://forum.ansible.com/t/variable-expansion-by-template/11638 "2012-07-02T13:45:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ansibleguest](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@ansibleguest](https://forum.ansible.com/u/ansibleguest)\
**Post date:** [July 2, 2012, 1:45pm UTC](https://forum.ansible.com/t/variable-expansion-by-template/11638/1 "2012-07-02T13:45:28Z")

</div>

Hi,

not sure if this i found a bug or this is by intention with the  
changes in 0.5 (using latest devel on CentOS6.2):

cat test.yml

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 2, 2012, 1:50pm UTC](https://forum.ansible.com/t/variable-expansion-by-template/11638/2 "2012-07-02T13:50:01Z")

</div>

> Result:  
> cat /opt/basedir/testfile.txt brings unexpanded variables ☹

In the above example, it looks like you set a variable called “base” and are trying to use a variable called “basedir”.

Is this the case?

–Michael

---

<div class="post-metadata">

**Author:** ![ansibleguest](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@ansibleguest](https://forum.ansible.com/u/ansibleguest)\
**Post date:** [July 2, 2012, 2:29pm UTC](https://forum.ansible.com/t/variable-expansion-by-template/11638/3 "2012-07-02T14:29:13Z")

</div>

Oh, sorry. I corrected that to $basedir, but still it does not expand  
$basedir within the template.  
Probably has to do with accessing them in vars\_files when declared in  
vars before ?

&nbsp;&nbsp;vars:  
&nbsp;&nbsp;&nbsp;&nbsp;user: root  
&nbsp;&nbsp;&nbsp;&nbsp;group: root  
&nbsp;&nbsp;&nbsp;&nbsp;basedir: /opt/basedir

&nbsp;&nbsp;vars\_files:  
&nbsp;&nbsp;&nbsp;&nbsp;- vars/import.yml

&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- action: template src=templates/test\_template.j2 dest=$basedir/  
testfile.txt

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@Michael\_DeHaan](https://forum.ansible.com/u/Michael_DeHaan)\
**Post date:** [July 2, 2012, 2:40pm UTC](https://forum.ansible.com/t/variable-expansion-by-template/11638/4 "2012-07-02T14:40:50Z")

</div>

All the variable-reuse stuff is actually loaded up in the template code and shouldn’t care.

I don’t know, you’ll have to debug it and figure it out, I guess.

–Michael

---

<div class="post-metadata">

**Author:** ![ansibleguest](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@ansibleguest](https://forum.ansible.com/u/ansibleguest)\
**Post date:** [July 2, 2012, 3:29pm UTC](https://forum.ansible.com/t/variable-expansion-by-template/11638/5 "2012-07-02T15:29:44Z")

</div>

I can see it is expanded correctly:

/etc/ansible/setup

&nbsp;&nbsp;&nbsp;&nbsp;"config": [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Debug=0",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"LicenseDirectory=/opt/basedir/license",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"LogDirectory=/opt/basedir/log"  
&nbsp;&nbsp;&nbsp;&nbsp;],

So passing the array into the jinja-template then doesnt seam to work  
correctly:

If i use there {{ config }}  
i get out:  
['Debug=0', 'LicenseDirectory=$basedir/license', 'LogDirectory=  
$basedir/log']
