---
- name: Install pciutils
  ansible.builtin.apt:
    name: pciutils
    state: present
  tags:
    - packages

- name: non-free-firmware components are installed (Debian 12+)
  ansible.builtin.replace:
    dest: /etc/apt/sources.list.d/system.sources
    regexp: '^(Components: ((?!\bnon-free-firmware\b).)*)$'
    replace: '\1 non-free-firmware'
  when:
    - ansible_distribution_major_version is version('12', '>=')

## Broadcom NetXtreme II

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

- name: Add non-free repo for Broadcom NetXtreme II (Debian <12)
  ansible.builtin.replace:
    dest: /etc/apt/sources.list
    regexp: '^(main ((?!\bnon-free\b).)*)$'
    replace: '\1 non-free'
  tags:
    - packages
  when:
    - broadcom_netextreme_search.rc == 0
    - ansible_distribution_major_version is version('12', '<')

## Baremetal servers

- name: Install some additionnals tools when it dedicated hardware
  ansible.builtin.apt:
    name:
      - libipc-run-perl
      - freeipmi
      - ipmitool
      - firmware-linux-nonfree
      - intel-microcode
    state: present
    update_cache: True
  tags:
    - packages

## 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
  ansible.builtin.shell:
    cmd: "lspci -q | grep -e 'RAID bus controller' -e 'Serial Attached SCSI controller'"
    executable: /bin/bash
  check_mode: no
  register: raidmodel
  changed_when: "'FAILED' in raidmodel.stdout"
  failed_when: "'FAILED' in raidmodel.stdout"
  tags:
    - packages

- name: "HP"
  ansible.builtin.import_tasks: hardware.hp.yml
  when:
    - "'Hewlett-Packard Company Smart Array' in raidmodel.stdout or  'Adaptec Smart Storage PQI' in raidmodel.stdout"
    - evolinux_packages_hardware_raid | bool

- name: "Dell"
  ansible.builtin.import_tasks: hardware.dell.yml
  when:
    - "'MegaRAID' in raidmodel.stdout"
    - evolinux_packages_hardware_raid | bool

- ansible.builtin.meta: flush_handlers
