---
slug: "ubuntu-1604-に-docker-ce-をインストールする-ansible-playbook"
title: "Ansible Playbook for Installing Docker CE on Ubuntu 16.04"
description: "A complete Ansible playbook to install Docker CE on Ubuntu 16.04 — apt key, repository registration, and the docker-ce package install in one playbook."
url: "https://www.ytyng.com/en/blog/ubuntu-1604-に-docker-ce-をインストールする-ansible-playbook"
publish_date: "2018-02-27T01:27:56Z"
created: "2018-02-27T01:27:56Z"
updated: "2026-05-11T13:03:27.805Z"
categories: ["Ansible"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/af931d830e564784a96e5acb3f431666.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "en"
---

# Ansible Playbook for Installing Docker CE on Ubuntu 16.04

Here's my attempt...

I used the following as a reference:

[Installing Docker CE on Ubuntu - Qiita](https://qiita.com/lynlea/items/e00248dd697059969203#4-%E8%A9%A6%E3%81%99)

```yaml
---
- hosts: servers
  gather_facts: no
  become: yes
  tasks:

    - apt:
        name: apt-transport-https

    - apt:
        name: ca-certificates

    - apt:
        name: curl

    - apt:
        name: software-properties-common

    - shell: "apt list --installed |grep docker-ce"
      register: docker_ce_installed
      ignore_errors: True

    - when: docker_ce_installed|failed
      block:
        - shell: apt-key list Docker
          register: apt_key_exists

        - when: not apt_key_exists.stdout
          shell: curl -fsSL https://download.docker.com/linux/ubuntu/gpg |apt-key add -

        - shell: grep download.docker.com /etc/apt/sources.list
          register: apt_source_docker_exists
          ignore_errors: True

        - when: apt_source_docker_exists|failed
          shell: add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

        - apt:
            update_cache: yes

        - apt:
            name: docker-ce
```

Does this look about right?
