--- - name: Check packages to be installed block: - name: "Check if packages are already installed" ansible.builtin.command: "dpkg -s {{ packages_list | join(' ') }}" args: warn: no register: dpkg_info changed_when: dpkg_info.failed rescue: - name: "Add missing packages to the apt install list" ansible.builtin.set_fact: packages_to_install: "{{ packages_to_install | default([]) + dpkg_info.stdout_lines | map('regex_findall', 'package (.+) is not installed and no information is available$') | flatten }}" when: rpm_info.failed - name: Install packages become: yes ansible.builtin.apt: name: "{{ packages_to_install }}" state: present when: packages_to_install | default([]) | length > 0