---
- name: Get packages
  ansible.builtin.package_facts:
  check_mode: false

- name: Get services
  ansible.builtin.service_facts:
  check_mode: false

- name: Upgrade packages (yum)
  ansible.builtin.yum:
    name: '*'
    state: latest  # noqa: package-latest - Intended to update packages to latest
    exclude: "{{ exclude_packages }}"
  when: ansible_pkg_mgr == "yum"
  register: patchingresult_yum

- name: Upgrade packages (dnf)
  ansible.builtin.dnf:
    name: '*'
    state: latest  # noqa: package-latest - Intended to update packages to latest
    exclude: "{{ exclude_packages }}"
  when: ansible_pkg_mgr == "dnf"
  register: patchingresult_dnf

- name: Check to see if we need a reboot
  ansible.builtin.command: needs-restarting -r
  register: result
  changed_when: result.rc == 1
  failed_when: result.rc > 1
  check_mode: false

- name: Reboot Server if Necessary
  ansible.builtin.reboot:
  when:
    - result.rc == 1
    - allow_reboot
