---
- name: Install Nginx v1.26.1
  ansible.builtin.dnf:
    name: /flash/squirro-repo/nginx-1.26.1-2.el8.ngx.x86_64.rpm
    disable_gpg_check: true
    state: present
  changed_when: true

- name: Check for OOTB Nginx packages
  ansible.builtin.shell:
    cmd: |
      set -o pipefail
      dnf list installed | grep nginx- | awk '{print $1}'
    executable: /bin/bash
  changed_when: false
  failed_when: false
  register: nginx_packages

- name: Remove unused OOTB Nginx packages
  # noqa: command-instead-of-module
  ansible.builtin.command: |
    rpm -e --nodeps {{ nginx_packages.stdout_lines | join(' ') }}
  changed_when: true
  when:
    - nginx_packages.stdout_lines is defined
    - nginx_packages.stdout_lines | length > 0

- name: Remove default Nginx configuration
  ansible.builtin.file:
    path: /etc/nginx/conf.d/default.conf
    state: absent
  changed_when: true

- name: Ensure nginx config is valid
  ansible.builtin.command: /usr/sbin/nginx -t
  changed_when: false

- name: Restart Nginx and set service to start on boot
  ansible.builtin.systemd:
    name: nginx
    daemon_reload: true
    enabled: true
    state: restarted
  changed_when: false
