防止 .bash_profile 的部分在 Ansible 连接时变为 运行
Prevent sections of .bash_profile from being run when Ansible connects
当我 运行 针对服务器的 Ansible 剧本时,它会打开一个新的 SSH 连接并执行操作。为此,我使用与交互式登录相同的帐户(我认为这里没有什么奇怪的)。
为了交互式登录,我向 .bash_profile
添加了一些代码以显示某些检查的结果。使用 Ansible,此代码在每个连接上获得 运行 花费不必要的时间(并在出现故障时将输出我的 .bash_profile
放入 Ansible 的结果中)。
Ansible中有没有原生的方法来阻止一段代码被执行?就像我可以在 .bash_profile
?
中检查的环境变量一样
Ansible 在非交互式 shell 模式下运行 bash
。
来自 man bash
(您的结果可能会有所不同...)
登录 shell 是参数零的第一个字符是 - 的登录,或者以 --login 选项开头的登录。
交互式 shell 是一种在没有非选项参数和 -c 选项的情况下启动的,其标准输入和错误都连接到终端(由 isatty(3) 确定),或者以 -i 选项启动。如果 bash 是交互式的,则 PS1 已设置并且 $- 包括 i,允许 shell 脚本或启动文件测试此状态。
您可以通过以下方式在目标系统上确认这一点:
$ echo "[=10=]"
-bash
一个解决方案是将类似下面 snippet/isoloation 的内容添加到您想要的登录 'rc' 文件中(不仅 .bash_profile
- 检查 what/which 文件的使用因人而异关于你的系统是如何配置的)
## is the first character a '-'? (as noted in the man page snip above)
if [[ ${0:0:1} == '-' ]]
then
echo running interactive
else
echo running non-interactive
fi
## nothing after this unless you want it in both interactive and non-interactive sessions
备选方案
检查 $-
的值:
$ echo "$-"
himBH
这里的i
代表interactive.
所以你可以通过以下方式测试$-
:
if [[ $- =~ i ]]; then
echo running interactive
fi
当我 运行 针对服务器的 Ansible 剧本时,它会打开一个新的 SSH 连接并执行操作。为此,我使用与交互式登录相同的帐户(我认为这里没有什么奇怪的)。
为了交互式登录,我向 .bash_profile
添加了一些代码以显示某些检查的结果。使用 Ansible,此代码在每个连接上获得 运行 花费不必要的时间(并在出现故障时将输出我的 .bash_profile
放入 Ansible 的结果中)。
Ansible中有没有原生的方法来阻止一段代码被执行?就像我可以在 .bash_profile
?
Ansible 在非交互式 shell 模式下运行 bash
。
来自 man bash
(您的结果可能会有所不同...)
登录 shell 是参数零的第一个字符是 - 的登录,或者以 --login 选项开头的登录。
交互式 shell 是一种在没有非选项参数和 -c 选项的情况下启动的,其标准输入和错误都连接到终端(由 isatty(3) 确定),或者以 -i 选项启动。如果 bash 是交互式的,则 PS1 已设置并且 $- 包括 i,允许 shell 脚本或启动文件测试此状态。
您可以通过以下方式在目标系统上确认这一点:
$ echo "[=10=]"
-bash
一个解决方案是将类似下面 snippet/isoloation 的内容添加到您想要的登录 'rc' 文件中(不仅 .bash_profile
- 检查 what/which 文件的使用因人而异关于你的系统是如何配置的)
## is the first character a '-'? (as noted in the man page snip above)
if [[ ${0:0:1} == '-' ]]
then
echo running interactive
else
echo running non-interactive
fi
## nothing after this unless you want it in both interactive and non-interactive sessions
备选方案
检查 $-
的值:
$ echo "$-"
himBH
这里的i
代表interactive.
所以你可以通过以下方式测试$-
:
if [[ $- =~ i ]]; then
echo running interactive
fi