VagrantのプロビジョナーにAnsibleを指定する

前回はVagrantのboxにDebian 9 “Stretch"を追加して起動するとこまでを実施しました。

今回は、IPアドレスの固定と、プロビジョナーにAnsibleを指定するよう、Vagrantfileを編集します。

ディレクトリの整理

前回は~/ansible/vagrant initしていましたが、ansible用のディレクトリと疎にしたかったので、~/vagrant/に移動し、以下のようにしました。 Ansibleのディレクトリ構成は、公式ドキュメント に従っています。

Best Practices — Ansible Documentation

ansible/
    production
    staging

    group_vars/
    host_vars/

    library/
    filter_plugins/

    site.yml

    roles/
    
vagrant/
    Vagrantfile

Vagrantfile

前回vagrant initしたディレクトリに、Vagrantfileが作られています。 これを編集して、IPアドレスの指定と、プロビジョナーの設定をします。

自動生成されたVagrantfileはコメントだらけですが、コメントを除去してみるとこれだけです。

Vagrant.configure("2") do |config|
  config.vm.box = "debian/stretch64"
end
 ネットワーク設定

ネットワークの設定はconfig.vm.networkです。

  config.vm.network "private_network", ip: "192.168.33.10"
プロビジョナーの設定

プロビジョナーの設定はconfig.vm.provisionです。 Ansibleを指定します。

  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "../ansible/site.yml"
    ansible.inventory_path = "../ansible/staging"
    ansible.limit = 'all'
  end

MacにVirtualBoxとVagrantを入れて、Debian 9 "Stretch"を立ち上げる

タイトルの通り、MacVirtualBoxVagrantを入れて、Debian 9 “Stretch"を立ち上げていきます。

今回の環境

ホストOS
macOS Sierra 10.12.5 (16F73)
VirtualBox
5.1.22 r115126 (Qt5.6.2)
Vagrant
1.9.5
ゲストOS
Debian 9.0 "Stretch"

VirtualBoxのインストー

https://www.virtualbox.orgのDownloadsページから、OS X hostsのバイナリー、VirtualBox-5.1.22-115126-OSX.dmgをダウンロードしてインストールしました。

f:id:satomi-san:20170627234940j:plain

Vagrantのインストー

https://www.vagrantup.comのDownloadsページから、Mac OS X Universal (32 and 64-bit)の、vagrant_1.9.5_x86_64.dmgをダウンロードしてインストールしました。

Debian 9 “Stretch”

Discover Vagrant Boxes | Atlas by HashiCorp で、debianのboxを探してみると、debian/stretch64が公開されていましたので、これを使わせてもらいます。

$ vagrant init debian/stretch64
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.

続いて起動です。自動的にboxをDLしてくれます。

$ vagrant up --provider virtualbox
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'debian/stretch64' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'debian/stretch64'
    default: URL: https://atlas.hashicorp.com/debian/stretch64
==> default: Adding box 'debian/stretch64' (v9.0.0) for provider: virtualbox
    default: Downloading: https://atlas.hashicorp.com/debian/boxes/stretch64/versions/9.0.0/providers/virtualbox.box
==> default: Successfully added box 'debian/stretch64' (v9.0.0) for 'virtualbox'!
==> default: Importing base box 'debian/stretch64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'debian/stretch64' is up to date...
==> default: Setting the name of the VM: ansible_default_1498577443083_48557
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> 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: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Installing rsync to the VM...
==> default: Rsyncing folder: /Users/*****/ansible/ => /vagrant

==> default: Machine 'default' has a post `vagrant up` message. This is a message
==> default: from the creator of the Vagrantfile, and not from Vagrant itself:
==> default: 
==> default: Vanilla Debian box. See https://atlas.hashicorp.com/debian/ for help and bug reports

無事起動できました。

f:id:satomi-san:20170628003737j:plain

MacでAnsible

Mac Ansibleでググってみると、どうやらAnsibleでMacの環境設定をするのが流行っていたみたいですね。

私が構築したいのはMacの方じゃないので、インストール方法だけ調べてみました。

Homebrewを使ってインストールする方法もあるみたいですが、公式マニュアルhttp://docs.ansible.com/ansible/intro_installation.html#latest-releases-on-mac-osxに従って、pipでインストールしてみます。 とは言っても、マニュアル通りにコマンドを打ち込むだけですが。

まずは、which pipでpipがインストールされているか確認したところ、インストールされていなかったので、easy_installでインストールします。

$ sudo easy_install pip

続いてAnsibleのインストールです。

$ sudo pip install ansible

無事、インストールできました。

$ ansible --version
ansible 2.3.1.0
  config file = 
  configured module search path = Default w/o overrides
  python version = 2.7.10 (default, Feb  7 2017, 00:08:15) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)]
接続確認

構成管理対象をまとめたインベントリファイルを用意する必要がありますが、デフォルトは/etc/ansible/hostsです。 /etcの下はシステム全体の設定ですから、プロジェクト用に作業ディレクトリを作成し、その中で管理したいところです。 インベントリファイルは-iオプションで直接指定できますので、作業ディレクトリにhostsというファイル名でインベントリファイルを作成して実行してみます。 また、sshでパスワード認証させる為に、--ask-passオプションもつけています。

$ echo 192.168.1.200 > hosts
$ ansible -i hosts --ask-pass 192.168.1.200 -m ping
SSH password: 
192.168.1.200 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

無事、(Ansibleの)pingコマンドが実行され、変更なしと返ってきました。

MacからDebianのUSBインストーラーを作成する

早速、Ansibleを入れて、VirtualBoxを入れて、としたいところですが、仮サーバとして用意したPCが、常時起動に耐えられるのかテストの為に、stretchをインストールして放置してみたいと思います。

普段使いのクライアントはMacBook Airなので、DVDドライブなんて物は付いていません。ですから、USBメディアにdebian-installerを入れて、ブートできるようにします。

インストールUSBを作成する

1. USBメディアに書き込むイメージを用意

今回は、 https://www.debian.org/ から、"Debian 9.0(64 ビット PC ネットワーク インストーラ)“debian-9.0.0-amd64-netinst.isoをダウンロードしました。

2. 書き込み用にイメージを変換

hdiutilコマンドを使って、イメージを変換します。

構文

hdiutil convert -format UDRW -o 出力ファイル名 入力ファイル名

~/Downloads/にダウンロードされているので、以下のように変換しました。

$ hdiutil convert -format UDRW -o ~/Downloads/debian-9.0.0-amd64-netinst.img ~/Downloads/debian-9.0.0-amd64-netinst.iso 
Driver Descriptor Map(DDM: 0)を読み込み中…
Debian 9.0.0 amd64 n            (Apple_ISO: 1)を読み込み中…
Apple(Apple_partition_map: 2)を読み込み中…
Debian 9.0.0 amd64 n            (Apple_ISO: 3)を読み込み中…
EFI(Apple_HFS: 4)を読み込み中…
Debian 9.0.0 amd64 n            (Apple_ISO: 5)を読み込み中…
..............................................................................
経過時間:  2.047s
速度: 141.7Mバイト/秒
節約率: 0.0%
created: /Users/*****/Downloads/debian-9.0.0-amd64-netinst.img.dmg

拡張子.dmgが付いているので、ファイル名を変更します(そのままでも良いかも)。

$ mv ~/Downloads/debian-9.0.0-amd64-netinst.img.dmg ~/Downloads/debian-9.0.0-amd64-netinst.img
3. USBメディアに書き込み

まずはUSBメディアをMacに接続し、デバイス名を調べます。

$ diskutil list
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:          Apple_CoreStorage Macintosh HD            250.1 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3

/dev/disk1 (internal, virtual):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                  Apple_HFS Macintosh HD           +249.8 GB   disk1
                                 Logical Volume on disk0s2
                                 3B44FB5C-D057-4B80-A127-6A24FCA0B5EE
                                 Unlocked Encrypted

/dev/disk2 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *31.0 GB    disk2
   1:             Windows_FAT_32 ESD-USB                 31.0 GB    disk2s1

/dev/disk2がUSBメディアなので、rawデバイスである/dev/rdisk2にddで書き込めば良い事がわかりました。 その前に、まずはアンマウントします。

$ diskutil unMountDisk /dev/disk2
Unmount of all volumes on disk2 was successful

続いて、ddコマンドを使って、先ほど変換したイメージを書き込みます。

$ sudo dd if=~/Downloads/debian-9.0.0-amd64-netinst.img of=/dev/rdisk2 bs=1m
Password:
290+0 records in
290+0 records out
304087040 bytes transferred in 2.365862 secs (128531188 bytes/sec)

以上でインストールUSBが出来上がったので、インストールしたいPCに接続して、USBメディアから起動します。

Debian 9に移行しようと思い立つ(squeezeからstretch)

はじめに

自宅サーバがsqueezeのままアップグレードしていませんでした(Securityアップグレードは当ててたけど)。

言い訳は色々あるけど、以前jessieに上げようとした時に、Xenがらみで色々トラブって、時間切れもあって元に戻してました。その後、別の機会に、dom0と一部の仮想サーバはwheezyにまでは上げられたのですが、メインのサーバはsqueezeのままという、なんとも中途半端な状態になってます。

先日、いい加減アップグレードしなきゃと思い腰を上げ、移行中に仮サーバ用として使うPCを用意したり、バックアップ用のディスクを用意したりしてたら、stretchが出てたと言う次第です。

メールとsambaだけ仮運用すれば、後はゆっくりでも良いのですが、ゆっくりしてるとまたいつになるかわからないので、移行は一気にやろうと思います。
当初予定では、jessieに切り替えるつもりでしたけど、stretchの情報を集めながらになるので、いきなり本番移行ではなく、仮想環境で一通り試してからにします(なんか、network/interfacesの書き方からして違うって聞くし)。

基本方針

  • 設計がわりに、やることを記事にしていく。
  • 途中でやり直ししたり、最終的には既存環境で再構築するので、構成管理ツールとしてAnsibleを使う。