---
- name: Install pciutils
  apt:
    name: pciutils
    state: present

## Broadcom NetXtreme II

- name: Check if Broadcom NetXtreme II device is present
  shell: "lspci | grep -q 'NetXtreme II'"
  check_mode: no
  register: broadcom_netextreme_search
  failed_when: False
  changed_when: False

# TODO: add the "non-free" part to the existing sources
# instead of adding a new source

- name: Add non-free repo for Broadcom NetXtreme II
  include_role:
    name: evolix/apt
    tasks_from: basics.yml
  vars:
    apt_basics_components: "main contrib non-free"
  when: broadcom_netextreme_search.rc == 0

## RAID
# Dell and others: MegaRAID SAS
# HP gen <10: Hewlett-Packard Company Smart Array
# HP gen >=10: Adaptec Smart Storage PQI
- name: Detect if RAID is installed
  shell: "set -o pipefila && lspci -q | grep -e 'RAID bus controller' -e 'Serial Attached SCSI controller'"
  check_mode: no
  register: raidmodel
  changed_when: "'FAILED' in raidmodel.stdout"
  failed_when: "'FAILED' in raidmodel.stdout"

- name: HPE Smart Storage Administrator (ssacli) is present
  block:
    - name: HPE GPG embedded key is absent
      apt_key:
        id: "26C2B797"
        keyring: /etc/apt/trusted.gpg
        state: absent

    - name: HPE GPG key is installed
      copy:
        src: hpePublicKey2048_key1.asc
        dest: /etc/apt/trusted.gpg.d/hpePublicKey2048_key1.asc
        force: yes
        mode: "0644"
        owner: root
        group: root

    - name: Add HPE repository
      apt_repository:
        repo: 'deb https://downloads.linux.hpe.com/SDR/repo/mcp {{ ansible_distribution_release }}/current non-free'
        state: present
    - name: Install HPE Smart Storage Administrator (ssacli)
      apt:
        name: ssacli
  when:
    - "'Hewlett-Packard Company Smart Array' in raidmodel.stdout"
    - "'Adaptec Smart Storage PQI' in raidmodel.stdout"

# NOTE: check_hpraid cron use check_hpraid from nagios-nrpe role
# So, if nagios-nrpe role is not installed it will not work
- name: Install and configure check_hpraid cron (HP gen >=10)
  block:
    - name: check_hpraid cron is present (HP gen >=10)
      copy:
        src: check_hpraid.cron.sh
        dest: /etc/cron.{{ evolinux_cron_checkhpraid_frequency | mandatory }}/check_hpraid
        mode: "0755"
  when: "'Adaptec Smart Storage PQI' in raidmodel.stdout"

- name: Install and configure cciss-vol-status (HP gen <10)
  block:
    - name: Install cciss-vol-status (HP gen <10)
      apt:
        name: cciss-vol-status
        state: present

    - name: cciss-vol-statusd init script is present (HP gen <10)
      template:
        src: hardware/cciss-vol-statusd.j2
        dest: /etc/init.d/cciss-vol-statusd
        mode: "0755"

    - name: Configure cciss-vol-statusd (HP gen <10)
      lineinfile:
        dest: /etc/default/cciss-vol-statusd
        line: 'MAILTO="{{ raid_alert_email or general_alert_email | mandatory }}"'
        regexp: 'MAILTO='
        create: yes

    - name: Enable cciss-vol-status in systemd (HP gen <10)
      service:
        name: cciss-vol-statusd
        enabled: true
        state: restarted
  when: "'Hewlett-Packard Company Smart Array' in raidmodel.stdout"

- name: MegaRAID SAS package is present
  block:
    - name: HWRaid embedded GPG key is absent
      apt_key:
        id: "23B3D3B4"
        keyring: /etc/apt/trusted.gpg
        state: absent
      when: ansible_distribution_major_version is version('9', '>=')

    - name: HWRaid GPG key is installed
      copy:
        src: hwraid.le-vert.net.asc
        dest: /etc/apt/trusted.gpg.d/hwraid.le-vert.net.asc
        force: yes
        mode: "0644"
        owner: root
        group: root
      when: ansible_distribution_major_version is version('9', '>=')

    - name: Add HW tool repository
      apt_repository:
        repo: 'deb http://hwraid.le-vert.net/debian {{ ansible_distribution_release }} main'
        state: present

    - name: Install packages for DELL/LSI hardware
      apt:
        name:
          - megacli
          - megaclisas-status
        allow_unauthenticated: yes

    - name: Configure packages for DELL/LSI hardware
      template:
        src: hardware/megaclisas-statusd.j2
        dest: /etc/default/megaclisas-statusd
        mode: "0755"

    - name: Enable DELL/LSI hardware in systemd
      service:
        name: megaclisas-statusd
        enabled: true
        state: started
  when: "'MegaRAID SAS' in raidmodel.stdout"

- meta: flush_handlers
