采购后没有设置环境变量。使用 Ansible 和 shell 进行 Vagrant 配置
No environment variables are set after sourcing. Vagrant provisioning with Ansible and shell
我使用 Ansible 将 /etc/bash.bashrc
和 $HOME/.bashrc
附加到变量。
Ansible 完成后,这两个文件如下所示。
在/etc/bash.bashrc
中:
...
export VAR1="some text comes here"
...
在$HOME/.bashrc
中:
...
export VAR2="other text comes here"
...
所以,Ansible 完美地完成了它的工作。
Vagrantfile 中的 shell 脚本,在 Ansible 完成后运行:
$init = <<-SCRIPT
whoami
echo $HOME
cat /etc/bash.bashrc
cat $HOME/.bashrc
source /etc/bash.bashrc
source $HOME/.bashrc
echo "VAR1 :: $VAR1"
echo "VAR2 :: $VAR2"
SCRIPT
config.vm.provision :init, type: :shell, inline: $init, privileged: false
即使在两个文件中设置了两个变量,然后对两个文件进行了源输出也是:
vagrant
/home/vagrant
---> /etc/bash.bashrc contains the export
---> $HOME/.bashrc contains the export as well
VAR1 ::
VAR2 ::
如我们所见,两个变量未设置并提供空字符串。
我用$HOME/.profile
试过了,结果一样。
你知道如何进行吗?
/etc/bash.bashrc
和 source $HOME/.bashrc
包含变量赋值是不够的。变量赋值一定要有实际执行的机会。
如果您查看这两个脚本的源代码(下面的示例来自 Debian),它们包含在非交互式 shell:
中调用时中断执行的行
/etc/bash.bashrc
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
$HOME/.bashrc
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
因此,即使您明确获取这些文件的来源,如果您在这些条件之后插入行 export VAR1="some text comes here"
或 export VAR2="other text comes here"
,它们将永远不会被执行,因为配置脚本以非交互式方式运行 shell.
您实际上并没有展示如何插入变量赋值行,而是说您“追加”它们。如果您使用 Ansible 的 lineinfile
模块,您可以添加 insertbefore
参数以确保在脚本退出之前放置这些行。
我使用 Ansible 将 /etc/bash.bashrc
和 $HOME/.bashrc
附加到变量。
Ansible 完成后,这两个文件如下所示。
在/etc/bash.bashrc
中:
...
export VAR1="some text comes here"
...
在$HOME/.bashrc
中:
...
export VAR2="other text comes here"
...
所以,Ansible 完美地完成了它的工作。
Vagrantfile 中的 shell 脚本,在 Ansible 完成后运行:
$init = <<-SCRIPT
whoami
echo $HOME
cat /etc/bash.bashrc
cat $HOME/.bashrc
source /etc/bash.bashrc
source $HOME/.bashrc
echo "VAR1 :: $VAR1"
echo "VAR2 :: $VAR2"
SCRIPT
config.vm.provision :init, type: :shell, inline: $init, privileged: false
即使在两个文件中设置了两个变量,然后对两个文件进行了源输出也是:
vagrant
/home/vagrant
---> /etc/bash.bashrc contains the export
---> $HOME/.bashrc contains the export as well
VAR1 ::
VAR2 ::
如我们所见,两个变量未设置并提供空字符串。
我用$HOME/.profile
试过了,结果一样。
你知道如何进行吗?
/etc/bash.bashrc
和 source $HOME/.bashrc
包含变量赋值是不够的。变量赋值一定要有实际执行的机会。
如果您查看这两个脚本的源代码(下面的示例来自 Debian),它们包含在非交互式 shell:
中调用时中断执行的行/etc/bash.bashrc
# If not running interactively, don't do anything [ -z "$PS1" ] && return
$HOME/.bashrc
# If not running interactively, don't do anything case $- in *i*) ;; *) return;; esac
因此,即使您明确获取这些文件的来源,如果您在这些条件之后插入行 export VAR1="some text comes here"
或 export VAR2="other text comes here"
,它们将永远不会被执行,因为配置脚本以非交互式方式运行 shell.
您实际上并没有展示如何插入变量赋值行,而是说您“追加”它们。如果您使用 Ansible 的 lineinfile
模块,您可以添加 insertbefore
参数以确保在脚本退出之前放置这些行。