# .profile

**URL:** <https://forum.ansible.com/t/profile/21561>\
**Category:** Ansible Project\
**Created:** [May 25, 2016, 11:06am UTC](https://forum.ansible.com/t/profile/21561 "2016-05-25T11:06:23Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [May 25, 2016, 11:06am UTC](https://forum.ansible.com/t/profile/21561/1 "2016-05-25T11:06:23Z")

</div>

Hello,

is it possible to run the .profile on the target server to be able to run a shell using these env variables ?

example:  
the JAVA\_HOME variable is set in the .profile on my target environment.

When running the playbook with:

- name: run .profile  
shell: ksh .profile  
args:  
chdir: ~

- name: Demarrage du server openhr  
shell: /bin/ksh dispatcher.sh

the error message is: JAVA\_HOME is not set.

Thank you

---

<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:** [May 25, 2016, 11:13am UTC](https://forum.ansible.com/t/profile/21561/2 "2016-05-25T11:13:18Z")

</div>

I think that is what setting the environment is for. Ansible uses an  
non-interactive shell.

From the docs:  
- hosts: all  
&nbsp;&nbsp;remote\_user: root  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- apt: name=cobbler state=installed  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;environment:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http\_proxy: [http://proxy.example.com:8080](http://proxy.example.com:8080)

[https://docs.ansible.com/ansible/playbooks\_environment.html](https://docs.ansible.com/ansible/playbooks_environment.html)

Johannes

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [May 25, 2016, 11:27am UTC](https://forum.ansible.com/t/profile/21561/3 "2016-05-25T11:27:29Z")

</div>

thank you but my understanding is that environment is only to set proxy.

---

<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:** [May 25, 2016, 11:40am UTC](https://forum.ansible.com/t/profile/21561/4 "2016-05-25T11:40:20Z")

</div>

IMHO no. Why not have a try?

Johannes

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [May 25, 2016, 12:10pm UTC](https://forum.ansible.com/t/profile/21561/5 "2016-05-25T12:10:24Z")

</div>

What shall I try ?  
I don’t understand how to add in the environment th eexecution of the .profile on the remote server ?  
I am a newby with ansible, I have not a clue to do so.

Thank you

---

<div class="post-metadata">

**Author:** ![Werner\_Flamme](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@Werner\_Flamme](https://forum.ansible.com/u/Werner_Flamme)\
**Post date:** [May 25, 2016, 12:44pm UTC](https://forum.ansible.com/t/profile/21561/6 "2016-05-25T12:44:48Z")

</div>

fanvalt [25.05.2016 14:10]:

> What shall I try ?  
> I don't understand how to add in the environment th eexecution of the  
> .profile on the remote server ?  
> I am a newby with ansible, I have not a clue to do so.

You can write a shell script that consists of  
- the setting of the environment variable  
- the desired action

For example, this script can contain three lines:

#!/bin/ksh  
. /root/.profile  
/usr/local/bin/dispatcher.sh

You call this script instead of dispatcher.sh directly, like

- name: Demarrage du server openhr  
&nbsp;&nbsp;shell: /bin/ksh /usr/local/bin/mynewscript.sh

Of course you should adapt all file names and paths according to your  
system 🙂

HTH,  
Werner

---

<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:** [May 25, 2016, 1:06pm UTC](https://forum.ansible.com/t/profile/21561/7 "2016-05-25T13:06:30Z")

</div>

> What shall I try ?  
> I don't understand how to add in the environment th eexecution of the  
> .profile on the remote server ?

I would set the variables, that your .profile contains, in the  
environment section, something like this (untested):

- hosts: all  
&nbsp;&nbsp;remote\_user: root  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;- name: Demarrage du server openhr  
&nbsp;&nbsp;&nbsp;&nbsp;shell: /bin/ksh dispatcher.sh  
&nbsp;&nbsp;&nbsp;&nbsp;environment:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;JAVA\_HOME: /usr/bin/foobar/

> I am a newby with ansible, I have not a clue to do so.

I also have never set the environment with ansible, so we're both  
newbies...

Johannes

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [May 25, 2016, 2:23pm UTC](https://forum.ansible.com/t/profile/21561/8 "2016-05-25T14:23:21Z")

</div>

OK thank you but this set the environment variable on the server where the playbook is run, I need the environment variables set on the target server whe running shells because the .profile is not run when connected with ssh.  
So I cannot use your bypass solution

---

<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:** [May 25, 2016, 2:49pm UTC](https://forum.ansible.com/t/profile/21561/9 "2016-05-25T14:49:33Z")

</div>

Oh, I did not know the environment is set on the controller. Sorry.

I think Werner's solution is pretty nice.

Johannes

---

<div class="post-metadata">

**Author:** ![Paul\_Totterman](https://avatars.discourse-cdn.com/v4/letter/p/3ec8ea/32.png) [@Paul\_Totterman](https://forum.ansible.com/u/Paul_Totterman)\
**Post date:** [May 25, 2016, 8:10pm UTC](https://forum.ansible.com/t/profile/21561/10 "2016-05-25T20:10:22Z")

</div>

> You call this script instead of dispatcher.sh directly, like

And there’s even the script-module: [https://docs.ansible.com/ansible/script\_module.html](https://docs.ansible.com/ansible/script_module.html)

Cheers,  
Paul

---

<div class="post-metadata">

**Author:** ![fanvalt](https://avatars.discourse-cdn.com/v4/letter/f/2bfe46/32.png) [@fanvalt](https://forum.ansible.com/u/fanvalt)\
**Post date:** [May 26, 2016, 12:49pm UTC](https://forum.ansible.com/t/profile/21561/11 "2016-05-26T12:49:59Z")

</div>

Thank you,  
the bypass solution I am finally using is to copy the dispatcher.sh into a new file, then with the help of lineinfile, I insert after the #!/bin/ksh first line the call of the .profile and when it is done I do remove this temporary file:

- name: Copy du script dispatcher.sh  
shell: cp /{{ rep\_user }}/{{ openhrname }}/bin/dispatcher.sh /{{ rep\_user }}/{{ openhrname }}/bin/startdisp.sh

- lineinfile:  
dest: /{{ rep\_user }}/{{ openhrname }}/bin/startdisp.sh  
regexp: “^exit 0”  
insertafter: “#!/bin/ksh”  
line: “. ~/.profile”

the startdisp.sh script does start with these lines:  
#!/bin/ksh  
. ~/.profile

Regards
