Dynamic Package Installs?

Currently I have the following.

tasks:

  • name: install the Debian packages
    apt: name={{ item }} state=latest
    with_items: “{{ packages }}”
    when: ansible_os_family == “Debian”

  • name: install the RedHat packages
    yum: pkg={{ item }} state=latest
    with_items: “{{ packages }}”
    when: ansible_os_family == “RedHat”

Is there anyway to have one task? I have packages array dynamically created. I wish at least I can just have tasks completely ignored if it is on the wrong OS, so it won’t be included…

Would it be possible to do something like this?

tasks:

  • include: “tasks/{{ ansible_os_family }}.yml”

I get this error

ERROR: file could not read: /Users/testuser/testarea/provisioning/tasks/{{ ansible_os_family }}.yml
Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.