6

I am having problems working with the environment variables of a remote host. For example, when I try {{ lookup('env', 'PATH') }} this returns the path of my guest machine not of the remote host.

How to pick up / change environment variables of the remote host?

my playbook :

---
- name : playbook
  hosts : webservers
  gather_facts: yes
  remote_user: user1
  vars:
   Path: "{{lookup('ansible_env','PATH')}}"
  roles :
 - task1
 - task2 
 - task3 

that's return the path of my machine not the path of remote host named user1 i'm a beginner in ansible need some help . thank you in advance.

mndhr
  • 1,409
  • 5
  • 13
  • 17
  • Can you edit your question to show your task, inventory and how you are calling Ansible? – ydaetskcoR Mar 28 '16 at 12:18
  • my play-book contain only those lines : --- - name: Set Path for java command: bash -c "export $JAVA_HOME/bin:$PATH" – mndhr Mar 28 '16 at 12:31
  • 1
    See the answer here: http://stackoverflow.com/questions/27733511/how-to-set-linux-environment-variables-with-ansible/27736434#27736434 – Michal Gasek Mar 28 '16 at 17:08

1 Answers1

8

You can set the PATH for a task or a playbook by using the environment keyword.

environment:
  PATH: "{{ ansible_env.PATH }}:/thingy/bin"
  SOME: value

The Ansible FAQ mentions this near the top http://docs.ansible.com/ansible/faq.html

So in your case try something like the following:

- name: Set Path for java
  environment:
    PATH: "$JAVA_HOME/bin:{{ ansible_env.PATH }}"

Setting the environment reference: http://docs.ansible.com/ansible/playbooks_environment.html

wazy
  • 1,065
  • 1
  • 10
  • 23
  • to work for example with the PATH of the remote host named "vagrant" .i write environment: PATH: "{{ ansible_env.PATH }}:/vagrant/bin" ?? @wazy – mndhr Mar 29 '16 at 10:48
  • That would add /vagrant/bin to the PATH on the hosts you have specified this task under. – wazy Mar 30 '16 at 18:19
  • i itried to use "{{ ansible_env.PATH }}" but it return the PATH of my machine . i want to have the PATH of the remote host mentioned in the file /etc/ansible/hosts not the Path of my machine in which i installed ansible . – mndhr Mar 31 '16 at 09:09
  • Are you running the tasks under a play that specifies your remote host? Please show your play. – wazy Mar 31 '16 at 14:14
  • i changed my post .in which you can see my playbook – mndhr Apr 04 '16 at 11:23