---

- name: Ensure that the required package groups are installed, RHEL 7
  command: "yum install {{ sap_preconfigure_packagegroups|join(' ') }} -y"
  args:
    warn: false
  register: __sap_preconfigure_register_yum_group_install
  when:
    - ansible_os_family == 'RedHat'
    - ansible_distribution_major_version == '7'

- name: Ensure that the required package groups are installed, RHEL 8 and RHEL 9
# Note: We want to avoid unwanted package upgrades, see bug 1983749.
  command: "yum install {{ sap_preconfigure_packagegroups|join(' ') }} --nobest --exclude=kernel* -y"
  args:
    warn: false
  register: __sap_preconfigure_register_yum_group_install
  when:
    - ansible_os_family == 'RedHat'
    - (ansible_distribution_major_version == '8' or
         ansible_distribution_major_version == '9'
      )

- name: Display the output of the yum install command
  debug:
    var: __sap_preconfigure_register_yum_group_install

- name: Ensure that the required packages are installed
  package:
    state: present
    name: "{{ sap_preconfigure_packages }}"

- name: Ensure that the minimum required package versions are installed
  block:
    - name: Create a list of minimum required package versions to be installed
# How does it work?
# 1 - Print the required package name and version with a prefix "1" followed by a space.
# 2 - In the same output sequence, list all installed versions of this package with a prefix "2" followed by a space.
# 3 - Replace all occurrences of ".el" by ".0.0" so that the sort -V correctly sorts packages with ".el" in its name
# 4 - Sort the list by the name and version.
# 5 - Replace ".0.0" by ".el" again to get back the original names.
# 6 - Store the last installed version of the package in variable latestpkg.
# 7 - Store the last content of column 1 in variable col1, the last content of column 2 in variable col2,
#     and the last number of fields in variable _nf.
# 8 - case 1: If the last number of output fields is greater than 2, it indicates that the package is not installed
#             because the output of "rpm -q" will be similar to "package XXX is not installed".
# 8 - case 2a: If the first column of the last line of the output is "1", it means that the required package is
#              the latest of all required and installed versions of the package, so it means that the package needs
#              to be updated.
# 8 - case 2b: If the first column of the last line of the output is "2", it means that at least of the installed
#              versions the package is equal to or greater than the required package version.
      shell: |
        (echo "1 {{ pkg[0] }}-{{ pkg[1] }}";rpm -q --qf "%{NAME}-%{VERSION}-%{RELEASE}\n" {{ pkg[0] }} |
         awk '{printf ("2 %s\n", $0)}') |
         awk '{gsub ("\\.el", ".0.0"); print}' |
         sort -k 2 -k 1 -V |
         awk '{gsub ("\\.0\\.0", ".el"); col1=$1; col2=$2; _nf=NF}
           $1==2{latestpkg=$2}
           END {
               if (_nf>2) {
                  printf ("{{ pkg[0] }}-{{ pkg[1] }}\n")
               } else {
                  if (col1==1) {
                     printf ("{{ pkg[0] }}-{{ pkg[1] }}\n")
                  }
               }
            }'
      with_list: "{{ __sap_preconfigure_min_pkgs }}"
      loop_control:
        loop_var: pkg
      check_mode: no
      register: __sap_preconfigure_register_minpkglist
      changed_when: no

    - name: Display the content of the minimum package list variable
      debug:
        var: __sap_preconfigure_register_minpkglist

    - name: Install minimum packages if required
      yum:
        name: "{{ line_item.stdout }}"
        state: present
      with_items: "{{ __sap_preconfigure_register_minpkglist.results }}"
      loop_control:
        loop_var: line_item

  when:
    - sap_preconfigure_min_package_check|bool
    - __sap_preconfigure_min_pkgs|d([])

# Reason for noqa: Both yum and dnf support "state: latest"
- name: Ensure that the system is updated to the latest patchlevel # noqa package-latest
  package:
    state: latest
    name: "*"
  when: sap_preconfigure_update

- name: "Set needs-restarting command in case of RHEL 7"
  set_fact:
    __sap_preconfigure_fact_needs_restarting_command: "needs-restarting -r"
  when:
    - ansible_os_family == 'RedHat'
    - ansible_distribution_major_version == '7'

- name: "Set needs-restarting command in case of RHEL 8 or RHEL 9, except RHEL 8.0"
  set_fact:
    __sap_preconfigure_fact_needs_restarting_command: "yum needs-restarting -r"
  when:
    - ansible_os_family == 'RedHat'
    - (ansible_distribution_major_version == '8' or
         ansible_distribution_major_version == '9'
      )
    - ansible_distribution_version != '8.0'

- name: "Set customized needs-restarting command in case of RHEL 8.0"
  set_fact:
    __sap_preconfigure_fact_needs_restarting_command: "_IKRNL=$(rpm -q --last kernel | awk 'NR==1{sub(/kernel-/,\"\"); print $1}');
                                                       _CKRNL=$(uname -r); if [ ${_IKRNL} != ${_CKRNL} ]; then exit 1; else exit 0; fi"
  when:
    - ansible_os_family == 'RedHat'
    - ansible_distribution_version == '8.0'

- name: Display the command for checking a reboot requirement
  debug:
    var: __sap_preconfigure_fact_needs_restarting_command

# Reason for noqa: The command to be executed might contain pipes
- name: Determine if the system needs to be restarted # noqa command-instead-of-shell
  shell: "{{ __sap_preconfigure_fact_needs_restarting_command }}"
  register: __sap_preconfigure_register_needs_restarting
  ignore_errors: true
  changed_when: false
  args:
    warn: false
  check_mode: false

- name: Display the output of the reboot requirement check
  debug:
    var: __sap_preconfigure_register_needs_restarting

- name: Call Reboot handler if necessary
  command: /bin/true
  notify: __sap_preconfigure_reboot_handler
  when: __sap_preconfigure_register_needs_restarting is failed
