Use in the OpenSible console
Open Infrastructure → Stack Hub Blueprints, switch the source to Cloud hub, then pick Docker Engine and press Use.
Requirements
ansible >=2.14
Default variables
defaults.json
{
"become": true,
"docker_user": "ubuntu",
"enable_buildx": true
}vars.example.yml
vars.example.yml
---
# User added to the docker group so they can run docker without sudo.
docker_user: ubuntu
# Install docker-buildx-plugin alongside the compose plugin.
enable_buildx: true
# Elevate to root via sudo.
become: true
Playbook (playbook.yml)
playbook.yml
---
# Rendered from template: Docker Engine
- name: Install Docker Engine
hosts: all
become: true
gather_facts: false
tasks:
- name: Bootstrap Python if missing
ansible.builtin.raw: |
if command -v python3 >/dev/null 2>&1; then
echo 'python3 present'
exit 0
fi
if command -v apt-get >/dev/null 2>&1; then
export DEBIAN_FRONTEND=noninteractive
apt-get update -y
apt-get install -y python3
echo 'python3 installed'
exit 0
fi
echo 'python3 missing and apt-get is unavailable' >&2
exit 1
register: bootstrap_python
changed_when: "'python3 installed' in bootstrap_python.stdout"
- name: Gather facts after Python bootstrap
ansible.builtin.setup:
- name: Detect upstream Debian/Ubuntu distro id for Docker repo
ansible.builtin.shell: |
set -e
. /etc/os-release
if [ "$ID" = "ubuntu" ] || [ "$ID" = "debian" ]; then
echo "$ID"
elif echo "${ID_LIKE:-}" | grep -qw ubuntu; then
echo ubuntu
elif echo "${ID_LIKE:-}" | grep -qw debian; then
echo debian
else
echo "unsupported: $ID" >&2; exit 1
fi
register: docker_distro_id
changed_when: false
when: ansible_os_family == 'Debian'
- name: Detect upstream Debian/Ubuntu codename for Docker repo
ansible.builtin.shell: |
set -e
. /etc/os-release
if [ -n "${UBUNTU_CODENAME:-}" ]; then
echo "$UBUNTU_CODENAME"
elif [ -n "${DEBIAN_CODENAME:-}" ]; then
echo "$DEBIAN_CODENAME"
elif [ "$ID" = "ubuntu" ] || [ "$ID" = "debian" ]; then
echo "${VERSION_CODENAME:-}"
else
echo "${VERSION_CODENAME:-}"
fi
register: docker_distro_codename
changed_when: false
when: ansible_os_family == 'Debian'
- name: Install prerequisites
ansible.builtin.apt:
name: [ca-certificates, curl, gnupg, lsb-release]
state: present
update_cache: true
when: ansible_os_family == 'Debian'
- name: Detect Debian package architecture for Docker repo
ansible.builtin.command: dpkg --print-architecture
register: docker_deb_arch
changed_when: false
when: ansible_os_family == 'Debian'
- name: Ensure keyrings dir
ansible.builtin.file:
path: /etc/apt/keyrings
state: directory
mode: '0755'
when: ansible_os_family == 'Debian'
- name: Add Docker apt key
ansible.builtin.get_url:
url: https://download.docker.com/linux/{{ docker_distro_id.stdout }}/gpg
dest: /etc/apt/keyrings/docker.asc
mode: '0644'
force: true
when: ansible_os_family == 'Debian'
- name: Add Docker apt repo
ansible.builtin.apt_repository:
repo: >-
deb [arch={{ docker_deb_arch.stdout }} signed-by=/etc/apt/keyrings/docker.asc]
https://download.docker.com/linux/{{ docker_distro_id.stdout }}
{{ docker_distro_codename.stdout }} stable
filename: docker
state: present
update_cache: true
when: ansible_os_family == 'Debian'
- name: Install docker packages
ansible.builtin.apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-compose-plugin
- docker-buildx-plugin
state: present
update_cache: true
when: ansible_os_family == 'Debian'
- name: Enable and start docker
ansible.builtin.service:
name: docker
state: started
enabled: true
- name: Add ubuntu to docker group
ansible.builtin.user:
name: ubuntu
groups: docker
append: true
when: ansible_os_family == 'Debian'
Versions (2)
v1.0.0playbook.ymlv28.3.3-1playbook.yml
