---
# SPDX-License-Identifier: GPL-3.0-only

- name: Install/Update required packages
  package:
    name: "{{ __rsyslog_packages }}"
    state: present
  when:
    - __rsyslog_packages | length > 0
    - not rsyslog_in_image | bool
  notify: Restart rsyslogd

- name: Generate role configuration files in rsyslog.d
  template:
    src: 'rules.conf.j2'
    dest: '{{ inner_item.path |
      d(__rsyslog_config_dir) }}/{{ inner_item.filename | d((inner_item.weight
      if inner_item.weight | d() else
      rsyslog_weight_map[inner_item.type | d("rules")]) +
      "-" + (inner_item.name | d("rules")) + "." +
      (inner_item.suffix | d("conf"))) }}'
    owner: '{{ inner_item.owner | d("root") }}'
    group: '{{ inner_item.group | d("root") }}'
    mode: '{{ inner_item.mode | d("0644") }}'
  loop: '{{ __rsyslog_rules | flatten }}'
  loop_control:
    loop_var: inner_item
  when:
    - __rsyslog_enabled | bool
    - inner_item.filename | d() or inner_item.name | d()
    - inner_item.options | d() or inner_item.sections | d()
    - inner_item.state is undefined or inner_item.state != 'absent'
  notify: Restart rsyslogd
  register: __rsyslog_deploy_templates

- name: Add deployed templates to global list
  set_fact:
    __rsyslog_template_results: "{{
      __rsyslog_template_results + [__rsyslog_deploy_templates] }}"

- name: Remove role config files from rsyslog.d
  file:
    path: '{{ inner_item.path |
      d(__rsyslog_config_dir) }}/{{ inner_item.filename | d((inner_item.weight
      if inner_item.weight | d() else
      rsyslog_weight_map[inner_item.type | d("rules")]) +
      "-" + (inner_item.name | d("rules")) + "." +
      (inner_item.suffix | d("conf"))) }}'
    state: absent
  loop: '{{ __rsyslog_rules | flatten }}'
  loop_control:
    loop_var: inner_item
  when:
    - (not __rsyslog_enabled | bool) or
      (inner_item.state | d('present') == 'absent')
    - inner_item.filename | d() or inner_item.name | d()
    - inner_item.options | d() or inner_item.sections | d()
  notify: Restart rsyslogd
