.profile

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

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

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

IMHO no. Why not have a try?

Johannes

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

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

HTH,
Werner

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

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

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

I think Werner's solution is pretty nice.

Johannes

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

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