【发布时间】:2016-05-17 02:19:05
【问题描述】:
我在让我的用户数据脚本读取在脚本执行之前设置的环境变量时遇到问题。我正在使用 Vagrant 进行测试。
所以我的 Vagrantfile 中有这一行...
config.vm.provision "shell", path: "bin/vagrant/build.sh"
... 指向配置脚本。该脚本包含加载 cloud-init 所需的所有内容,如here 所述。
在build.sh里面,我有一条线……
echo "SOME_PATH=/some/path" >> /etc/environment
...这可能使环境变量全局可用。
完整的文件,如果你有兴趣:
echo "SOME_PATH=/some/path" >> /etc/environment
# Check to see if we have done this already.
if [ -f /.vagrant_build_done ]; then
echo "Found, not running."
exit
fi
# Make the box think it hasn't init-ed yet.
rm -rf /var/lib/cloud/instance/*
rm -rf /var/lib/cloud/seed/nocloud-net/user-data
# Seed our own init scripts
cat << 'END_OF_FILE_CONTENTS' > /var/lib/cloud/seed/nocloud-net/user-data
Content-Type: multipart/mixed; boundary="===============apiserversStackMultipartMessage=="
MIME-Version: 1.0
# Beginning of our user-data script.
--===============apiserversStackMultipartMessage==
#include
/vagrant/bin/vagrant/user-data.sh
--===============apiserversStackMultipartMessage==--
END_OF_FILE_CONTENTS
# End of our user-data script.
# Re-run cloud-init.
cloud-init init
cloud-init modules --mode init
cloud-init modules --mode config
cloud-init modules --mode final
# Do not let this run again.
touch /.vagrant_build_done
现在,我有一条在/vagrant/bin/vagrant/user-data.sh 中回显SOME_PATH 的行,就像这样...
#!/bin/bash
echo $SOME_PATH
...当我运行vagrant up 时,什么都没有打印出来!
有没有办法让 user-data.sh 中的环境变量可用?
【问题讨论】:
标签: vagrant environment-variables user-data cloud-init