Installing Ubuntu phased updates using ansible.builtin.apt?

Ubuntu phased updates don’t appear to be installable using ansible.builtin.apt even with allow_change_held_packages set to True?

This is how I’m working around this:

- name: Check for phased updates  # noqa: command-instead-of-module
  ansible.builtin.command:
    cmd: apt-get -sq --allow-change-held-packages full-upgrade
  environment:
    DEBIAN_FRONTEND: noninteractive
  check_mode: false
  changed_when: false
  register: update_simulate_full_upgrade

- name: Ubuntu phased updates
  block:

    - name: Set a fact for available phased updates
      ansible.builtin.set_fact:
        update_phased_updates_available: >-
          {{ update_simulate_full_upgrade.stdout_lines
          | ansible.builtin.select('ansible.builtin.regex', '^[ ][ ][a-z0-9]'
          | ansible.builtin.map('ansible.builtin.regex_replace', '^[ ][ ]')
          | ansible.builtin.join(' ')
          | ansible.builtin.split(' ') }}

    - name: Debug update_phased_updates_available
      ansible.builtin.debug:
        var: update_phased_updates_available
      when:
        - update_phased_updates_available is ansible.builtin.defined
        - update_phased_updates_available | ansible.builtin.type_debug == "list"
        - update_phased_updates_available != []

    # The ansible.builtin.apt module can't be used to install phased updates
    - name: Install phased updates  # noqa: command-instead-of-module
      ansible.builtin.command:
        cmd: >-
          apt-get install -yqq -o "Dpkg::Options::=--force-confdef" -o "Dpkg::Options::=--force-confold"
          --allow-change-held-packages {{ '--simulate' if ansible_check_mode | ansible.builtin.bool }}
          {{ update_phased_updates_available | ansible.builtin.join(' ') }}
      environment:
        DEBIAN_FRONTEND: noninteractive
      check_mode: false
      register: update_phased_updates_installed
      when:
        - update_phased_updates | ansible.builtin.bool
        - update_phased_updates_available is ansible.builtin.defined
        - update_phased_updates_available | ansible.builtin.type_debug == "list"
        - update_phased_updates_available != []

    - name: Debug update_phased_updates_installed.stdout_lines
      ansible.builtin.debug:
        var: update_phased_updates_installed.stdout_lines
        verbosity: >-
          {{ '0' if (ansible_check_mode | ansible.builtin.bool) or (ansible_diff_mode | ansible.builtin.bool) else '1' }}
      tags:
        - debug

  when:
    - update_simulate_full_upgrade.stdout_lines | ansible.builtin.select('ansible.builtin.regex', '^[ ][ ][a-z0-9]') != []

Would / could / should a upgrade_phased_packages option be considered as an appropriate addition?

allow_change_held_packages applies to packages placed on hold with apt-mark hold; phased updates are a separate APT policy. A phased update can be held back because that host is not in the rollout cohort, even when the package is not on hold.

If your goal is for a group of managed hosts to receive the same phased updates, APT supports setting a shared APT::Machine-ID. If your goal is to install every phased update immediately, APT supports APT::Get::Always-Include-Phased-Updates, though that opts the hosts into updates early.

I’d manage the desired APT setting in /etc/apt/apt.conf.d/ and see whether ansible.builtin.apt honors it in your playbook. The current workaround parses human-readable apt-get output and explicitly installs the listed packages, which bypasses the rollout policy; it may also install a package that was held back for a different reason.

A module option could be useful if there’s a reproducible case where the relevant APT configuration isn’t honored by ansible.builtin.apt. I’d include the Ansible, Ubuntu, and python3-apt versions plus a minimal playbook in a feature request, since the module’s allow_change_held_packages option is about package holds rather than phased updates.

Thanks @IPvSean, I agree, I’ve also realised that “it may also install a package that was held back for a different reason”, so I’m going to leave update_phased_updates set to false and not actually install and of the phased updates available — it is handy to know which servers have pending updates and what they are.