# YML 中的环境变量 - 仅替换为操作系统的变量

**URL:** https://meta.discourse.org/t/env-vars-in-yml-only-substituded-with-os-ones/280032
**Category:** Self-hosting
**Created:** [2023年九月25日 15:53 UTC](https://meta.discourse.org/t/env-vars-in-yml-only-substituded-with-os-ones/280032 "2023-09-25T15:53:26Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![lejeczek](https://avatars.discourse-cdn.com/v4/letter/l/edb3f5/32.png) [@lejeczek](https://meta.discourse.org/u/lejeczek)
#### Post date: [2023年九月25日 15:53 UTC](https://meta.discourse.org/t/env-vars-in-yml-only-substituded-with-os-ones/280032/1 "2023-09-25T15:53:26Z")

</div>

大家好。  
是否可以在_构建_时使用操作系统（bash）自己的参数/变量替换参数/变量？  
比如说……  
`host: /APPOW/log/${HOSTNAME}/discourse.test`  
我曾以为这会开箱即用，但对我来说不行。谢谢。

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [2023年九月25日 16:06 UTC](https://meta.discourse.org/t/env-vars-in-yml-only-substituded-with-os-ones/280032/2 "2023-09-25T16:06:30Z")

</div>

> [@Set Environmental Variables](https://meta.discourse.org/t/set-environmental-variables/270832):
>
> notebook_with_decorative_cover This is a #how-to guide for setting up environmental variables in a self-hosted Discourse installation. person_raising_hand Required user level: Administrator Environmental variables play a crucial role in configuring your Discourse instance. They can store sensitive data like API keys and database passwords, making your installation more secure and flexible. In Discourse, environmental variables are set in the app.yml file within your Docker container setup…

---

<div class="post-metadata">

### Author: ![lejeczek](https://avatars.discourse-cdn.com/v4/letter/l/edb3f5/32.png) [@lejeczek](https://meta.discourse.org/u/lejeczek)
#### Post date: [2023年九月25日 16:17 UTC](https://meta.discourse.org/t/env-vars-in-yml-only-substituded-with-os-ones/280032/3 "2023-09-25T16:17:38Z")

</div>

谢谢。不，除非我暂时瞎了——有可能——这并不包括我的问题。是吗？  
我说的是操作系统环境变量，YAML 中会解析为其值的 shell 变量——同样，在宿主机的 shell 中设置（在 .yml 文件中不存在）

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2023年九月25日 17:59 UTC](https://meta.discourse.org/t/env-vars-in-yml-only-substituded-with-os-ones/280032/4 "2023-09-25T17:59:01Z")

</div>

> [@lejeczek](#):
>
> 在_构建_时用操作系统（bash）自己的参数/变量进行替换？

不可以。

你可以在启动时设置它们，但不能在构建时设置。

你试图通过在构建时替换 YAML 中的 ENV 来实现什么目标？

---

<div class="post-metadata">

### Author: ![lejeczek](https://avatars.discourse-cdn.com/v4/letter/l/edb3f5/32.png) [@lejeczek](https://meta.discourse.org/u/lejeczek)
#### Post date: [2023年九月25日 18:06 UTC](https://meta.discourse.org/t/env-vars-in-yml-only-substituded-with-os-ones/280032/5 "2023-09-25T18:06:22Z")

</div>

这些小的_调整_在扩展时非常有用，即使只是微小的扩展。  
上面那个例子应该最能说明问题——无需每次在不同环境/不同主机上 **构建** 时都在 **yaml** 中进行“修复”。  
我搜索了一下，看到一些例子声称这种简单的替换应该适用于 YAML，但我无法使其生效。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2023年九月25日 18:09 UTC](https://meta.discourse.org/t/env-vars-in-yml-only-substituded-with-os-ones/280032/6 "2023-09-25T18:09:09Z")

</div>

我不认为启动器会替换那些环境变量。可以构建一个镜像，然后使用不同的域名多次启动同一个镜像。你可以运行

```
./launcher start-cmd app

```

来查看启动需要哪些环境变量。

要在一个尚未引导的宿主机上执行此操作，你需要迁移数据库并预编译资源。

---

<div class="post-metadata">

### Author: ![lejeczek](https://avatars.discourse-cdn.com/v4/letter/l/edb3f5/32.png) [@lejeczek](https://meta.discourse.org/u/lejeczek)
#### Post date: [2023年九月25日 18:23 UTC](https://meta.discourse.org/t/env-vars-in-yml-only-substituded-with-os-ones/280032/7 "2023-09-25T18:23:51Z")

</div>

如果能那样的话，那绝对会是未来启动器版本的一个非常好的增强功能。
