diff --git a/defaults/main.yml b/defaults/main.yml index 408d75d..566d9c6 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -2,16 +2,94 @@ # A list of users to be added to the docker group. docker_users: [] -centos_repo_file: docker-ce.repo -centos_repo_full_path: "/etc/yum.repos.d/{{ centos_repo_file }}" -centos_repo_url: "https://download.docker.com/linux/centos/\ - {{ centos_repo_file }}" +docker_base_url: "https://download.docker.com/linux" +docker_distro_aliases: + CentOS: centos + # RedHat -> centos is intentional. + RedHat: centos + Ubuntu: ubuntu +docker_distro_base_url: "{{ docker_base_url }}/\ + {{ docker_distro_aliases[ansible_distribution] }}" -ubuntu_docker_base_url: https://download.docker.com/linux/ubuntu -ubuntu_gpg_url: "{{ ubuntu_docker_base_url }}/gpg" +################################################################################ +# RedHat family docker repo. +################################################################################ + +redhat_fam_docker_repo_file: docker-ce.repo +redhat_fam_repo_url: "{{ docker_base_url }}/\ + {{ docker_distro_aliases[ansible_distribution] }}/\ + {{ redhat_fam_docker_repo_file }}" +redhat_fam_docker_repo_full_path: "{{ redhat_fam_repo_base_path }}/\ + {{ redhat_fam_docker_repo_file }}" + +################################################################################ +# RedHat family extras repo. +# +# The extras repo holds some packages that are dependencies of docker. +################################################################################ + +redhat_fam_repo_base_path: /etc/yum.repos.d + +# RedHat 8 doesn't seem to have an extras repo. Docker also installs fine i RHEL +# 8 without making any changes to an extras repo. +redhat_7_extras_repo_file: redhat.repo +redhat_7_extras_group: rhel-7-server-extras-rpms +centos_7_extras_repo_file: CentOS-Sources.repo +centos_8_extras_repo_file: CentOS-Linux-Extras.repo +centos_7_8_extras_group: extras + +redhat_fam_extras_repo_file: + CentOS: + "7": + repo: "{{ redhat_fam_repo_base_path }}/{{ centos_7_extras_repo_file }}" + group: "{{ centos_7_8_extras_group }}" + "8": + repo: "{{ redhat_fam_repo_base_path }}/{{ centos_8_extras_repo_file }}" + group: "{{ centos_7_8_extras_group }}" + RedHat: + "7": + repo: "{{ redhat_fam_repo_base_path }}/{{ redhat_7_extras_repo_file }}" + group: "{{ redhat_7_extras_group }}" + + +################################################################################ +# Ubuntu 3rd party repo. +################################################################################ + +ubuntu_gpg_url: "{{ docker_distro_base_url }}/gpg" ubuntu_gpg_fingerprint: 9DC858229FC7DD38854AE2D88D81803C0EBFCD88 ubuntu_apt_repo: > deb [arch=amd64] - "{{ ubuntu_docker_base_url }}" + "{{ docker_distro_base_url }}" "{{ ansible_distribution_release }}" stable + +################################################################################ +# Thes are the packages that are removed before adding the 3rd party repo. +################################################################################ + +centos_remove_old_packages: + - docker + - docker-client + - docker-client-latest + - docker-common + - docker-latest + - docker-latest-logrotate + - docker-logrotate + - docker-engine + +ubuntu_remove_old_packages: + - docker + - docker-engine + - docker.io + - containerd + - runc + +# A map of ansible_distribution -> old packages to remove. +# +# We use centos for RedHat because Docker doesn't officially support RHEL for +# x86_64, however the centos repo works. +docker_remove_old_packages: + CentOS: "{{centos_remove_old_packages}}" + RedHat: "{{centos_remove_old_packages}}" + Ubuntu: "{{ubuntu_remove_old_packages}}" diff --git a/meta/main.yml b/meta/main.yml index 551e04e..5a5d05d 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -16,9 +16,11 @@ galaxy_info: - name: Amazon Linux 2 versions: - all + - name: EL + - 7 + - 8 - name: Ubuntu - focal # 20.04 - # CentOS is apparently not in https://galaxy.ansible.com/api/v1/platforms/ galaxy_tags: - containers diff --git a/tasks/main.yml b/tasks/main.yml index 8ce1b93..78a911e 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -2,27 +2,38 @@ # This has only been tests on Amazon Linux 2, CentOS 7-8, Ubuntu 20.04, and # ArchLinux. +################################################################################ # Prequisites # This step will: # 1. Remove any unwanted docker packages, (we want newest package from official # docker repos. # 2. Install dependencies. # 3. Add official docker repo. +################################################################################ # Archlinux and Amazon Linux don't have any prequisite steps, they're # repositories have up to date docker packages so we don't need to add a 3rd # party repo or uninstall unwanted packages. -- include_tasks: prereq_centos.yml - when: ansible_distribution == "CentOS" +- include_tasks: prereq_redhat_family.yml + # We need to exclude Amazon because it's technically in the "RedHat" family + # but it does not require these prereq steps. + when: ansible_os_family == "RedHat" and ansible_distribution != "Amazon" - include_tasks: prereq_ubuntu.yml when: ansible_distribution == "Ubuntu" -# The arch cloud image does not have a package cache. +################################################################################ +# End prerequisite steps +################################################################################ + +# The arch cloud image does not have a package cache, this file will update the +# cache only if we need to install the docker package (not already installed). - include_tasks: update_arch.yml when: ansible_distribution == "Archlinux" +# Install the docker packages. The packages and their names depends on the +# distro, - name: Install docker packages after adding 3rd party repo. package: name: diff --git a/tasks/prereq_centos.yml b/tasks/prereq_centos.yml deleted file mode 100644 index 21327dd..0000000 --- a/tasks/prereq_centos.yml +++ /dev/null @@ -1,103 +0,0 @@ ---- -# This commented out code was an attempt to make sure the CentOS extras repo is -# enabled. -# TODO This isn't working. It will create the repo everytime regardless whether -# or not the repo is already enabled. For now we leave it because this repo is -# enabled by defaullt on CentOS 7 & 8 -# -# https://docs.docker.com/engine/install/centos/#os-requirements -# Look in /etc/yum.repos.d/ for examples of the fields for a repo. This files -# have variables in their config. To get the values of the variables you can -# run: -# `python -c 'import yum; yb = yum.YumBase(); print(yb.conf.yumvar)'` -# - name: Ensure CentOS extras repo is enbaled for CentOS 7. -# yum_repository: -# name: extras -# description: CentOS-{{ ansible_distribution_major_version }} - Extras -# enabled: true -# mirrorlist: "http://mirrorlist.centos.org/?\ -# release={{ ansible_distribution_major_version }}&\ -# arch={{ ansible_architecture }}&\ -# repo=extras&infra=genclo" -# gpgcheck: true -# become: true -# when: > -# ansible_distribution == "CentOS" and -# ansible_distribution_major_version == "7" -# - name: Ensure CentOS extras repo is enbaled for CentOS 8. -# yum_repository: -# name: extras -# description: > -# CentOS Linux {{ ansible_distribution_major_version }} - Extras -# enabled: true -# mirrorlist: "http://mirrorlist.centos.org/?\ -# release={{ ansible_distribution_major_version }}&\ -# arch={{ ansible_architecture }}&\ -# repo=extras&infra=genclo" -# gpgcheck: true -# become: true -# when: > -# ansible_distribution == "CentOS" and -# ansible_distribution_major_version == "8" - -# TODO Add this step. -# https://docs.docker.com/engine/install/centos/#os-requirements - -################################################################################ -# Uninstall unwanted docker packages. -################################################################################ - -# This step combines "Uninstall old versions" for RedHat family distros. -# https://docs.docker.com/engine/install/centos/#uninstall-old-versions -# https://docs.docker.com/engine/install/fedora/#uninstall-old-versions -# https://docs.docker.com/engine/install/rhel/#uninstall-old-versions - -# We check if a docker repo has alredy been added to yum. If so, then we want to -# skip the removing of the old docker packages to make this script more -# idempotent. -- name: Check if docker repo has alredy been added. - command: yum repolist # noqa command-instead-of-module - changed_when: false - register: repolist - -- name: Uninstall old versions of docker RedHat like distros.. - yum: - name: - - docker - - docker-client - - docker-client-latest - - docker-common - - docker-latest - - docker-latest-logrotate - - docker-logrotate - - docker-selinux - - docker-engine-selinux - - docker-engine - - podman - - runc - state: absent - become: true - when: "'docker' not in repolist.stdout" - -################################################################################ -# Install dependencies -################################################################################ - -# https://docs.docker.com/engine/install/centos/#install-using-the-repository -- name: Install dependencies for yum distros. - yum: - name: - - yum-utils - state: present - become: true - -################################################################################ -# Install docker repo. -################################################################################ - -# https://docs.docker.com/engine/install/centos/#install-using-the-repository -- name: Add docker yum repo for centos. - command: - cmd: "yum-config-manager --add-repo {{ centos_repo_url }}" - creates: "{{ centos_repo_full_path }}" - become: true diff --git a/tasks/prereq_redhat_family.yml b/tasks/prereq_redhat_family.yml new file mode 100644 index 0000000..2db4482 --- /dev/null +++ b/tasks/prereq_redhat_family.yml @@ -0,0 +1,63 @@ +--- +# https://docs.docker.com/engine/install/centos/#os-requirements +- name: Enable the extras repo in RedHat family distros. + ini_file: + path: "{{ redhat_fam_extras_repo_file[ansible_distribution]\ + [ansible_distribution_major_version].repo }}" + section: "{{ redhat_fam_extras_repo_file[ansible_distribution]\ + [ansible_distribution_major_version].group }}" + option: enabled + value: 1 + mode: '0644' + exclusive: true + state: present + backup: true + become: true + when: > + ansible_distribution == "CentOS" or + ( ansible_distribution == "RedHat" and + ansible_distribution_major_version == "7" ) + +################################################################################ +# Uninstall unwanted docker packages. +################################################################################ + +# https://docs.docker.com/engine/install/centos/#uninstall-old-versions +# +# We check if a docker repo has alredy been added to yum. If so, then we want to +# skip the removing of the old docker packages to make this script more +# idempotent. +- name: Check if docker repo has already been added. + command: yum repolist # noqa command-instead-of-module + changed_when: false + register: repolist + +- name: Uninstall old versions of docker RedHat like distros. + yum: + name: "{{ docker_remove_old_packages[ansible_distribution] }}" + state: absent + become: true + when: "'docker' not in repolist.stdout" + +################################################################################ +# Install dependencies +################################################################################ + +# https://docs.docker.com/engine/install/centos/#install-using-the-repository +- name: Install dependencies for yum distros. + yum: + name: + - yum-utils + state: present + become: true + +################################################################################ +# Install docker repo. +################################################################################ + +# https://docs.docker.com/engine/install/centos/#install-using-the-repository +- name: Add docker yum repo for centos. + command: + cmd: "yum-config-manager --add-repo {{ redhat_fam_repo_url }}" + creates: "{{ redhat_fam_docker_repo_full_path }}" + become: true diff --git a/tasks/prereq_ubuntu.yml b/tasks/prereq_ubuntu.yml index 67e5c47..623dbe2 100644 --- a/tasks/prereq_ubuntu.yml +++ b/tasks/prereq_ubuntu.yml @@ -6,12 +6,7 @@ # https://docs.docker.com/engine/install/ubuntu/#uninstall-old-versions - name: Remove old docker packages for Debian like distros. apt: - name: - - docker - - docker-engine - - docker.io - - containerd - - runc + name: "{{ docker_remove_old_packages[ansible_distribution] }}" state: absent become: true diff --git a/tasks/update_arch.yml b/tasks/update_arch.yml index ad61285..9aa107b 100644 --- a/tasks/update_arch.yml +++ b/tasks/update_arch.yml @@ -1,5 +1,5 @@ --- -# These tasks will update the pacman repos if Docker is not alredy instflled. +# These tasks will update the pacman repos if Docker is not already installed. # # We only want to update the chache if needed, otherwise this will break # idempotentcy.