Help us understand the problem. What is going on with this article?

【Ubuntu 18.04】Ansible で Docker と docker-compose をインストールする

業務で Ansible を使い始めたので、プライベートの Ubuntu マシンも Ansible で環境構築中です
公式サイトでの手順を Ansible のお作法にのっとって書き直しただけですが、無事にインストールできました🎉

【参考】
https://docs.docker.com/install/linux/docker-ce/ubuntu/
https://docs.docker.com/compose/install/
https://gist.github.com/rbq/886587980894e98b23d0eee2a1d84933

前提

  • Ansible Roles を使用
  • sudo のパスワードは別途指定が必要
  • Docker/docker-compose のバージョンを固定したい場合はそれぞれ追加の指定が必要

できたもの

roles/docker/tasks/main.yml
---
- name: Add docker GPG key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
  become: yes

- name: Install basic list of packages
  apt:
    name: "{{ packages }}"
    state: present
    update_cache: yes
  vars:
    packages:
      - apt-transport-https
      - ca-certificates
      - curl
      - gnupg2
      - software-properties-common
  become: yes

- name: Add apt repository
  apt_repository:
    repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
  become: yes

- name: Install Docker packages
  apt:
    name: "{{ packages }}"
    state: present
  vars:
    packages:
      - docker-ce
      - docker-ce-cli
      - containerd.io
  become: yes

- name: Add user to docker group
  user:
    name: "{{ ansible_env.USER }}"
    groups: docker
    append: yes
  become: yes

- name: Ensure docker service is enabled
  systemd:
    name: docker
    state: started
    enabled: yes
  become: yes

- name: Install docker-compose
  get_url:
    url: "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-{{ ansible_system }}-{{ ansible_architecture }}"
    dest: /usr/local/bin/docker-compose
    mode: +x
  become: yes

一部解説

GPG Key

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

公式では上記コマンドですが、apt-key モジュールのパラメータで url が指定できるので簡潔にできます

- name: Add docker GPG key
  apt_key:
    url: https://download.docker.com/linux/ubuntu/gpg
  become: yes

Docker 用リポジトリ

$ sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

lsb_release -cs は bionic や xenial などの ubuntu のリリースバージョンを取得するコマンドですが
Ansible では gather_facts を有効にしていれば ansible_distribution_release で取得できます

- name: Add apt repository
  apt_repository:
    repo: "deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
  become: yes

docker-compose

$ sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
$ sudo chmod +x /usr/local/bin/docker-compose

公式は curl なので get_url モジュールを使います
uname -s/uname -m はそれぞれ ansible_system/ansible_architecture で取得しています
また、ダウンロード先のパーミッションも同時に指定できるので1回のタスクで済みます

- name: Install docker-compose
  get_url:
    url: "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-{{ ansible_system }}-{{ ansible_architecture }}"
    dest: /usr/local/bin/docker-compose
    mode: +x
  become: yes
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away