How to get remote host environment variables

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

How to select / change remote host environment variables?

my playbook:

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

which return the path to my machine, not the path to the remote host named user1. I'm starting to need help. thanks in advance.

+7
linux yaml environment-variables ansible ansible-playbook
source share
2 answers

The behavior of the lookup function is explicitly described :

Plugins

allow access to data in Ansible from external sources. These plugins are evaluated on the Ansible control machine ...

Regarding access to remote environment variables, there is a FAQ :

Ansible 1.4 will also make remote environment variables available through the facts in the ansible_env variable:

 {{ ansible_env.SOME_VARIABLE }} 

Note that deleted facts (for example, ansible_env are only available if fact collection is enabled (which is the default behavior, but can be disabled in the configuration file or in your books).

If you want to change the remote host environment, you will again look at the documentation that describes the environment directive:

Ansible simplifies environment customization using the "environment" keyword. Here is an example:

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

They set the environment variable for this particular task. This is not a permanent modification.

+8
source share

According to the documentation here , you cannot use lookup for remote machines, this keyword only works for the local machine.

Instead, you want to use {{ ansible_env.PATH}} .

+1
source share

All Articles