Skip to main content

Which Ubuntu Release Apt Repo

Author
Jeffrey Forman
Table of Contents

Problem Statement
#

Ubuntu and Linux Mint use different values for their release codenames, even though they both use Ubuntu apt repositories. When trying to install docker using Docker’s instructions, the Linux Mint invocation failed because there is no jammy codename of Docker binaries.

Goal
#

Make an Ansible playbook like the following that works for both Ubuntu and Linux Mint that handles the release codename correctly.

- name: add docker repository sources entry
  apt_repository:
    repo: deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu {{ RELEASE_CODENAME }} stable
    filename: docker

How to get it done.
#

First, I needed a way to get the release codename from Linux Mint that matches Ubuntu.

Enter /etc/os-release.

From Linux Mint (21.1):

NAME="Linux Mint"
VERSION="21.1 (Vera)"
ID=linuxmint
ID_LIKE="ubuntu debian"
PRETTY_NAME="Linux Mint 21.1"
VERSION_ID="21.1"
HOME_URL="https://www.linuxmint.com/"
SUPPORT_URL="https://forums.linuxmint.com/"
BUG_REPORT_URL="http://linuxmint-troubleshooting-guide.readthedocs.io/en/latest/"
PRIVACY_POLICY_URL="https://www.linuxmint.com/"
VERSION_CODENAME=vera
UBUNTU_CODENAME=jammy

From Ubuntu (22.04.1 LTS):

PRETTY_NAME="Ubuntu 22.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.1 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy

The important line is UBUNTU_CODENAME, which in this case matches, and is what I’m looking for since https://download.docker.com/linux/ubuntu/dists/ publishes releases based upon the Ubuntu codename. Now I need to extract this value with Ansible and use it as part of the apt repository specification.

Solution
#

I found https://itecnotes.com/server/extracting-part-of-the-string-using-ansible-regex_search-and-save-the-output-as-a-variable.

Ansible Playbook

---
- hosts: all
  become: no

  tasks:
    - name: check for presense of /etc/os-release
      stat:
        path: "/etc/os-release"
      register: file_os_release

    - name: get os-release contents
      shell:
        cmd: "cat /etc/os-release"
      register: file_os_release_content
      when: file_os_release.stat.exists

    - set_fact:
        codename: "{{ file_os_release_content.stdout  | regex_search('UBUNTU_CODENAME=(.*)', '\\1') | first }}"

    - name: print release codename
      debug:
        var: codename

And now in the yaml for the apt_repository task, you can use {{codname}} as the distribution like so:

- name: add docker repository sources entry
  ansible.builtin.apt_repository:
    repo: deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu {{ codename }} stable

Victory.