0

I'm having hard time figuring out how to iterate through dicts using a for to build a template-file (Jinja2) for rsyslog config. The scenarios is that I have a centralized rsyslog server that accepts logs from various rsyslog clients.

I've built a default dicts file in this manner:

rsyslog_client_groups:
  caprica: [ '"10.5.10.111"' , '"10.5.10.112"', '"10.5.10.11"', '"10.5.10.12"', '"10.5.12.21"' ]
   apache: [ '"10.5.13.24"' , '"10.5.13.25"' , '"10.5.13.26"' ]


rsyslog_client_destinations:
 apache:
   - var: '$syslogtag'
     contains: 'apache-access:'
     destination: '/logs/web/access.log'
   - var: '$syslogtag'
     contains: 'apache-error:'
     destination: '/logs/web/error.log'
 caprica:
    - destination: '/logs/mail/mail.log'

I need to write a template-file (using for) combining dicts rsyslog_client_groups and rsyslog_client_destinations so that the rsyslog config file ends up getting created this way on the centralized log server:

if $fromhost-ip == [ "10.5.10.111", "10.5.10.112", "10.5.10.11", "10.5.10.12", "10.5.12.21" ] then /logs/mail/mail.log
& stop

if $fromhost-ip == [ "10.5.13.24", "10.5.13.25", "10.5.13.26" ] then {
  if $syslogtag contains 'apache-access:' then /logs/web/access.log
  & stop
  else if $syslogtag contains 'apache-error:' then /logs/web/error.log
  & stop
  else /logs/web/other.log
  & stop
}

UPDATE:

The below code is closest to the task I've come:

{% for k, v in syslog_clients.iteritems() %}
if $fromhost-ip == {{ v }} then /logs/{{ k }}/{{ k }}.log
& stop
{% endfor %}

This doesn't achieve the end result as I've not been able to set the proper destination for logs from rsyslog_client_groups according to contains string that needs to be matched (in rsyslog_client_destinations)

0 Answers0