---
# yamllint disable rule:line-length
- name: Set ovirt platform/version specific variables
  include_tasks: "{{ role_path }}/tasks/inputs/ovirt/set_vars.yml"

# yamllint enable rule:line-length
# Deploy configuration files
- name: "Install/Update oVirt input packages and generate
    configuration files in /etc/rsyslog.d"
  vars:
    __rsyslog_packages: "{{ __rsyslog_ovirt_prereq_packages +
                            __rsyslog_ovirt_packages }}"
    __rsyslog_rules: "{{ __rsyslog_ovirt_rules }}"
  include_tasks:
    file: "{{ role_path }}/tasks/deploy.yml"

- name: "Create oVirt input configuration file
    /etc/rsyslog.d/input-ovirt-{{ __rsyslog_input.name }}"
  vars:
    __rsyslog_packages: []
    __rsyslog_rules:
      - name: "input-ovirt-{{ __rsyslog_input.name }}"
        type: input
        sections:
          - options: "{{ lookup('template', 'input_ovirt.j2') }}"
        state: "{{ __rsyslog_input.state | d('present') }}"
  include_tasks:
    file: "{{ role_path }}/tasks/deploy.yml"
  when:
    - __rsyslog_input.type | d('') == 'ovirt'
    - __rsyslog_input.subtype | d('') in ['collectd','engine','vdsm']

- name: Ensure collectd port is allowed for rsyslogd to listen to
  shell: |-
    set -euo pipefail
    sport={{ __rsyslog_input.rsyslog_read_collectd_port | d(44514) }}
    plist=$( semanage port -l | grep "$sport" || : )
    if [ "$plist" = "" ]; then
      semanage port -a -t syslogd_port_t -p tcp "$sport"
    else
      elems=()
      for pl in "$plist"
      do
        elems+=($pl)
      done
      if [ "${elems[0]}" != "syslogd_port_t" -o \
           "${elems[1]}" != "tcp" ]; then
        semanage port -a -t syslogd_port_t -p tcp "$sport"
      fi
    fi
  when:
    - __rsyslog_input.type | d() == 'ovirt'
    - ansible_selinux.mode in ["enforcing", "permissive"]
  notify: Restart rsyslogd
