# Write dynmaic variables in variable file and load into playbooks

**URL:** <https://forum.ansible.com/t/write-dynmaic-variables-in-variable-file-and-load-into-playbooks/26212>\
**Category:** Ansible Project\
**Created:** [December 6, 2017, 11:13am UTC](https://forum.ansible.com/t/write-dynmaic-variables-in-variable-file-and-load-into-playbooks/26212 "2017-12-06T11:13:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Python\_Guru](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@Python\_Guru](https://forum.ansible.com/u/Python_Guru)\
**Post date:** [December 6, 2017, 11:13am UTC](https://forum.ansible.com/t/write-dynmaic-variables-in-variable-file-and-load-into-playbooks/26212/1 "2017-12-06T11:13:12Z")

</div>

Need suggestion on how to deal the below scenario, as i am working on a story about Oracle EBS clone,

for the Existing shell script project uses different conf files, where it exports the number of variables to linux environment to use it later dynamically in the scripts.

Ex:

target\_master.conf looks like this in the existing shell script project

export LSID=`echo ${TARGET_SID} | tr '[A-Z]' '[a-z]'`  
export **TARGET\_ORACLE\_HOME** =${DBBASEDIR}/base/product/oh1  
export **DBCONTEXTNAME** =${TARGET\_SID}\_${DBSERVER}  
export TNS\_ADMIN=${ **TARGET\_ORACLE\_HOME** }/network/admin/${ **DBCONTEXTNAME** }  
export INVENTORYDIR=${DBBASEDIR}/oraInventory

export DBFILEPATH=/u05/${LSID}/data

export LOGFILEPATH=/u02/${LSID}/redo

export ARCHLOGDEST=/u06/${LSID}/arc  
#AP

export APCONTEXTNAME=${TARGET\_SID}\_${APSERVER}  
export APINVDIR=${APBASEDIR}/oraInventory

The highlighted variables are exported and also used dynamically. As to store these variables, i  
thought of going with variable(ex.yml) files keeping under vars folder according to ansible standards,  
but in ansible how can i declare variables to get over this dynamic replacing of values.

I tried doing like below:

target\_master.yml

**TARGET\_ORACLE\_HOME** : {{DBBASEDIR}}/base/product/oh1

TNS\_ADMIN: {{ **TARGET\_ORACLE\_HOME}** } /network/admin/ {{ **DBCONTEXTNAME** }}

i tried googling it but not enough solutions to figure it out.

How can i make a n number of variables available for the ansible project and how can i dynamically assign  
as i was trying to do as above.

ex:

var: firstname

var1: {{ var }}\_lastname

The output of var1 should be like firstname\_lastname. will this be possible, if yes, please suggest me.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 6, 2017, 11:50am UTC](https://forum.ansible.com/t/write-dynmaic-variables-in-variable-file-and-load-into-playbooks/26212/2 "2017-12-06T11:50:01Z")

</div>

> Need suggestion on how to deal the below scenario, as i am working on a  
> story about Oracle EBS clone,  
> for the Existing shell script project uses different conf files, where it  
> exports the number of variables to linux environment to use it later  
> dynamically in the scripts.
> 
> Ex:  
> target\_master.conf looks like this in the existing shell script project
> 
> export LSID=`echo ${TARGET_SID} | tr '[A-Z]' '[a-z]'`  
> export \*TARGET\_ORACLE\_HOME\*=${DBBASEDIR}/base/product/oh1  
> export \*DBCONTEXTNAME\*=${TARGET\_SID}\_${DBSERVER}  
> export TNS\_ADMIN=${\*TARGET\_ORACLE\_HOME\*}/network/admin/${\*DBCONTEXTNAME\*}  
> export INVENTORYDIR=${DBBASEDIR}/oraInventory  
> export DBFILEPATH=/u05/${LSID}/data  
> export LOGFILEPATH=/u02/${LSID}/redo  
> export ARCHLOGDEST=/u06/${LSID}/arc  
> #AP  
> export APCONTEXTNAME=${TARGET\_SID}\_${APSERVER}  
> export APINVDIR=${APBASEDIR}/oraInventory
> 
> The highlighted variables are exported and also used dynamically. As to  
> store these variables, i  
> thought of going with variable(ex.yml) files keeping under vars folder  
> according to ansible standards,  
> but in ansible how can i declare variables to get over this dynamic  
> replacing of values.
> 
> I tried doing like below:
> 
> target\_master.yml
> 
> \*TARGET\_ORACLE\_HOME\*: {{DBBASEDIR}}/base/product/oh1  
> TNS\_ADMIN: {{\*TARGET\_ORACLE\_HOME}\*} /network/admin/ {{\*DBCONTEXTNAME\*}}

When { comes after a colon you need to put all of it in single or double quotes.

> i tried googling it but not enough solutions to figure it out.
> 
> How can i make a n number of variables available for the ansible project  
> and how can i dynamically assign  
> as i was trying to do as above.
> 
> ex:
> 
> var: firstname  
> var1: {{ var }}\_lastname
> 
> The output of var1 should be like firstname\_lastname. will this be  
> possible, if yes, please suggest me.

It's difficult to give any pointer since you have not provided any error messages or explaining what's not working  
By the way, this works:

vars.yml

---

<div class="post-metadata">

**Author:** ![Python\_Guru](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@Python\_Guru](https://forum.ansible.com/u/Python_Guru)\
**Post date:** [December 6, 2017, 4:35pm UTC](https://forum.ansible.com/t/write-dynmaic-variables-in-variable-file-and-load-into-playbooks/26212/3 "2017-12-06T16:35:48Z")

</div>

Hi,

Whatever you said is working and i’m happy to see that it’s working. but what if i want to load the variable file into a ansible variable and then start taking out each variable  
from the var file.

My Var file:

name: APPSERVER  
var2: ‘{{ name }}\_PROD’

My Playbook:

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 8, 2017, 8:15am UTC](https://forum.ansible.com/t/write-dynmaic-variables-in-variable-file-and-load-into-playbooks/26212/4 "2017-12-08T08:15:12Z")

</div>

It looks like it changes behavior when name: is used on include\_vars.

&nbsp;&nbsp;- debug: var=stuff  
This will fail

&nbsp;&nbsp;- debug: var=vars['stuff']  
This will work

Both of them should have worked so it must be a bug.

And when using name in the include\_vars the variables doesn't get expanded.  
I guess this is also a bug since it works without the name:.

---

<div class="post-metadata">

**Author:** ![Python\_Guru](https://avatars.discourse-cdn.com/v4/letter/p/8c91f0/32.png) [@Python\_Guru](https://forum.ansible.com/u/Python_Guru)\
**Post date:** [December 11, 2017, 5:31am UTC](https://forum.ansible.com/t/write-dynmaic-variables-in-variable-file-and-load-into-playbooks/26212/5 "2017-12-11T05:31:34Z")

</div>

so, how can i achieve the above thing then.
