简体   繁体   English

如何在Digital Ocean的云配置文件中设置系统范围的环境变量

[英]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不支持环境变量的内置资源类型。

  1. 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或用户的配置文件等。

  1. Another way to do it would be to leverage Cloud Init's support for Chef, and use that tool to set the variables when the profile is applied.另一种方法是利用 Cloud Init 对 Chef 的支持,并在应用配置文件时使用该工具设置变量

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.

相关问题 如何在Digital Ocean中设置环境变量? - How to set up environment variables in Digital Ocean? 为Digital Ocean上的Rails应用程序保留环境变量 - Persist environment variable for a Rails app on Digital Ocean 如何在 .NET Core 中设置全局环境变量(用户范围或系统范围) - How to set a global environment variable in .NET Core (user-wide or system-wide) 如何在 Digital Ocean 应用程序中添加当前 git 提交 hash 作为环境变量 - How to add the current git commit hash as an environment variable in a Digital Ocean app 如何在docker容器中的文件中设置dockerfile中的系统范围的环境变量? - How to set system-wide environment variables in dockerfile from a file inside the docker container? 如何在codeigniter中的config.php文件中设置环境变量 - how to set environment variable in config.php file in codeigniter NSIS - 如何在系统变量中设置环境变量 - NSIS - How to set an environment variable in system variable 如何在C#中设置系统环境变量? - How to set system environment variable in C#? 无法在php-fpm中访问系统范围的环境变量 - Unable to access system-wide environment variable in php-fpm 如何在 spring 测试中设置环境变量或系统属性? - How to set environment variable or system property in spring tests?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM