简体   繁体   English

Ansible - 从远程主机读取环境变量

[英]Ansible - Reading Environment Variable from Remote Host

I am trying to read an Environment variable from Target Linux Host using Ansible playbook.我正在尝试使用 Ansible playbook 从目标 Linux 主机读取环境变量。 I tried all the below tasks as per the document but there is no result.我按照文档尝试了以下所有任务,但没有结果。

   - name: Test1    
     debug: msg="{{ ansible_env.BULK }}"
     delegate_to: "{{ target_host }}"

   - name: Test2  
     shell: echo $BULK
     delegate_to: "{{ target_host }}"
     register: foo

   - debug: msg="{{ foo.stdout }}"

   - name: Test3 
     debug: msg="{{ lookup('env','BULK')}} is an environment variable"
     delegate_to: "{{ target_host }}"

The Environment variable "BULK" is not set in the local Host where I am executing the playbook, so I assume its returning nothing.环境变量“BULK”未在我执行剧本的本地主机中设置,因此我假设它不返回任何内容。 Instead of BULK, if I use "HOME" which is always available, it returns the result.如果我使用始终可用的“HOME”而不是 BULK,它会返回结果。 If I SSH into the target_host I am able to run echo $BULK without any issue.如果我通过 SSH 连接到 target_host,我可以毫无问题地运行echo $BULK

How to obtain the Environment variable from the remote host?如何从远程主机获取环境变量?

If I SSH into the target_host I am able to run echo $BULK without any issue.如果我通过 SSH 连接到 target_host,我可以毫无问题地运行echo $BULK

Most likely, because BULK is set in one of the rc-files sourced only in an interactive session of the shell on the target machine.最有可能的是,因为BULK设置在仅在目标计算机上 shell 的交互式会话中来源的 rc 文件之一中。 And Ansible's gather_facts task runs in a non-interactive one. Ansible 的gather_facts任务在非交互式任务中运行。

How to obtain the Environment variable from the remote host?如何从远程主机获取环境变量?

Move the line setting the BULK variable to a place where it is sourced regardless of the session type (where exactly, depends on the target OS and shell)将设置BULK变量的行移动到它的来源位置,而不考虑会话类型(具体位置取决于目标操作系统和外壳)

See for example: https://unix.stackexchange.com/a/170499/133107 for hints.例如,请参阅: https : //unix.stackexchange.com/a/170499/133107以获取提示。

source /etc/profile and then grep the env source /etc/profile然后grep env

my solution is not perfect, but often works.我的解决方案并不完美,但通常有效。 for example i want to check if the remote_host has environment variables for proxy servers, i do the following:例如,我想检查 remote_host 是否具有代理服务器的环境变量,我执行以下操作:

as an adhoc ansible command:作为一个临时的 ansible 命令:

ansible remote_host -b -m shell -a '. /etc/profile && (env | grep -iP "proxy")'

Explanation:说明:

  • i prefere the shell module, it does what i expect... the same if i do it on a shell我更喜欢 shell 模块,它做我所期望的......如果我在 shell 上做也是一样
  • . /etc/profile . /etc/profile sources the /etc/profile. . /etc/profile是 /etc/profile 的来源。 And this file sources other files like under /etc/profile.d .这个文件来源其他文件,如 /etc/profile.d 下。 So after this i have the fix machine part of the environment.所以在这之后我有了环境的修复机器部分。
  • env | grep -iP "proxy" env | grep -iP "proxy" then filter the expanded environment for my variables i am looking for env | grep -iP "proxy"然后过滤我正在寻找的变量的扩展环境

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM