| 1 | --- |
| 2 | - name: Set next release |
| 3 | hosts: all |
| 4 | tasks: |
| 5 | - set_fact: |
| 6 | release_index: "{{lookup('ansible.utils.index_of', data=lookup('vars', ansible_distribution+'_releases', default=[]), test='eq', value=ansible_distribution_release)}}" |
| 7 | # If there is not a next release available (as defined below in Debian_releasess |
| 8 | # or Ubuntu_releases), the execution of the playbook will fail at this step. |
| 9 | - set_fact: |
| 10 | next_release: "{{lookup('vars', ansible_distribution+'_releases')[release_index|int + 1]}}" |
| 11 | - debug: |
| 12 | msg: "Upgrading from {{ansible_distribution_release}} to {{next_release}}" |
| 13 | vars: |
| 14 | # 'stable' releases ordered from oldest to newest |
| 15 | Debian_releases: |
| 16 | - buster |
| 17 | - bullseye |
| 18 | - bookworm |
| 19 | Ubuntu_releases: |
| 20 | - xenial |
| 21 | - bionic |
| 22 | - focal |
| 23 | - jammy |
| 24 | - name: Run any outstanding upgrades |
| 25 | hosts: all |
| 26 | tasks: |
| 27 | - apt: |
| 28 | update_cache: true |
| 29 | - apt: |
| 30 | upgrade: dist |
| 31 | - apt: |
| 32 | autoremove: true |
| 33 | purge: true |
| 34 | - name: Pre-upgrade backups |
| 35 | hosts: all |
| 36 | tasks: |
| 37 | - name: Check if /etc is a git repo |
| 38 | register: etckeeper |
| 39 | command: |
| 40 | cmd: test -d /etc/.git |
| 41 | ignore_errors: true |
| 42 | - name: Tag etc configuration |
| 43 | when: etckeeper.rc == 0 |
| 44 | block: |
| 45 | - command: |
| 46 | chdir: /etc |
| 47 | argv: |
| 48 | - git |
| 49 | - tag |
| 50 | - "pre-{{next_release}}" |
| 51 | - command: |
| 52 | chdir: /etc |
| 53 | cmd: 'git gc --prune' |
| 54 | - name: Backup package state |
| 55 | block: |
| 56 | - shell: |
| 57 | cmd: "tar czf /var/backups/pre-{{next_release}}-backup.tgz /etc /var/lib/dpkg /var/lib/apt/extended_states" |
| 58 | # Mitogen doesn't seem to work with the 'archive' module, since tarfile is |
| 59 | # "present in the Mitogent importer blacklist", so a shell command is used |
| 60 | # here instead |
| 61 | warn: false |
| 62 | - shell: |
| 63 | cmd: "dpkg --get-selections '*' > /var/backups/dpkg-selections-pre-{{next_release}}.txt" |
| 64 | - file: |
| 65 | path: "{{item}}" |
| 66 | mode: '0600' |
| 67 | with_items: |
| 68 | - "/var/backups/pre-{{next_release}}-backup.tgz" |
| 69 | - "/var/backups/dpkg-selections-pre-{{next_release}}.txt" |
| 70 | - name: Update hostname |
| 71 | hosts: all |
| 72 | vars: |
| 73 | # eg. ansible-playbook -e '{"UPDATE_HOSTNAME":true}' playbooks/release-upgrade.yml |
| 74 | update_hostname: "{{lookup('vars', 'UPDATE_HOSTNAME', default='false')}}" |
| 75 | tasks: |
| 76 | - debug: |
| 77 | msg: "update_hostname: {{update_hostname}}" |
| 78 | - replace: |
| 79 | path: /etc/hostname |
| 80 | regexp: "{{ansible_distribution_release}}" |
| 81 | replace: "{{next_release}}" |
| 82 | when: update_hostname | bool |
| 83 | - replace: |
| 84 | path: /etc/hostname |
| 85 | regexp: "deb{{ansible_distribution_version}}" |
| 86 | replace: "deb{{ansible_distribution_version|int + 1 }}" |
| 87 | when: update_hostname | bool and ansible_distribution == 'Debian' |
| 88 | - name: Debian major version upgrade |
| 89 | hosts: all |
| 90 | vars: |
| 91 | apt_noninteractive_environment: |
| 92 | DEBIAN_FRONTEND: noninteractive |
| 93 | APT_LISTCHANGES_FRONTEND: mail |
| 94 | tasks: |
| 95 | - import_tasks: ../tasks/debian_release_upgrade.yml |
| 96 | when: ansible_distribution == 'Debian' |
| 97 | - name: Ubuntu major version upgrade |
| 98 | hosts: all |
| 99 | tasks: |
| 100 | - name: Do release upgrade |
| 101 | when: ansible_distribution == 'Ubuntu' |
| 102 | command: |
| 103 | cmd: 'do-release-upgrade -m server --frontend=DistUpgradeViewNonInteractive' |
| 104 | - name: Post-upgrade tasks |
| 105 | hosts: all |
| 106 | tasks: |
| 107 | - name: Mark rsyslog as auto |
| 108 | when: next_release == 'bookworm' |
| 109 | command: |
| 110 | cmd: 'apt-mark auto rsyslog' |
| 111 | - name: Autoremove any packages |
| 112 | apt: |
| 113 | autoremove: true |
| 114 | purge: true |
| 115 | - name: Clean apt cache |
| 116 | apt: |
| 117 | autoclean: true |