---
# This will look for packages in install_files/ansible-base
- name: if defined copy local deb packages to target system
  copy:
    src: ../../build/{{ item }}
    dest: /root/
  with_items: local_deb_packages
  tags:
    - install_local_pkgs

# TODO:
# This should use the ansible apt module
# apt: deb=/root/{{ item }}
# but keep getting an error when using a list with that module. Using the shell
# command as a temp work around until that is resolved.
- name: if defined install local deb packages
  shell: "dpkg -i /root/{{ item }} || true && apt-get -f install -y"
  with_items: local_deb_packages
  tags:
    - install_local_pkgs

---
# This will look for packages in install_files/ansible-base,
# so the "build" directory is relative to playbook, which is
# why the parent dirs ("..") are necessary.
- name: if defined copy local deb packages to target system
  copy:
    src: ../../build/{{ item }}
    dest: /root/
  with_items: local_deb_packages
  tags:
    - install_local_pkgs

# There's a known bug in Ansible that causes installing
# .deb packages via the apt module to fail when invoked
# with `with_items`. A temporary workaround is to use
# `with_indexed_items` and reference the value, which tricks
# the `apt` module into installing each deb package separately,
# rather than joining into a single comma-separated string.
- name: if defined install local deb packages
  apt:
    deb: /root/{{ item.1 }}
  with_indexed_items: local_deb_packages
  tags:
    - install_local_pkgs

Install local packages via apt module
Install local packages via apt module

The apt module flattens package lists so apt is only called once,
rather than iteratively, when used with `with_items`. That works well
for installing packages from repos, but when installing from local deb
files, then flattening causes an error.

As a workaround, let's trick the apt module into not flattening by
using `with_indexed` and referencing the deb files via the `.1`
attribute.
