Debian – set a multi-line environment variable in systemd’s EnvironmentFile

debiansystemd

I need an environment variable like this defined for my application's mail features to work:

RUBY_MAIL_SMTP_SETTINGS='
:address: "smtp.example.com"
:port: "25"
:authentication: :plain
:encryption: :tls
:user_name: "user"
:password: "pass"
'

Pasting that into a bash terminal works fine, but putting it into an environment file and referencing it from a systemd unit file with the EnvironmentFile option does not work:

juli 01 07:17:58 myserver systemd[1]: Ignoring invalid environment 'RUBY_MAIL_SMTP_SETTINGS=
juli 01 07:17:58 myserver systemd[1]: :address: "smtp.example.com"
juli 01 07:17:58 myserver systemd[1]: :port: "25"
juli 01 07:17:58 myserver systemd[1]: :authentication: :plain
juli 01 07:17:58 myserver systemd[1]: :encryption: :tls
juli 01 07:17:58 myserver systemd[1]: :user_name: "user"
juli 01 07:17:58 myserver systemd[1]: :password: "pass"': /usr/local/etc/myproject/environment

I have tried appending a backslash after each line, like this:

RUBY_MAIL_SMTP_SETTINGS='\
:address: "smtp.example.com"\
:port: "25"\
:authentication: :plain\
:encryption: :tls\
:user_name: "user"\
:password: "pass"
'

This works, but is interpreted as one long line, which means that the contents can't be parsed as YAML.

So does this mean it's impossible to add multi-lined environment variables to systemd environment files?

Best Answer

You need to quote whole variable assignment (not the value) AND backslash each line.

Example service file:

[Service]
Environment='RUBY_MAIL_SMTP_SETTINGS=\
:address: "smtp.example.com"\
:port: "25"\
:authentication: :plain\
:encryption: :tls\
:user_name: "user"\
:password: "pass"\
'

ExecStart=/bin/echo $RUBY_MAIL_SMTP_SETTINGS

Running this example will produce log:

Oct 27 14:03:32 io echo[9621]: :address: smtp.example.com :port: 25 :authentication: :plain :encryption: :tls :user_name: user :password: pass