/home/zuul/src/opendev.org/openstack/openstack-ansible-rsyslog_client/tasks/rsyslog_client_post_install.yml
---
# Copyright 2014, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: Rsyslog Setup
  template:
    src: "50-default.conf"
    dest: "/etc/rsyslog.d/50-default.conf"
    owner: "root"
    group: "root"
  when: (groups['rsyslog_all'] is not defined) or (inventory_hostname not in groups['rsyslog_all'])
  tags:
    - rsyslog_client-install
  notify:
    - restart rsyslog

- name: Check if log dir exists
  stat:
    path: "{{ rsyslog_client_log_dir }}"
  when:
    - "rsyslog_client_log_dir is defined"
  register: log_dir
  tags:
    - rsyslog_client-config

- name: Find all log files
  find:
    paths: "{{ rsyslog_client_log_dir }}"
    patterns: "*.log"
    recurse: yes
    follow: yes
    file_type: file
  register: log_files
  changed_when: false
  when:
    - not (log_dir is skipped)
  tags:
    - rsyslog_client-config

# The log files to handle/rotate are the files you can find
# in your log folder - the ones already defined for your
# distribution + the ones you decide to do manually
- name: Union the log files
  set_fact:
    rsyslog_client_all_log_files: "{{ log_files.files | map(attribute='path') | list | default([]) | union(rsyslog_client_log_files) | difference(rsyslog_client_already_handled_files) | list }}"
  when:
    - rsyslog_client_log_dir is defined
  tags:
    - rsyslog_client-config

- name: Set rsyslog_client_all_log_files when log_files does not exist
  set_fact:
    rsyslog_client_all_log_files: "{{ rsyslog_client_log_files | difference(rsyslog_client_already_handled_files) | list }}"
  when:
    - rsyslog_client_log_dir is not defined

- name: Write rsyslog config for found log files
  template:
    src: "rsyslog.conf.j2"
    dest: "/etc/rsyslog.conf"
    owner: "root"
    group: "root"
  when: (groups['rsyslog_all'] is not defined) or (inventory_hostname not in groups['rsyslog_all'])
  tags:
    - rsyslog_client-config
  notify:
    - restart rsyslog

- 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 }}"
    owner: "root"
    group: "root"
  when: (rsyslog_client_all_log_files | length) > 0
  tags:
    - rsyslog_client-config
  notify:
    - restart rsyslog

- name: Write log rotate file
  template:
    src: "logrotate.j2"
    dest: "/etc/logrotate.d/{{ rsyslog_client_log_rotate_file }}"
    owner: "root"
    group: "root"
  when: (rsyslog_client_all_log_files | length) > 0
  tags:
    - rsyslog_client-config

- name: Configure logrotate to compress logs by default
  lineinfile:
    dest: /etc/logrotate.conf
    regexp: "^#compress"
    line: "compress"
    backrefs: yes
  tags:
    - rsyslog_client-install