Here is a playbook for Ansible to install Docker and Docker Compose on Debian.

You can of course, use a role from Galaxy. You have Gerling’s version or Debops (Ansible stack for Debian).

But here I wanted to implement the install guide from Docker – and translate this into an Ansible playbook (which then should be turn into a custom role for more reusability).

The official guide give use the following install steps:

  1. Update the apt package index
  2. Install packages to allow apt to use a repository over HTTPS
  3. Add Docker’s official GPG key
  4. Add Docker’s official Apt Repository
  5. Then refresh the apt index
  6. Install docker from the package

Yeah, that’s a few steps. But that’s the best way to make sure you always have the last version of docker. Docker is a project that is moving fast and you cannot wait for your favourite distrib to give you the right package, it will always be dirty old version you don’t want. Maybe in some future when things get a bit more stable it will be enough to install it from debian default – but for now that’s really a bad idea.

So, let’s cut the chase here is a the playbook – with comments to make things easy to follow if anything is unclear.


---
- hosts: all
  tasks:
  - name: Update the apt package index
    become: yes
    apt:
      name: "*"
      state: latest
      update_cache: yes
      force_apt_get: yes
  - name: Install packages for apt add repository over HTTPS
    become: yes
    apt:
      name: "{{ packagesdep }}"
      force_apt_get: yes
      state: latest
      update_cache: yes
    vars:
      packagesdep:
      - git
      - apt-transport-https
      - ca-certificates
      - wget
      - software-properties-common
      - gnupg2
      - curl
  - name: Add Apt signing key from official docker repo
    apt_key:
      url: https://download.docker.com/linux/debian/gpg
      state: present
  - name: add docker official repository for Debian Stretch
    apt_repository:
      repo: deb [arch=amd64] https://download.docker.com/linux/debian stretch stable
      state: present
  - name: Index new repo into the cache
    become: yes
    apt:
      name: "*"
      state: latest
      update_cache: yes
      force_apt_get: yes
  - name: actually install docker
    apt:
      name: "docker-ce"
      state: latest

Now this works pretty fine – but we still need to add docker compose – so we do the same – we follow the official documentation – which surprisingly enough do not provide a package but instruct us to curl something – not even with a “latest” tag… so this will require some maintaining – which is annoying but will do for now.

So the step are simple:

  1. Download the latest version of Docker Compose
  2. Add executable permissions to the binary

For this we use the module get_url from Ansible – which replace the curl and allow us to also define the permissions on the fly.


- name: Ensure docker-compose is installed and available
    get_url:
      url: https://github.com/docker/compose/releases/download/1.22.0/docker-compose-{{ ansible_system }}-{{ ansible_userspace_architecture }}
      dest: /usr/local/bin/docker-compose
      mode: 'u+x,g+x'

And that’s it.

As you can see we also made use of {{ansible_system}} and {{ ansible_userspace_architecture }} to replace respectively uname -s and uname -m – if you want a full list of Ansible variables available – you can go check the doc here.

That’s it!

Leave a Reply

avatar
  Subscribe  
Notify of