読者です 読者をやめる 読者になる 読者になる

初老のボケ防止日記

おっさんのひとりごとだから気にしないようにな。

スポンサーリンク

今更だけどVagrantでも使ってみるか(終)



前回までのあらすじ

実際の所、第2回目位から薄々わかっていたのだが、既にVagrantについて何も語っていないのでこの連載を終わりにする。ということでこれまでとっちらかっていた手順を整理してみたのである。つまり走馬灯である。

今更だけどVagrantでも使ってみるか(5) - 初老のボケ防止日記

環境

  • Vagrantを動かす環境(ホスト)
OS Windows 8.1(x64)
VirtualBox 4.3.20
Vagrant 1.6.5
Packer 0.7.2
  • Vagrantで起動する仮想環境(ゲスト)
OS Ubuntu 14.04(x64)
Docker 1.4.1
Ansible 1.8.2

PackerでVagrant向けのboxを作成する。

ホストのPackerでVagrant向けのboxを生成するまでの流れ

テンプレートをもってくる

We've moved · GitHub

$ git clone https://github.com/box-cutter/ubuntu-vm
$ ls ubuntu-vm/
AUTHORS       script                   ubuntu1404-desktop.json
CHANGELOG.md  test                     ubuntu1404-docker.json
LICENSE       tpl                      ubuntu1404-i386.json
Makefile      ubuntu1004-i386.json     ubuntu1404.json
README.md     ubuntu1004.json          ubuntu1410-docker.json
VERSION       ubuntu1204-desktop.json  ubuntu1410-i386.json
bin           ubuntu1204-docker.json   ubuntu1410.json
box           ubuntu1204-i386.json
http          ubuntu1204.json

ローカライズしてみる

  • http/preseed.chg
$ git diff
diff --git a/http/preseed.cfg b/http/preseed.cfg
index 2f1beb0..d4c74aa 100644
--- a/http/preseed.cfg
+++ b/http/preseed.cfg
@@ -18,7 +18,19 @@
 # but you must provide a hostname as a boot parameter to prevent the Ubuntu
 # install from prompting for a hostname

-choose-mirror-bin mirror/http/proxy string
+#choose-mirror-bin mirror/http/proxy string
+d-i mirror/country string manual
+d-i mirror/http/hostname string ftp.jaist.ac.jp
+d-i mirror/http/directory string /pub/Linux/ubuntu
+d-i mirror/http/proxy string
+
+d-i debian-installer/language string ja
+d-i debian-installer/country string japan
+d-i debian-installer/locale string ja_JP.UTF-8
+d-i localechooser/supported-locales ja_JP.UTF-8
+d-i console-setup/ask_detect boolean true
+d-i console-setup/layoutcode string jp
+d-i console-setup/charmap select UTF-8
 d-i base-installer/kernel/override-image string linux-server
 d-i clock-setup/utc boolean true
 d-i clock-setup/utc-auto boolean true
@@ -51,7 +63,9 @@ d-i pkgsel/install-language-support boolean false
 # "unattended-upgrades" (install security updates automatically), or
 # "landscape" (manage system with Landscape).
 d-i pkgsel/update-policy select none
-d-i pkgsel/upgrade select full-upgrade
+#d-i pkgsel/upgrade select full-upgrade
+d-i pkgsel/upgrade select none
 #d-i time/zone string UTC
-d-i time/zone string US/Pacific
+#d-i time/zone string US/Pacific
+d-i time/zone string Japan
 tasksel tasksel/first multiselect standard, ubuntu-server

Ansibleインストール用のスクリプトを新規作成

前回のものだとansibleが古いので最新をインストールするように修正

  • scripts/ansible.sh
#!/bin/bash -eux

echo "==> Installing Ansible"
apt-get -y install software-properties-common
apt-add-repository ppa:ansible/ansible
apt-get -y update
apt-get -y install ansible

Vagrantfileをカスタマイズしたテンプレートを新規作成

デフォルトのVagrantの共有フォルダ設定だと、VM側のAnsibleでファイルを読むとエラーになるのでマウントの際のPermission変更

  • tpl/vagrantfile-ubuntu1404-docker-ansible.tpl
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu1404-docker-ansible"
  config.vm.synced_folder ".", "/vagrant", :mount_options => ['dmode=775', 'fmode=664']
end

これらを反映したPackerのテンプレートを新規作成

$ cp ubuntu1404-docker.json ubuntu1404-docker-ansible.json
$ vi ubuntu1404-docker-ansible.json
$ diff -u ubuntu1404-docker.json ubuntu1404-docker-ansible.json
--- ubuntu1404-docker.json      Fri Dec  5 11:36:26 2014
+++ ubuntu1404-docker-ansible.json      Wed Dec 17 11:28:14 2014
@@ -5,7 +5,7 @@
     "cm_version": "",
     "cleanup_pause": "",
     "headless": "",
-    "update": "false",
+    "update": "true",
     "http_proxy": "{{env `http_proxy`}}",
     "https_proxy": "{{env `https_proxy`}}",
     "ftp_proxy": "{{env `ftp_proxy`}}",
@@ -56,7 +56,7 @@
     }
   },
   {
-    "vm_name": "ubuntu1404-docker",
+    "vm_name": "ubuntu1404-docker-ansible",
     "type": "virtualbox-iso",
     "http_directory": "http",
     "iso_url": "{{ user `iso_url` }}",
@@ -139,12 +139,14 @@
       "script/vmtool.sh",
       "script/cmtool.sh",
       "script/docker.sh",
+      "script/ansible.sh",
       "script/cleanup.sh"
     ]
   }],
   "post-processors": [{
     "type": "vagrant",
     "keep_input_artifact": false,
-    "output": "box/{{.Provider}}/ubuntu1404-docker-{{user `cm`}}{{user `cm_version`}}-{{user `version`}}.box"
+    "vagrantfile_template": "tpl/vagrantfile-ubuntu1404-docker-ansible.tpl",
+    "output": "box/{{.Provider}}/ubuntu1404-docker-ansible-{{user `cm`}}{{user `cm_version`}}-{{user `version`}}.box"
   }]
 }

boxをビルドする*1

$ packer build -only=virtualbox-iso ubuntu1404-docker-ansible.json

==> virtualbox-iso: Downloading or copying Guest additions
    virtualbox-iso: Downloading or copying: file:///C:/App/VirtualBox/VBoxGuestAdditions.iso

==> virtualbox-iso: Downloading or copying ISO
    virtualbox-iso: Downloading or copying: http://releases.ubuntu.com/14.04/ubuntu-14.04.1-server-amd64.iso
==> virtualbox-iso: Starting HTTP server on port 8304
==> virtualbox-iso: Creating virtual machine...
==> virtualbox-iso: Creating hard drive...
==> virtualbox-iso: Creating forwarded port mapping for SSH (host port 4230)
==> virtualbox-iso: Executing custom VBoxManage commands...
    virtualbox-iso: Executing: modifyvm ubuntu1404-docker --memory 512
    virtualbox-iso: Executing: modifyvm ubuntu1404-docker --cpus 1
==> virtualbox-iso: Starting the virtual machine...
==> virtualbox-iso: Waiting 10s for boot...
==> virtualbox-iso: Typing the boot command...
==> virtualbox-iso: Waiting for SSH to become available...
==> virtualbox-iso: Connected to SSH!
==> virtualbox-iso: Uploading VirtualBox version info (4.3.20)
==> virtualbox-iso: Uploading VirtualBox guest additions ISO...
==> virtualbox-iso: Provisioning with shell script: script/update.sh
(snip)
==> virtualbox-iso: Provisioning with shell script: script/vagrant.sh
    virtualbox-iso: [sudo] password for vagrant: ==> Giving vagrant sudo powers
    virtualbox-iso: ==> Installing vagrant key
==> virtualbox-iso: Provisioning with shell script: script/sshd.sh
==> virtualbox-iso: Provisioning with shell script: script/vmtool.sh
    virtualbox-iso: ==> Installing VirtualBox guest additions
(snip)
==> virtualbox-iso: Provisioning with shell script: script/cmtool.sh
    virtualbox-iso: ==> Building box without baking in a configuration management tool
==> virtualbox-iso: Provisioning with shell script: script/docker.sh
(snip)
==> virtualbox-iso: Provisioning with shell script: script/ansible.sh
(snip)
==> virtualbox-iso: Provisioning with shell script: script/cleanup.sh
(snip)
==> virtualbox-iso: Gracefully halting virtual machine...
==> virtualbox-iso: Preparing to export machine...
==> virtualbox-iso: Exporting virtual machine...
==> virtualbox-iso: Unregistering and deleting virtual machine...
==> virtualbox-iso: Running post-processor: vagrant
==> virtualbox-iso (vagrant): Creating Vagrant box for 'virtualbox' provider
 virtualbox-iso (vagrant): Copying from artifact: output-virtualbox-iso\ubuntu1404-docker-ansible-disk1.vmdk
    virtualbox-iso (vagrant): Copying from artifact: output-virtualbox-iso\ubuntu1404-docker-ansible.ovf
    virtualbox-iso (vagrant): Renaming the OVF to box.ovf...
    virtualbox-iso (vagrant): Using custom Vagrantfile: tpl/vagrantfile-ubuntu14
    virtualbox-iso (vagrant): Compressing: Vagrantfile
    virtualbox-iso (vagrant): Compressing: box.ovf
    virtualbox-iso (vagrant): Compressing: metadata.json
    virtualbox-iso (vagrant): Compressing: ubuntu1404-docker-ansible-disk1.vmdk
Build 'virtualbox-iso' finished.

==> Builds finished. The artifacts of successful builds are:
--> virtualbox-iso: 'virtualbox' provider box: box/virtualbox/ubuntu1404-docker-ansible-nocm-0.1.0.box

Packerで作成したboxでVagrantを起動する

Packerで作成したboxを生成してVagrantで起動するまでの流れ

boxをadd

$ vagrant box add ubuntu1404-docker-ansible box/virtualbox/ubuntu1404-docker-ansible-nocm-0.1.0.box
==> box: Adding box 'ubuntu1404-docker-ansible' (v0) for provider:
    box: Downloading: file://c:/AppData/Packer/ubuntu-vm/box/virtualbox/ubuntu1404-docker-ansible-nocm-0.1.0.box
    box: Progress: 100% (Rate: 631M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'ubuntu1404-docker-ansible' (v0) for 'virtualbox'!
$ vagrant box list
ubuntu1404-docker-ansible (virtualbox, 0)

登録したboxを元にVagrantを初期化*2

$ mkdir test
$ cd test
$ vagrant init ubuntu1404-docker-ansible
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

VM側でのAnsible動作確認用のファイルを共有フォルダのマウント先で作成

$ mkdir ansible
$ echo "127.0.0.1 ansible_connection=local" > ansible/hosts

Vagrantを起動する*3

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'ubuntu1404-docker-ansible'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: test1_default_1418788213958_81968
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => C:/AppData/Vagrant/test1

Vagrantで起動したVMにログインする

$ vagrant ssh
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-43-generic x86_64)

 * Documentation:  https://help.ubuntu.com/
Last login: Wed Dec 17 12:46:27 2014 from 10.0.2.2
vagrant@ubuntu1404-docker-ansible:~$ docker version
Client version: 1.4.1
Client API version: 1.16
Go version (client): go1.3.3
Git commit (client): 5bc2ff8
OS/Arch (client): linux/amd64
Server version: 1.4.1
Server API version: 1.16
Go version (server): go1.3.3
Git commit (server): 5bc2ff8
vagrant@ubuntu1404-docker-ansible:~$ ansible --version
ansible 1.8.2
  configured module search path = None
vagrant@ubuntu1404-docker-ansible:~$ ansible -i /vagrant/ansible/hosts all -m p ing
127.0.0.1 | success >> {
    "changed": false,
    "ping": "pong"
}

感想

暫く触ってみて、Vagrant側でゴニョゴニョするよりもPackerとAnsibleでやったほうが後々Dockerとかに移行する場合に楽なんじゃないかなーとか思いました。それでも一度boxなり作ってしまえば気軽にVMを作ったり壊したりできるのは非常によい。ここ暫くはVirtualBoxのGUI触ってないのでこれからもVagrantを中心に使ってみようかと今は思っている。でも年が明けたらわからないけど。

"オレはようやくのぼりはじめたばかりだからな。このはてしなく遠いVagrant坂をよ…(未完)"


実践 Vagrant

実践 Vagrant

Vagrant入門ガイド

Vagrant入門ガイド

*1:Box-Cutterはmakeで実行なんだけどWindowsでmakeが動かないので直で実行

*2:この時点ではVagrantfileはカスタマイズしたものではない

*3:この時点でカスタマイズしたVagrantfileがマージされているような

スポンサーリンク