我想知道Ansible是否有办法访问本地环境变量.
该文档引用了访问目标机器上的变量:
{{ lookup('env', 'SOMEVAR') }}
Run Code Online (Sandbox Code Playgroud)
有没有办法在源计算机上访问环境变量?
gri*_*lix 90
我在osx上运行了一个Linux vm,对我来说:
lookup('env', 'HOME')返回"/ Users/Gonzalo"(HOME来自osx 的变量),同时ansible_env.HOME返回"/ root"(HOME来自vm 的变量).
值得一提的是,ansible_env.VAR如果变量不存在则lookup('env', 'VAR')失败,而不会失败.
Quy*_*uan 19
使用ansible lookup:
- set_fact: env_var="{{ lookup('env','ENV_VAR') }}"
Run Code Online (Sandbox Code Playgroud)
那些变量在管理机器中,我认为是你机器中的源机器.
检查一下:http://docs.ansible.com/faq.html#how-do-i-access-shell-environment-variables
它说: If you just need to access existing variables, use the ‘env’ lookup plugin. For example, to access the value of the HOME environment variable on management machine:
现在,如果您需要在远程计算机中访问它,您只需在远程计算机中本地运行ansible脚本即可.或者你可以只是ansible事实变量.如果它不在ansible事实中,你可以运行一个shell命令来获取它.