This blog I will upgrade ubuntu, update all packages, only target ubuntu nodes using the ‘when’ command and run multiple tasks.

Here are my two yml files tested and working in my lab

sudo nano upgrade-ubuntu-to-next-version.yml
---
- hosts: all
  become: true
  tasks:
  - name:  Update apt cache
    apt:
      update_cache: yes
    when: ansible_distribution == 'Ubuntu'

  - name: Upgrade to the next Ubuntu release
    command:
        cmd: /usr/bin/do-release-upgrade -f DistUpgradeViewNonInteractive
    when: ansible_distribution == 'Ubuntu'

  - name:  Reboot the system
    reboot:
        connect_timeout: 60
        reboot_timeout: 300
    when: ansible_distribution == 'Ubuntu'
sudo nano upgrade-ubuntu-packages.yml
---
- hosts: all
  become: true
  tasks:
  - name:  Update apt cache
    apt:
      update_cache: yes
    when: ansible_distribution == 'Ubuntu'

  - name: Upgrade all packages
    apt:
       upgrade: yes
       update_cache: yes
    when: ansible_distribution == 'Ubuntu'
ansible-playbook --ask-become-pass upgrade-ubuntu-packages.yml

The below screen shot is me trying to run the playbook upgrade-ubuntu-to-next-version.yml it failed due being the latest ubuntu version [“Checking for a new Ubuntu release”, “No new release found.”]}

The below screen shot is me trying to run the upgrade-ubuntu-packages.yml this worked

You can see from the managed node it ran, I checked the apt history file

You can also see there are no available updates

By Kad