# how to access variables set on target env

**URL:** <https://forum.ansible.com/t/how-to-access-variables-set-on-target-env/23476>\
**Category:** Ansible Project\
**Created:** [January 17, 2017, 12:56pm UTC](https://forum.ansible.com/t/how-to-access-variables-set-on-target-env/23476 "2017-01-17T12:56:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anand\_Dasari](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Anand\_Dasari](https://forum.ansible.com/u/Anand_Dasari)\
**Post date:** [January 17, 2017, 12:56pm UTC](https://forum.ansible.com/t/how-to-access-variables-set-on-target-env/23476/1 "2017-01-17T12:56:03Z")

</div>

Hi

i have a shell script which is setWLSEnv.sh on target machine, i run this first to set up web logic env  
after that i run the command  
java weblogic.WLST {{ pyhton file}}  
now i get Error: could not find or load main class web logic.WLST  
why i’m loosing the env variable set in previous command by running setWLSEnv.sh ?  
my code is like this ,can somebody help me please

pre\_tasks:

- name: set up web logic env  
command: /usr/bin/sh /path/setWLSEnv.sh

roles:  
role: …/roles/deploy\_app

deploy\_app role contains this

command: "java weblogic.WLST {{python file }}

---

<div class="post-metadata">

**Author:** ![Rui\_Alves](https://avatars.discourse-cdn.com/v4/letter/r/9d8465/32.png) [@Rui\_Alves](https://forum.ansible.com/u/Rui_Alves)\
**Post date:** [January 18, 2017, 12:25am UTC](https://forum.ansible.com/t/how-to-access-variables-set-on-target-env/23476/2 "2017-01-18T00:25:07Z")

</div>

Hi,

The shell variables set in the setWLSEnv.sh are not persisted between ansible tasks. You can try execute both scripts in the same shell:

- shell: source /setWLSEnv.sh && java weblogic.WLST {{ python file }}

Regards,

Rui Alves

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 18, 2017, 8:34am UTC](https://forum.ansible.com/t/how-to-access-variables-set-on-target-env/23476/3 "2017-01-18T08:34:46Z")

</div>

Each task has its own environment, so what your setting by running a shell script is lost afterwards, hence your error.

You can define environment variables for a task: [http://docs.ansible.com/ansible/playbooks\_environment.html](http://docs.ansible.com/ansible/playbooks_environment.html)

If that setWLSEnv.sh contains many vars then store all of them in one ansible var for easy reuse and less clutter.

Dick

---

<div class="post-metadata">

**Author:** ![Johannes\_Kastl](https://avatars.discourse-cdn.com/v4/letter/j/bbe5ce/32.png) [@Johannes\_Kastl](https://forum.ansible.com/u/Johannes_Kastl)\
**Post date:** [January 18, 2017, 10:01am UTC](https://forum.ansible.com/t/how-to-access-variables-set-on-target-env/23476/4 "2017-01-18T10:01:16Z")

</div>

I would suggest setting the variables in the host\_vars for the target  
host. Maybe even encrypted with ansible\_vault, if the contain sensible  
information.

Johannes
