在 mosquitto 配置文件中使用环境变量

Using environment variable in mosquitto configuration file

我正在尝试使用系统环境变量中的值设置 remote_client ID,而不是在 mosquitto.conf 配置文件中对其进行硬编码。

是否可以在启动 mosquitto 时内联传递它,例如:

mosquitto -remote_clientid $envar

或者我可以直接从配置文件中引用环境变量吗?

remote_clientid $envar

我认为目前执行此操作的唯一方法是以编程方式编辑 mosquitto.confthis project 使用该方法允许通过 docker 中的环境变量支持配置)。

没有命令行选项可以设置 remote_clientid,并且 mosquitto.conf 不支持环境变量(但这可能会改变 - 请参阅 this feature request)。

我没有测试过这个,但是你可以 运行 mosquitto-c CONFIGFILE,所以如果你不想改变磁盘上的实际 mosquitto.conf,你应该能够使用 "process substitution" 来传递你的 conf 文件的修改副本,就像这样 bash:

mosquitto -c <(sed "s/remote_clientid=.*/remote_clientid=$CONFIGVAR" /etc/mosquitto.conf)

注意这是一个 bash-ism 所以你需要使用 bash shell.