如何获取远程主机的环境变量 [英] How to get environment variables of remote host

查看:781
本文介绍了如何获取远程主机的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理远程主机的环境变量时遇到问题。例如,当我尝试{{lookup('env','PATH')}}这将返回我的客机不是远程主机的路径。



接收/更改远程主机的环境变量?



我的Playbook:

  --- 
- 名称:playbook
主机:webservers
gather_facts:是
remote_user:user1
vars:
路径:{ {lookup('ansible_env','PATH')}}
roles:
- task1
- task2
- task3
/ pre>

这是返回我的机器的路径,而不是名为user1的远程主机的路径我是一个可以帮助的初学者需要一些帮助。

解决方案

lookup 函数的行为是明确记录


插件允许从外部来源访问Ansible中的数据。这些插件在Ansible控制机上进行评估...


有一个 FAQ


Ansible 1.4还将通过ansible_env变量中的事实使远程环境变量可用:

 code> {{ansible_env.SOME_VARIABLE}} 


注意如果启用了事件采集(这是ansible的默认行为,但可以在配置文件或您的Playbooks中禁用),那么远程事实(如 ansible_env )才可用。



如果要修改远程主机的环境,您再次查看 documentat描述环境指令:


可解释使它变得容易让您通过使用环境关键字来配置您的环境。以下是一个例子:

   - 主机:全部
remote_user:root

任务:

- apt:name = cobbler state = installed
environment:
http_proxy:http://proxy.example.com:8080


这些设置了一个特定任务的环境变量。它不是持续的修改。


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.

解决方案

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.

这篇关于如何获取远程主机的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆