.profile

73 views
Skip to first unread message

fanvalt

unread,
May 25, 2016, 7:06:23 AM5/25/16
to Ansible Project
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

Johannes Kastl

unread,
May 25, 2016, 7:13:33 AM5/25/16
to ansible...@googlegroups.com
On 25.05.16 13:06 fanvalt wrote:
> Hello,
>
> is it possible to run the .profile on the target server to be able to run a
> shell using these env variables ?

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

From the docs:
- hosts: all
remote_user: root
tasks:
- apt: name=cobbler state=installed
environment:
http_proxy: http://proxy.example.com:8080

https://docs.ansible.com/ansible/playbooks_environment.html

Johannes

signature.asc

fanvalt

unread,
May 25, 2016, 7:27:29 AM5/25/16
to Ansible Project
thank you but my understanding is that environment is only to set proxy.

Johannes Kastl

unread,
May 25, 2016, 7:40:34 AM5/25/16
to ansible...@googlegroups.com
On 25.05.16 13:27 fanvalt wrote:
> thank you but my understanding is that environment is only to set proxy.

IMHO no. Why not have a try?

Johannes


signature.asc

fanvalt

unread,
May 25, 2016, 8:10:24 AM5/25/16
to Ansible Project
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

Johannes Kastl

unread,
May 25, 2016, 9:06:40 AM5/25/16
to ansible...@googlegroups.com
On 25.05.16 14:10 fanvalt wrote:
> 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
remote_user: root
tasks:
- name: Demarrage du server openhr
shell: /bin/ksh dispatcher.sh
environment:
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

signature.asc

Werner Flamme

unread,
May 25, 2016, 10:22:07 AM5/25/16
to ansible...@googlegroups.com
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
shell: /bin/ksh /usr/local/bin/mynewscript.sh

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

HTH,
Werner

--

fanvalt

unread,
May 25, 2016, 10:23:21 AM5/25/16
to Ansible Project
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

Johannes Kastl

unread,
May 25, 2016, 10:49:49 AM5/25/16
to ansible...@googlegroups.com
On 25.05.16 16:23 fanvalt wrote:
> 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.

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

I think Werner's solution is pretty nice.

Johannes

signature.asc

Paul Tötterman

unread,
May 25, 2016, 4:10:22 PM5/25/16
to Ansible Project
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

Cheers,
Paul 
 

fanvalt

unread,
May 26, 2016, 8:49:59 AM5/26/16
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages