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

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

问题描述

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

如何获取/更改远程主机的环境变量?

我的剧本:

---- 名称:剧本主机:网络服务器收集事实:是的远程用户:用户 1变量:路径:{{lookup('ansible_env','PATH')}}"角色:- 任务1- 任务 2- 任务 3

那是返回我机器的路径而不是名为 user1 的远程主机的路径我是 ansible 的初学者需要一些帮助.提前致谢.

解决方案

lookup 函数的行为是 明确记录:

<块引用>

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

有一个常见问题解答 关于访问远程环境变量:

<块引用>

Ansible 1.4 还将通过ansible_env"变量中的事实提供远程环境变量:

{{ ansible_env.SOME_VARIABLE }}

请注意,远程事实(如 ansible_env 仅在启用事实收集时才可用(这是 ansible 的默认行为,但可以在配置文件或剧本中禁用).

如果您想修改远程主机的环境,您再次查看文档 描述了 environment 指令:

<块引用>

Ansible 使您可以使用environment"关键字轻松配置环境.下面是一个例子:

- 主机:所有远程用户:root任务:- apt:名称=鞋匠状态=已安装环境: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天全站免登陆