Post date:

Ansible を利用した Docker 環境構築メモ

初めて Ansible に触れたので、体験メモとして記事を書きました。

事前準備

pip から Ansible をインストールします。Windows ホストの場合は WSL 環境上でインストールする必要があります。1

python3 -m pip install --user ansible

Ansible playbook

Docker 公式ドキュメント2を参考に、構成ファイルを用意します。

hosts

hosts ファイルでは、構築対象のリモートマシンの情報を記述します。このファイルを、Ansible ではインベントリファイルと呼びます。

例えば、リモートマシンの IP アドレスが 192.168.11.11 、SSHログインのユーザー名が onamae 、使用する秘密鍵ファイルのパスが ~/.ssh/id_ed25519 であれば、次のように記述します。

[node]
192.168.11.11

[node:vars]
ansible_user=onamae
ansible_ssh_private_key_file="~/.ssh/id_ed25519"

playbook.yml

パッケージのインストールは、原則スーパーユーザで実行する必要があります。そこで become: true とパラメータを指定すると、管理者権限上で実行することができます。

# playbook.yml
- hosts: node
  become: true
  gather_facts: false
  tasks:
    - name: Delete docker.gpg and docker.list files if they exist
      file:
        path: "{{ item }}"
        state: absent
      with_items:
        - /usr/apt/keyrings/docker.gpg
        - /etc/apt/sources.list.d/docker.list
    - name: Uninstall old docker packages
      apt:
        pkg:
          - docker
          - docker-engine
          - docker.io
          - containerd
          - runc
        state: absent
        update_cache: yes
    - name: Install required packages to install docker
      apt:
        pkg:
          - ca-certificates
          - curl
          - gnupg
          - lsb-release
        state: latest
        update_cache: yes
    - name: Create keyrings dir
      file:
        path: /usr/apt/keyrings
        state: directory
    - name: Append official Docker GPG key
      shell: "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor --batch --yes -o /usr/apt/keyrings/docker.gpg"
    - name: Set up the stable repository
      shell: 'echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/apt/keyrings/docker.gpg]
        https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null'
    - name: Change permission of docker.gpg and docker.list to make them readable by all users
      file:
        path: "{{ item }}"
        mode: a+r
      with_items:
        - /usr/apt/keyrings/docker.gpg
        - /etc/apt/sources.list.d/docker.list
    - name: Install docker packages
      apt:
        pkg:
          - docker-ce
          - docker-ce-cli
          - containerd.io
          - docker-compose-plugin
        state: latest
        update_cache: yes
    - name: Chceck if docker-compose command exists
      shell: "docker compose --version"
      register: docker_compose_version
      ignore_errors: false
    - name: Create docker group
      group:
        name: docker
        state: present
    - name: Append present user to docker group
      user:
        name: "{{ ansible_user }}"
        groups: docker
        append: yes
    - name: Enable docker service
      service:
        name: docker
        state: started
        enabled: yes

コマンド実行

二つのファイルを用意したところで、次のコマンドを実行します

ansible-playbook playbook.yml -i hosts -K

コマンド説明

$ ansible-playbook --help3 を参照していただければ明らかなことですが、簡単なコマンドの説明です。

  • -i hosts
    • インベントリファイル hosts を指定します。
  • -K (または -ask-become-pass)
    • 権限昇格パスワードを入力するオプションです。

参考

1. “Installation Guide — Ansible Documentation,” Ansible.com, 2021. https://docs.ansible.com/ansible/latest/installation_guide/ (accessed Mar. 27, 2023).
2. “Install Docker Engine on Ubuntu,” Docker Documentation, Mar. 26, 2023. https://docs.docker.com/engine/install/ubuntu/ (accessed Mar. 27, 2023).
3. “ansible-playbook — Ansible Documentation,” Ansible.com, 2021. https://docs.ansible.com/ansible/latest/cli/ansible-playbook.html (accessed Mar. 27, 2023).

results matching ""

    No results matching ""