[英]How to set System Wide Environment Variable in Cloud Config File on Digital Ocean
I am pretty new to setting up remote servers, but I was playing around today and was hoping that I could leverage a Cloud Config file upon setup in order to set a few environment variables as the server spins up.我对设置远程服务器很陌生,但我今天玩得很开心,希望我可以在设置时利用云配置文件,以便在服务器启动时设置一些环境变量。
How can I set my environment variables programmatically when spinning up a machine on Digital Ocean?在 Digital Ocean 上启动机器时,如何以编程方式设置环境变量? The key is that I want to automate the setup and avoid interactively defining these variables.关键是我想自动化设置并避免以交互方式定义这些变量。
Thanks in advance.提前致谢。
This is what I did with for ubuntu这就是我为 ubuntu 所做的
write_files:
- path: /etc/environment
content: |
FOO="BAR"
append: true
There's a couple ways to do this, although Cloud Init doesn't support a built-in resource type for environment variables.有几种方法可以做到这一点,尽管Cloud Init不支持环境变量的内置资源类型。
Depending on your OS, use a write-files
section to output the env vars you want to the appropriate file.根据您的操作系统,使用write-files
部分将您想要的环境变量输出到适当的文件。 For CoreOS, you'd do something like:对于 CoreOS,您可以执行以下操作:
write_files: - path: "/etc/profile.env" content: | export MY_VAR="foo"
For Ubuntu, use /etc/environment
, or a user's profile, etc.对于 Ubuntu,使用/etc/environment
或用户的配置文件等。
Do you need the environment variable to be permanent, or just for the execution of a single command/script?您需要环境变量是永久的,还是仅用于执行单个命令/脚本? If it's for a single command, you can do that:如果是单个命令,您可以这样做:
FOO=${BAR} | sh ./your_script.sh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.