From 705b264df29d7be4bca6f8dae5e9ba86b2757ffa Mon Sep 17 00:00:00 2001 From: Ravi Kumar Boyapati Date: Sun, 12 Mar 2017 23:07:45 -0400 Subject: [PATCH] Fix remote logging template Splitted functionality, one template for generating remote syslog targets and another for streaming the user specifed log files in syslog format. This avoids duplcate targets when the role is invoked mutiple times against a host when installing services. Change-Id: I53e2faf69c0446bea9738a66ee8585e8d795cf52 --- tasks/rsyslog_client_post_install.yml | 14 ++++++++- templates/99-rsyslog.conf.j2 | 44 -------------------------- templates/remote-logging.j2 | 45 +++++++++++++++++++++++++++ tests/test.yml | 18 ++++++++--- 4 files changed, 72 insertions(+), 49 deletions(-) create mode 100644 templates/remote-logging.j2 diff --git a/tasks/rsyslog_client_post_install.yml b/tasks/rsyslog_client_post_install.yml index 521055b..9aa91fa 100644 --- a/tasks/rsyslog_client_post_install.yml +++ b/tasks/rsyslog_client_post_install.yml @@ -74,7 +74,19 @@ notify: - restart rsyslog -- name: Write rsyslog target file +- name: Write rsyslog config for remote logging + template: + src: "remote-logging.j2" + dest: "/etc/rsyslog.d/51-remote-logging.conf" + owner: "root" + group: "root" + when: (rsyslog_client_all_log_files | length) > 0 + tags: + - rsyslog_client-config + notify: + - restart rsyslog + +- name: Write rsyslog config for converting logs into syslog messages template: src: "99-rsyslog.conf.j2" dest: "/etc/rsyslog.d/{{ rsyslog_client_config_name }}" diff --git a/templates/99-rsyslog.conf.j2 b/templates/99-rsyslog.conf.j2 index ca994e5..8bf5207 100644 --- a/templates/99-rsyslog.conf.j2 +++ b/templates/99-rsyslog.conf.j2 @@ -1,49 +1,5 @@ # {{ ansible_managed }} -$WorkDirectory {{ rsyslog_client_spool_directory }} -$template RFC3164fmt,"<%PRI%>%TIMESTAMP% %HOSTNAME% %syslogtag%%msg%" - -{% if groups['rsyslog_all'] is defined and groups['rsyslog_all'] %} -# Log shipment rsyslog target servers -{% for server in groups['rsyslog_all'] %} -$ActionQueueFileName {{ server }} -$ActionQueueSaveOnShutdown on -$ActionQueueType LinkedList -$ActionResumeRetryCount 250 -{% if rsyslog_client_tcp_reception == true %} -*.* @@{{ hostvars[server]['ansible_host'] }}:{{ rsyslog_client_tcp_port }};RFC3164fmt -{% endif %} -{% if rsyslog_client_udp_reception == true %} -*.* @{{ hostvars[server]['ansible_host'] }}:{{ rsyslog_client_udp_port }};RFC3164fmt -{% endif %} - -{% endfor %} -{% endif %} - -{% if rsyslog_client_user_defined_targets is defined %} -# Log shipment user defined target servers -{% for target in rsyslog_client_user_defined_targets %} -$ActionQueueFileName {{ target.name }} -$ActionQueueSaveOnShutdown on -$ActionQueueType LinkedList -$ActionResumeRetryCount 250 -{% if target.template is defined %} -{{ target.template }} -{% endif %} -{% if target.proto == "udp" %} -{% set action_sting = '*.* @' %} -{% elif target.proto == "tcp" %} -{% set action_sting = '*.* @@' %} -{% endif %} -{% if target.action_options is defined %} -{{ action_sting }}{{ target.hostname }}:{{ target.port }};{{ target.action_options }} -{% else %} -{{ action_sting }}{{ target.hostname }}:{{ target.port }} -{% endif %} - -{% endfor %} -{% endif %} - # Log files {% for log_file in rsyslog_client_all_log_files %} $InputFileName {{ log_file }} diff --git a/templates/remote-logging.j2 b/templates/remote-logging.j2 new file mode 100644 index 0000000..f8b5bb4 --- /dev/null +++ b/templates/remote-logging.j2 @@ -0,0 +1,45 @@ +# {{ ansible_managed }} + +$WorkDirectory {{ rsyslog_client_spool_directory }} +$template RFC3164fmt,"<%PRI%>%TIMESTAMP% %HOSTNAME% %syslogtag%%msg%" + +{% if groups['rsyslog_all'] is defined and groups['rsyslog_all'] %} +# Log shipment rsyslog target servers +{% for server in groups['rsyslog_all'] %} +$ActionQueueFileName {{ server }} +$ActionQueueSaveOnShutdown on +$ActionQueueType LinkedList +$ActionResumeRetryCount 250 +{% if rsyslog_client_tcp_reception == true %} +*.* @@{{ hostvars[server]['ansible_host'] }}:{{ rsyslog_client_tcp_port }};RFC3164fmt +{% endif %} +{% if rsyslog_client_udp_reception == true %} +*.* @{{ hostvars[server]['ansible_host'] }}:{{ rsyslog_client_udp_port }};RFC3164fmt +{% endif %} + +{% endfor %} +{% endif %} + +{% if rsyslog_client_user_defined_targets is defined %} +# Log shipment user defined target servers +{% for target in rsyslog_client_user_defined_targets %} +$ActionQueueFileName {{ target.name }} +$ActionQueueSaveOnShutdown on +$ActionQueueType LinkedList +$ActionResumeRetryCount 250 +{% if target.template is defined %} +{{ target.template }} +{% endif %} +{% if target.proto == "udp" %} +{% set action_sting = '*.* @' %} +{% elif target.proto == "tcp" %} +{% set action_sting = '*.* @@' %} +{% endif %} +{% if target.action_options is defined %} +{{ action_sting }}{{ target.hostname }}:{{ target.port }};{{ target.action_options }} +{% else %} +{{ action_sting }}{{ target.hostname }}:{{ target.port }} +{% endif %} + +{% endfor %} +{% endif %} diff --git a/tests/test.yml b/tests/test.yml index b2c20d4..b496e86 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -47,6 +47,10 @@ - nocreate - size 1G post_tasks: + - name: Open rsyslog remote logging file + slurp: + src: /etc/rsyslog.d/51-remote-logging.conf + register: rsyslog_remote_logging_file - name: Open rsyslog client file slurp: src: /etc/rsyslog.d/99-test-rsyslog-client.conf @@ -57,12 +61,17 @@ register: logrotate_client_file - name: Read files set_fact: + rsyslog_remote_logging_content: "{{ rsyslog_remote_logging_file.content | b64decode }}" rsyslog_client_content: "{{ rsyslog_test_client_file.content | b64decode }}" logrotate_client_content: "{{ logrotate_client_file.content | b64decode }}" - name: Check rsyslog defaults file stat: path: /etc/rsyslog.d/50-default.conf register: rsyslog_default_file + - name: Check rsyslog remote logging config file + stat: + path: /etc/rsyslog.d/51-remote-logging.conf + register: rsyslog_remote_logging_conf - name: Check rsyslog client file stat: path: /etc/rsyslog.d/99-test-rsyslog-client.conf @@ -74,10 +83,10 @@ - name: Check role functions assert: that: - - "'$ActionQueueFileName test1' in rsyslog_client_content" - - "'$ActionQueueFileName test2' in rsyslog_client_content" - - "'$ActionQueueFileName test-splunk1' in rsyslog_client_content" - - "'$ActionQueueFileName test-loggly1' in rsyslog_client_content" + - "'$ActionQueueFileName test1' in rsyslog_remote_logging_content" + - "'$ActionQueueFileName test2' in rsyslog_remote_logging_content" + - "'$ActionQueueFileName test-splunk1' in rsyslog_remote_logging_content" + - "'$ActionQueueFileName test-loggly1' in rsyslog_remote_logging_content" # check for log files that are explicitly defined in rsyslog_client_log_files - "'/var/log/dmesg' in logrotate_client_content" - "'/var/log/udev' in logrotate_client_content" @@ -88,5 +97,6 @@ or '/var/log/yum.log' in logrotate_client_content" - "'size 1G' in logrotate_client_content" - "rsyslog_default_file.stat.exists" + - "rsyslog_remote_logging_conf.stat.exists" - "rsyslog_client_file.stat.exists" - "logrotate_file.stat.exists"