问题描述
我正在编写 Ansible playbook,以便在 Solaris 服务器上设置和安装我们的应用程序.
I am writing Ansible playbooks to setup and install our applications on Solaris servers.
问题是我需要执行的 (bash) 脚本都假设某个目录位于 PATH 上,即 /data/bin
- 如果它通常不会成为问题不适用于 Ansible 忽略所有 .profile
和 .bashrc
配置.
The problem is that the (bash) scripts which I need to execute all assume that a certain directory lies on the PATH, namely /data/bin
- which would normally not be a problem were it not for Ansible ignoring all the .profile
and .bashrc
config.
现在,我知道您可以通过 environment
标志为 shell
任务指定环境,例如:
Now, I know that you can specify the environment for shell
tasks via the environment
flag, for example like this:
- shell: printenv
environment:
PATH: /usr/bin:/usr/sbin:/data/bin
这将正确路径 /data/bin
文件夹,并且 printenv
命令将正确显示(或者我的 bash 脚本将正确运行).
This will properly path the /data/bin
folder, and the printenv
command will correctly display (or my bash scripts would correctly run).
但是.但是有两个问题:
- 首先,必须一遍又一遍地指定环境非常烦人.我知道您可以在某些剧本基本文件变量和引用中定义环境,但是您仍然必须在每个
上设置
任务.environment: ...
shell - 其次,上面的例子不允许我动态指定路径,例如作为
PATH: $PATH:/data/bin
- 因为 Ansible 以不解析$PATH
的方式执行此操作,因此该命令会灾难性地失败.所以基本上这将覆盖对PATH
的任何其他更改.
- First of all it is very annoying to have to specify the environment over and over again. I know that you can define the environment in some playbook base file variable and the reference that, but you still have to set
environment: ...
on every singleshell
task. - Secondly, the above example does not allow me to specify the path dynamically, e.g. as
PATH: $PATH:/data/bin
- because Ansible executes this in a way which does not resolve$PATH
, thus the command fails catastrophically. So essentially this will override any other changes toPATH
.
我正在寻找解决方案
- 额外的
PATH
条目应该只添加一次 - 附加的
PATH
条目不应覆盖其他任务添加的条目
- the additional
PATH
entry should only be added once - the additional
PATH
entry should not override entries added by other tasks
附言我在 如何在 Linux 上执行此操作,但它使用了 /etc/environment
而在 Solaris 上不存在.(并且 /etc/profile
再次被 Ansible 忽略.)
P.S. I found this nice explanation on how to do this on Linux, but it makes use of /etc/environment
which does not exist on Solaris. (And /etc/profile
is once again ignored by Ansible.)
推荐答案
尝试将 -o SendEnv=PATH
添加到 ssh_args
在 ansible.cfg.要求
try adding -o SendEnv=PATH
to ssh_args
in ansible.cfg. Requires that
- 运行 ansible 的 shell 在 PATH 中有/data/bin.或者,ansible 允许您修改当前/本地 PATH 变量.
- 远程机器已正确设置AcceptEnv.
这篇关于Ansible:如何为 solaris 全局设置 PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!