3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vagrant + virtualbox の ubuntu上にdocker-ce 17.0.6とdocker-compose 1.14.0をansibleでインストールしなおしたメモ

Last updated at Posted at 2017-07-07

久しぶりにプログラムで遊ぶ気力がわいた。
ツールを最新に更新してからのぞみたいので、更新の手間を削減できるように手間をかけた。
更新したファイルのメモ。

環境

  • windows10
  • vagrant1.9.6
  • virtualbox5.1.22
  • ubuntu-16.04
  • docker17.06
  • docker-compose1.14.0

ファイル構成

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

Ansibleの更新

virtualenvを使ってインストール・実行できるように変更した。

Vagrantfile
# ansibleインストール用shell
$ansible_install = <<SHELL
  if ! type virtualenv > /dev/null 2>&1; then
    # rootユーザとして実行されるためsudo不要
    apt-get update
    apt-get -y install curl
    apt-get -y install libffi-dev libssl-dev python-pip
    pip install --upgrade pip
    pip install virtualenv

    # vagrantユーザとしてvirtualenvとansibleをインストール
    su -c "source /vagrant/provision/bash/install_ansible.sh" vagrant
  fi
SHELL

# virtual machine設定
Vagrant.configure(2) do |config|
  # 使用するディストリビューションのボックスの設定
  config.vm.box = "bento/ubuntu-16.04"
  
  # 使用するメモリ容量を変更。 デフォルトだと512で少ないためdockerのbuildが失敗しやすい
  config.vm.provider "virtualbox" do |vm|
    vm.memory = 2048
    vm.linked_clone = true
    vm.customize [ "modifyvm", :id, "--cpus", "2", "--ioapic", "on"]
  end
  
  # ansibleをインストール
  config.vm.provision "shell", inline: $ansible_install
  # ansibleを実行
  config.vm.provision "shell", inline: <<-SHELL
    # virtualenv起動
    source /home/vagrant/venv/bin/activate
    # provision 実行
    ANSIBLE_CONFIG=/vagrant/provision/.ansible.cfg ansible-playbook -i /vagrant/provision/playbooks/inventory/hosts /vagrant/provision/playbooks/site.yml -c local
  SHELL
end
provision/bash/install_ansible.sh
# !/bin/bash

curl -kL "https://bootstrap.pypa.io/get-pip.py" | sudo python
cd $HOME
virtualenv venv
source $HOME/venv/bin/activate
pip install ansible
ansible --version
provision/.ansible.cfg
[defaults]

# 並列処理を行うプロセス数。
# 大きいほど並列に早く処理を行える。
# 大きすぎるとCPUやネット負荷に繋がる。
forks = 15
log_path = /vagrant/provision/logs/ansible.log
# Trueの場合、ターゲットノードにSSH接続するときにフィンガープリントを行う
host_key_checking = False
# ターゲットノードの詳細情報取得に関する設定
# * implicit 常に情報収集
# * explicit キャッシュを使い、情報収集を行わない
# * smart 新規に接続したときのみ情報収集。キャッシュがあればそれを使う。
gathering = smart
provision/logs/.gitignore
!.gitignore
*

dockerの更新

provision/playbooks/site.yml
- hosts: localhost
  become: yes
  become_user: root # 以下、sudoコマンドが不要になる 
  roles:
    - docker
provision/inventory/hosts
[localhost]
127.0.0.1
provision/roles/docker/vars/main.yml
---
docker_version: 17.06.0
docker_compose_version: 1.14.0
provision/roles/docker/tasks/main.yml
# https://docs.docker.com/engine/installation/linux/ubuntulinux/
- name: check docker install
  command: docker
  register: result
  ignore_errors: True
  check_mode: no
  failed_when: no
  changed_when: result.rc != 0

- block:
  - name: check docker  version
    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: Remove "docker" package
    apt:
      name: docker-ce
      state: absent
    when: ver_check|failed

  - name: check docker
    command: docker-compose
    register: result
    ignore_errors: True
    check_mode: no
    failed_when: no
    changed_when: result.rc != 0
    when: ver_check|failed
  when: result|success

- block:
  - name: step1
    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: step2
    shell: killall -KILL apt.systemd.daily
    ignore_errors: True

  - name: step3
    apt: name={{item}} state=present update_cache=yes
    with_items:
      - apt-transport-https 
      - ca-certificates 
      - curl
      - software-properties-common

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

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

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

  - name: step7
    shell: apt-get update warn=no

  - name: step8
    apt: name={{item}} state=present update_cache=yes
    with_items:
      - docker-ce

  - name: step9
    shell: apt-cache madison docker-ce warn=no

  - name: step10
    shell: gpasswd -a vagrant docker
  when: result|failed

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

- block:
  - name: check docker-compose version
    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: Remove docker-compose
    shell: rm /usr/local/bin/docker-compose
    when: ver_check_compose|failed

  - name: check docker-compose
    command: docker-compose
    register: result_compose
    ignore_errors: True
    check_mode: no
    failed_when: no
    changed_when: result_compose.rc != 0
    when: ver_check_compose|failed
  when: result_compose|success

- block:
  - name: 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|failed

更新の実行

vagrant up
vagrant provision

参考

ansible - lineinfile
ansible - apt
install docker
ubuntuのdocker-engine12とdocker-compose1.8をdocker17.03.0-ceとdocker-compose1.11.2に更新したメモ

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?