LoginSignup
0
1

More than 3 years have passed since last update.

Windows10 + VirtualBox6.1.10 + Vagrant2.2.9 + Ubuntu 20.4上でansibleを使ってdocker-ce19.03 + docker-compose1.26をインストールしたメモ

Last updated at Posted at 2020-06-11

概要

ubuntu20.04が出たので、ubuntu18.04から環境をアップデートした。
そのままvagrant upすると、virtualboxがフリーズしてしまい、タスクから殺さないとどうしようもないような状態になった。
ubuntu/focal64の起動オプションが原因だったようで、2行Vagrantfileに追記して解決。(*参考)
dockerのインストールがとても楽になっていた。
docker-compose 1.25.0がインストールされてしまうのでもとに戻した 2020/6/13

ubuntu18.04時のソース
ubuntu20.04時のソース

フォルダ構成

- Vagrantfile
- provision
  - bash
    - install_ansible.sh
  - playbooks
    - inventory
      - hosts
    - roles
      - docker
         - tasks
           - main.yml
    - site.yml
  - .ansible.cfg 

ファイル

Vagrantfile
# ansibleインストール用shell
$ansible_install = <<SHELL
if ! ansible --version > /dev/null 2>&1; then
  # apt をスクリプトで使うと警告が出る。 https://codeday.me/jp/qa/20190808/1404436.html
  apt-get install -y python3-venv python3-pip
  su -c "source /vagrant/provision/bash/install_ansible.sh" vagrant
fi
SHELL

# virtual machine設定
Vagrant.configure(2) do |config|
  # 使用するディストリビューションのボックスの設定
  config.vm.box = "ubuntu/focal64"
  # ネットワーク設定。
  config.vm.network "private_network", ip: "192.168.22.167"
  # Google AOuth
  config.vm.network "forwarded_port", guest: 9005, host: 9005

  config.vm.provider "virtualbox" do |vm|
    vm.memory = 8192
    vm.linked_clone = true
    vm.customize [ "modifyvm", :id, "--cpus", "2", "--ioapic", "on"]

    # ubuntu/focal64の起動オプションとしてttyS0が有効となっていることが原因でpanicが起こるらしい対策
    vm.customize ["modifyvm", :id, "--uart1", "0x3F8", "4"]
    vm.customize ["modifyvm", :id, "--uartmode1", "file", File::NULL]

    # vagrant-disksizeでサイズを変更する。ubuntu/bionic64は10Gくらいしかない。 "ubuntu/focal64"は40Gあるので不要かも。
    # vagrant plugin install vagrant-disksize
    config.disksize.size = '64GB'
  end

  # ansibleをインストール
  config.vm.provision "shell", inline: $ansible_install
  # ansibleを実行
  config.vm.provision "shell", inline: <<-SHELL
    timedatectl set-timezone Asia/Tokyo
    source /home/vagrant/venv3/bin/activate
    ANSIBLE_CONFIG=/vagrant/provision/.ansible.cfg ansible-playbook -i /vagrant/provision/playbooks/inventory/hosts /vagrant/provision/playbooks/site.yml -c local -v
  SHELL
end
provision/bash/install_ansible.sh
#!/bin/bash

# https://kazuhira-r.hatenablog.com/entry/2019/01/09/231800
cd $HOME
python3 -m venv venv3
source $HOME/venv3/bin/activate
sudo pip3 install ansible
ansible --version
provision/.ansible.cfg
[defaults]

# 並列処理を行うプロセス数。
forks = 15
log_path = /vagrant/provision/logs/ansible.log
# Trueの場合、ターゲットノードにSSH接続するときにフィンガープリントを行う
host_key_checking = False
# ターゲットノードの詳細情報取得に関する設定
# * smart 新規に接続したときのみ情報収集。キャッシュがあればそれを使う。
gathering = smart
provision/playbooks/site.yml
#
# site.yml
#
- hosts: localhost
  become: yes #
  become_user: root # 以下、sudoコマンドが不要になる
  roles:
    - docker
provision/playbooks/inventory/hosts
[localhost]
127.0.0.1

簡単なdocker-compose install. 2020/6/13 削除
provision/playbooks/roles/docker/tasks/main.yml
---
- name: docker-composeとdockerを同時にインストール
  apt: pkg=docker-compose

- name: そのままだとsudoが必要なのでグループにdockerを追加
  user:
    name: vagrant
    groups: vagrant,docker
- name: "プロキシ設定の修正(1)"
  lineinfile: >-
    dest='/etc/resolv.conf'
    state=present
    backrefs=yes
    regexp='^nameserver'
    line='nameserver 8.8.8.8'

2020/6/13 追記

provision/playbooks/roles/docker/tasks/main.yml
---
# Installation ubuntu
# https://docs.docker.com/engine/installation/linux/ubuntulinux/

- block:
    - name: dockerがインストールされていればバージョンを確認
      shell: docker --version | grep {{ docker_version }}
      register: ver_check
      ignore_errors: True
      check_mode: no
      failed_when: no
      changed_when: ver_check.rc != 0

    - name: debug docker version
      debug: var=ver_check

    - name: バージョンがvarsで指定されたものと異なれば、Remove "docker" package
      apt:
        name: docker-ce
        state: absent
      when: ver_check.rc != 0
      ignore_errors: True

    - name: dockerがインストールされていなければインストール
      command: docker
      register: result
      ignore_errors: True
      check_mode: no
      failed_when: no
      changed_when: result.rc != 0

- block:
    - name: debug dockerインストール
      debug: var=result

    - name: step0.5
      lineinfile:
        path: /etc/hosts
        line: "127.0.1.1 vagrant"

    # http://qiita.com/udzura/items/576c2c782adb241070bc
    # https://gist.github.com/indykish/a6facea4748dc578abbaf2b09065ead5
    - name: step1
      shell: export DEBIAN_FRONTEND=noninteractive

    # 一部環境では以下を実行しないとエラーが発生
    # aptが起動していて次のタスクが立ち上がらないエラー
    - name: step1.5
      shell: killall -KILL apt.systemd.daily
      ignore_errors: True

    - name: step2
      apt:
        name: "{{ packages }}"
        state: present
        update_cache: yes
      vars:
        packages:
          - apt-transport-https
          - ca-certificates
          - curl
          - software-properties-common

    - name: step3.1
      shell: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - warn=no
      register: res3
      ignore_errors: True

    - name: step4
      shell: apt-key fingerprint 0EBFCD88 warn=no

    - name: step5.1
      shell: add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) {{ docker_version_type }}" warn=no

    - name: step5.2
      shell: apt-get update warn=no

    - name: step6
      apt:
        name: docker-ce
        state: present
        update_cache: yes

    - name: step6.2
      shell: apt-cache madison docker-ce warn=no

    - name: step8
      shell: gpasswd -a vagrant docker
  when: result.rc != 0

- block:
    - name: docker-composeがインストールされていればバージョンを確認
      shell: docker-compose --version | grep {{ docker_compose_version }}
      register: ver_check_compose
      ignore_errors: True
      check_mode: no
      failed_when: no
      changed_when: ver_check_compose.rc != 0

    - name: debug docker-compose
      debug: var=ver_check_compose

    - name: バージョンがvarsで指定されたものと異なれば、Remove "docker-compose" package
      shell: rm /usr/local/bin/docker-compose
      ignore_errors: True
      when: ver_check_compose.rc != 0

    - name: check docker-compose
      command: docker-compose -v
      register: result_compose
      ignore_errors: True
      check_mode: no
      failed_when: no
      changed_when: result_compose.rc != 0

    - name: debug docker-compose
      debug: var=result_compose

- block:
    - name: debug docker-compose result
      debug: var=result_compose
    - name: step1 install docker-compose
      shell: curl -L "https://github.com/docker/compose/releases/download/{{ docker_compose_version }}/docker-compose-$(uname -s)-$(uname -m)" > /usr/local/bin/docker-compose &&  chmod +x /usr/local/bin/docker-compose warn=no
  when: result_compose.rc != 0

- name: "プロキシ設定の修正(1)"
  lineinfile: >-
    dest='/etc/resolv.conf'
    state=present
    backrefs=yes
    regexp='^nameserver'
    line='nameserver 8.8.8.8'

provision/playbooks/roles/docker/vars/main.yml
---
docker_version: 19.03
docker_version_type: edge
docker_compose_version: 1.26.0

参考

How to accelerate booting ubuntu VirtualBox image on vagrant
Ubuntu 20.04 LTS に docker をインストールする

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1