I'm building AMIs here for my organization which include default configuration to send all of our logs to Loggly from rsyslogd. It works pretty well, but I'd like to be able to customize the configuration from a cloud-init script to provide custom logging tags on a per-instance basis. I'd like to do something like this in cloud-init:
#cloud-config
write_files:
- path: /etc/sysconfig/rsyslog-loggly
content: |
LOGGLY_TAGS=staging,search,solr,staging-search-solr
And then read the environment variable/file into the rsyslog config:
$template LogglyFormat,"<%pri%>%protocol-version% %timestamp:::date-rfc3339% %HOSTNAME% %app-name% %procid% %msgid% [---TOKEN--- tag=\"$LOGGLY_TAGS\"] %msg%"
Is there a way to source an environment file or read an environment variable in an rsyslog config? I could always augment the SystemD unit for rsyslog to include an environment file, provided that I could read it.
What I'd like to avoid is doing something like this:
[Service]
...
ExecStartPre=/usr/local/sbin/rewrite-loggly-conf.py
and rewriting the config on every boot with some Python script.
Is there a way in rsyslog to do what I'm looking to accomplish?
Best Answer
Yes, you can include environment variables similarly to shell scripting by using backticks. This works since rsyslog 8.33.0
From String Constants in the rsyslog documentation: