---
- name: "package yum/apt retry installer: {{ package | join(' ') }} {{ rpm | join(' ') }} {{ deb | join(' ') }}"
  shell: |
      if [ "{{ creates | join(' ') }}" != "" ]; then
        allfound=1
        for file in {{ creates | join(' ') }}; do
          if [ $(ls -l1 $(echo $file | tr , ' ') 2>/dev/null | wc -l) -eq 0 ]; then
            allfound=0
          fi
        done
        if [ $allfound -eq 1 ]; then
          exit 0
        fi
      fi
      RC=0
      if test -f /usr/bin/apt; then
        apt-get install -y {{ package | join(' ') }} {{ deb | join(' ') }}
        RC=$?
      elif test -f /usr/bin/dnf; then
        dnf install -y {{ package | join(' ') }} {{ rpm | join(' ') }}
        RC=$?
      else
        yum install -y {{ package | join(' ') }} {{ rpm | join(' ') }}
        RC=$?
      fi
      for file in {{ creates | join(' ') }}; do
        if [ $(ls -l1 $(echo $file | tr , ' ') 2>/dev/null | wc -l) -eq 0 ]; then
          echo "file $file not found after pkg installation"
          yum clean all >/dev/null 2>&1 || :
          apt update    >/dev/null 2>&1 || :
          exit 1
        fi
      done
      exit $RC
  register: install_result
  ignore_errors: yes
  failed_when: install_result.rc != 0
  retries: "{{ retries | default(5) }}"
  delay: "{{ delay | default(30) }}"
  until: install_result is succeeded

- debug: var=install_result
  when: install_result.rc != 0

- name: Verify installation
  fail:
    msg: package installation failed
  when: install_result.rc != 0
