Using environment variable in mosquitto configuration file

Viewed 874

I am trying to set remote_client ID using a value from the system environment variables instead of hardcoding it in the mosquitto.conf configuration file.

Is it possible to pass it inline when starting mosquitto such as:

mosquitto -remote_clientid $envar

Or can I refer environment variables directly from inside the config file?

remote_clientid $envar
2 Answers

I have not tested this, but you can run mosquitto with -c CONFIGFILE, so if you don't want to change your actual mosquitto.conf on disk, you should be able to use a "process substitution" to pass a modified copy of your conf file like this in bash:

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

Note that this is a bash-ism so you need to be using bash shell.

I believe that the only way to do this currently is to programmatically edit mosquitto.conf (this project uses that approach to allow support for configuration via environment variables in docker).

There is no command line option to set remote_clientid and environment variables are not supported in the mosquitto.conf (but this may change - see this feature request).

Related