  - name: Try to update apt cache
    apt: update_cache=yes cache_valid_time=3600
    when: ansible_os_family == "Debian" or ansible_os_family == "Linuxmint" or ansible_os_family == "Kylin"
    ignore_errors: true

  - name: Install {{ package_name }} (latest) using apt
    # We use update_cache=yes without ignoring errors (Unlike the "Try to update apt cache" task above),
    # because if `latest` package is requested it is an error if we can not update the cache.
    apt: name={{ package_name }} state={{ package_state }} update_cache=yes cache_valid_time=3600
    when: package_state == "latest" and ansible_os_family == "Debian" or ansible_os_family == "Linuxmint" or ansible_os_family == "Kylin"

  - name: Install {{ package_name }} using apt
    apt: name={{ package_name }} state={{ package_state }}
    when: package_state != "latest" and ansible_os_family == "Debian" or ansible_os_family == "Linuxmint" or ansible_os_family == "Kylin"

  - name: Install {{ package_name }} using yum
    yum: name={{ package_name }} state={{ package_state }} update_cache=yes
    when: ansible_os_family == "RedHat"

  - name: Install {{ package_name }} using zypper
    zypper: name={{ package_name }} state={{ package_state }} update_cache=yes
    when: ansible_os_family == "Suse"
