Skip to content

Instantly share code, notes, and snippets.

@lutangar
Last active October 18, 2021 19:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lutangar/65bff40dc61d8c48c3eb8ab179d21d11 to your computer and use it in GitHub Desktop.
Save lutangar/65bff40dc61d8c48c3eb8ab179d21d11 to your computer and use it in GitHub Desktop.
Ansible task to setup docker-ce on ubuntu or debian
---
- name: Update the `apt` package index
apt:
update_cache: yes
- name: Install packages to allow apt to use a repository over HTTPS
package:
name: "{{ item }}"
state: latest
with_items:
- apt-transport-https
- curl
- software-properties-common
- name: Install `easy_install` to install `pip`
package:
name: python-setuptools
state: present
- name: Install `pip` to install `docker-py`
easy_install:
name: pip
state: latest
- name: Install `docker-py` for the Ansible Docker module
pip:
name: "{{ item }}"
with_items:
- docker-py
# - docker-compose
- name: Add Docker’s official GPG key
apt_key:
url: "https://download.docker.com/linux/{{ ansible_distribution | lower }}/gpg"
state: present
- name: Register distribution *short* code name
shell: lsb_release -cs
register: lsb_release
- name: Set up the stable repository
apt_repository:
repo: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_distribution | lower }} {{ ansible_distribution_release | lower }} stable"
state: present
- name: Update the `apt` package index
apt:
update_cache: yes
- name: Install Docker CE
package:
name: docker-ce
state: present
- name: Verify that Docker CE is installed correctly by running the hello-world image
command: docker run hello-world
register: command_result
failed_when: "'Hello from Docker!' not in command_result.stdout"
@Geod24
Copy link

Geod24 commented Oct 13, 2020

-- name: Register distribution *short* code name
-  shell: lsb_release -cs
-  register: lsb_release
-
 - name: Set up the stable repository
   apt_repository:
-    repo: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_os_family | lower }} {{ lsb_release.stdout }} stable"
+    repo: "deb [arch=amd64] https://download.docker.com/linux/{{ ansible_os_family | lower }} {{ ansible_distribution_release | lower }} stable"
     state: present

This allows it to work with --check

@lutangar
Copy link
Author

This allows it to work with --check

Thanks @Geod24 👍

@pareul
Copy link

pareul commented Apr 1, 2021

Wouldn't it be better to use ansible_distribution instead of ansibe_os_family? At least for Ubuntu it fails for me because it tries to reach the debian repo instead of the ubuntu repo?

@lutangar
Copy link
Author

lutangar commented Apr 1, 2021

Sure @pareul let's try ansible_distribution instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment