我在让我的用户数据脚本读取在脚本执行之前设置的环境变量时遇到问题。我正在使用 Vagrant 进行测试。
所以我在我的 Vagrantfile 中有这一行......
config.vm.provision "shell", path: "bin/vagrant/build.sh"
...指向配置脚本。该脚本包含加载 cloud-init 所需的所有内容,如此处所述。
在里面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
现在,我有一条线像这样SOME_PATH
在里面回响/vagrant/bin/vagrant/user-data.sh
......
#!/bin/bash
echo $SOME_PATH
...当我跑步时vagrant up
,什么都没有打印出来!
有没有办法让环境变量在里面可用user-data.sh
?