使用 ansible 检索目标节点的环境变量 [英] Retrieve env variable of target node using ansible

查看:31
本文介绍了使用 ansible 检索目标节点的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ansible 获取目标节点的 env variale JAVA_HOME 值.

I am trying to get env variale JAVA_HOME value of a target node using ansible.

- name: Copy JAVA_HOME location to variable
  command:  bash -c "echo $JAVA_HOME"
  sudo: yes
  register: java_loc

当我在另一个任务中使用 java_loc.stdout 值时,它显示空白值.我怎样才能获得那个 env 变量并在另一个任务中使用它?

When i use, java_loc.stdout value in another task, it is showing blank value. How can i get that env variable and use it in another task?

我需要将文件复制到 JAVA_HOME 中的 JAVA 目录.

I need to copy files to JAVA dir which is present in JAVA_HOME.

推荐答案

Ansible 使用非登录 shell 通过 SSH 登录.可能您的问题是您在 $HOME/.bash_profile 或其他需要登录 shell 的文件上定义了环境变量,因此您需要将-l"标志添加到/bin"/bash":

Ansible logins via SSH using non-login shell. Probably your problem is that you defined the environment variable on $HOME/.bash_profile or some other file that requires login shell, so you need to add the "-l" flag to "/bin/bash":

---
- name: Copy JAVA_HOME location to variable 
  command:  /bin/bash -l -c "echo $JAVA_HOME"    
  sudo: no                                  
  register: java_loc                        

- name: show debug                          
  debug: var=java_loc

请试一试并告诉我,

这篇关于使用 ansible 检索目标节点的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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