Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get environment variables of remote host

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.

like image 419
mndhr Avatar asked Apr 04 '16 13:04

mndhr


People also ask

How do I get a list of environment variables?

To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.

How do you access the environment variables of target remote hosts in ansible?

Accessing the variables The environment variables of the remote servers can be accessed via 'facts'. There is a sublist called 'ansible_env' which has all the env variables inside it. Note that, the fact-gathering needs to be 'ON' if you want to access this. By default, it is set to true.

How do I see my environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables.


2 Answers

The behavior of the lookup function is documented explicitly:

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

There is a FAQ regarding access to remote environment variables:

Ansible 1.4 will also make remote environment variables available via facts in the ‘ansible_env’ variable:

{{ ansible_env.SOME_VARIABLE }}

Note that remote facts (like ansible_env) are only available if fact gathering is enabled (which is the default behavior of ansible, but can be disabled in the config file or in your playbooks).

If you want to modify the environment of the remote host, you again look to the documentation which describes the environment directive:

Ansible makes it easy for you to configure your environment by 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

These sets an environment variable for this specific task. It is not a persistent modification.

like image 52
larsks Avatar answered Sep 17 '22 16:09

larsks


According to the documentation here, you can't use lookup for remote machines, that keyword only works for the local machine.

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

like image 43
TriskalJM Avatar answered Sep 17 '22 16:09

TriskalJM