Write dynmaic variables in variable file and load into playbooks

788 views
Skip to first unread message

Python Guru

unread,
Dec 6, 2017, 6:13:12 AM12/6/17
to Ansible Project
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.

Kai Stian Olstad

unread,
Dec 6, 2017, 6:50:39 AM12/6/17
to ansible...@googlegroups.com
On 06.12.2017 12:13, Python Guru wrote:
> 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
---
var: firstname
var1: '{{ var }}_lastname'


test.yml
---
- hosts: localhost
tasks:
- include_vars: vars.yml
- debug: var=var
- debug: var=var1


$ ansible-playbook test.yml

PLAY [localhost] ******************

TASK [include_vars] ***************
ok: [localhost]

TASK [debug] **********************
ok: [localhost] => {
"var": "firstname"
}

TASK [debug] **********************
ok: [localhost] => {
"var1": "firstname_lastname"
}


--
Kai Stian Olstad

Python Guru

unread,
Dec 6, 2017, 11:35:48 AM12/6/17
to Ansible Project
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:   
   ---
   -
  hosts: machines
  vars:
      TARGET_SID: "APPSERVER"
  tasks:
    -
      include_vars:
        file: target_master.yml
        name: stuff

    -
      debug:
        msg: "{{stuff.var2}}"

 I cannot access variables like the above mentioned way, though your way is working but i am unable to load all variables into a different ansible variable,and then 
 want to extract using that variable name.

 I am getting the below error:
 
TASK [debug] *************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: {u'name': u'Appserver', u'var2': u'{{ name }}_var2'}: 'name' is undefined\n\nThe error appears to have been in '/root/anisble_examples/ebs/my.yml': line 13, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n    -\n      debug:\n      ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: {u'name': u'Appserver', u'var2': u'{{ name }}_var2'}: 'name' is undefined"}
        to retry, use: --limit @/root/anisble_examples/ebs/my.retry

Kai Stian Olstad

unread,
Dec 8, 2017, 3:15:34 AM12/8/17
to ansible...@googlegroups.com
It looks like it changes behavior when name: is used on include_vars.

- debug: var=stuff
This will fail

- 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:.


--
Kai Stian Olstad

Python Guru

unread,
Dec 11, 2017, 12:31:48 AM12/11/17
to ansible...@googlegroups.com
so, how can i achieve the above thing then.

-
  hosts: machines
  tasks:
    -
      debug:
        msg: local
    -
      include_vars: target_master.yml
      name: variables
    -
      debug:
        msg: variables['name']

Doing this not working for me


--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/q7H_dY1B-v0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/3916834.UNl1ugis9t%40x1.
For more options, visit https://groups.google.com/d/optout.



--


Thanks & Regards,
Python Guru
Hyderabad

Reply all
Reply to author
Forward
0 new messages