ソフトウェアテストで生きてます。

社内で自動化エンジニアとかテストコンサルとして生きてます。でも設計好きなエンジニアの技術ブログ。

今さらだけどVagrantやってみる

タイトル通り、今さらだけど。 ansibleをやり始めたのだけど、VMware Player上でlocalhost実行で面白くないので、Vagrant利用して複数台でも動かせるようにしてみる。

前提環境

  • CentOS 7.2 (64bit)
  • sudo権限付きユーザ(na0yam)

インストール

HashicorpのサイトからVagrantダウンロードしてくる。 https://www.vagrantup.com/downloads.html

$ sudo rpm -ivh vagrant_1.9.2_x86_64.rpm
$ vagrant --version

バージョンが表示されればインストール完了。

セットアップ

VagrantはVirtual Box, VMware, Docker, Hyper-Vなどと一緒に動く。Vagrant upでそれらの上にOSを立ち上げる感じになる。 なので、その前提のツール(Provider)が必要。

今回は、VagrantのGetting Started https://www.vagrantup.com/docs/getting-started/でも使われているVitrual Boxで実行してみる。(CentOSでも動くの知らなかった。。。)

Vitrual Boxのインストール方法は割愛。ハマったのは、SDLを個別にインストール(yum install SDL)しなくちゃいけなかったくらい。

そのうち、Dockerでも試します。

最初のコマンド

$ vagrant init hashicorp/precise64
$ vagrant up
$ vagrant init hashicorp/precise64

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.

$ vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'hashicorp/precise64' could not be found. Attempting to find and install...
    default: Box Provider: virtualbox
    default: Box Version: >= 0
==> default: Loading metadata for box 'hashicorp/precise64'
    default: URL: https://atlas.hashicorp.com/hashicorp/precise64
==> default: Adding box 'hashicorp/precise64' (v1.1.0) for provider: virtualbox
    default: Downloading: https://atlas.hashicorp.com/hashicorp/boxes/precise64/versions/1.1.0/providers/virtualbox.box
==> default: Successfully added box 'hashicorp/precise64' (v1.1.0) for 'virtualbox'!
==> default: Importing base box 'hashicorp/precise64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'hashicorp/precise64' is up to date...
==> default: Setting the name of the VM: gettingStarted_default_1488715600932_47402
==> 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: 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: The guest additions on this VM do not match the installed version of
    default: VirtualBox! In most cases this is fine, but in rare cases it can
    default: prevent things such as shared folders from working properly. If you see
    default: shared folder errors, please make sure the guest additions within the
    default: virtual machine match the version of VirtualBox you have installed on
    default: your host and reload your VM.
    default: 
    default: Guest Additions Version: 4.2.0
    default: VirtualBox Version: 5.1
==> default: Mounting shared folders...
    default: /vagrant => /home/na0yam

こんなエラーが出た人は、VirtualBox on VMwareとかやってて、仮想化がONになってない人。

There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["startvm", "e0abc37a-ff58-4deb-b48e-8af93e8153fc", "--type", "headless"]

Stderr: VBoxManage: error: VT-x is not available (VERR_VMX_NO_VMX)
VBoxManage: error: Details: code NS_ERROR_FAILURE (0x80004005), component ConsoleWrap, interface IConsole

以下を参考に、vhv.enable=‘TRUE'を設定すると起動できる。

zokibayashi.hatenablog.com

オプションなしだと、vagrant upを起動した場所がVirtualBox上でもマウントされている状態になるように見える。

マシンへのログイン

同じフォルダでvagrant sshを実行すると、仮想マシンにログインした状態になる。終了するには、exitを実行。

マシンの廃棄

Vagrantfileがあるフォルダでvagrant destroyを実行する。 このコマンドは起動したVMを削除するだけで、ダウンロードしてきたVMイメージ(VagrantではBoxと呼ぶ)を削除するわけではない。

ダウンロード済みのBox一覧を見るには、vagrant box listを実行する。

$ vagrant box list
hashicorp/precise64 (virtualbox, 1.1.0)

削除する場合は、vagrant box remove hashicorp/precise64を実行する。

$ vagrant box remove hashicorp/precise64
Removing box 'hashicorp/precise64' (v1.1.0) with provider 'virtualbox'...

インターネット上に転がっているBoxたちは Discover Vagrant Boxes | Atlas by HashiCorp を参照。

この後

これで、作成、起動、終了、削除ができた。 使い方としては、作成した仮想環境にツールとかぶち込んで、それをpackageして自分用開発環境Boxを作成する感じ。 ツールとかぶち込むところはansibleに任せてしまうと、開発環境構築が楽になったりするかな。

というわけで、次回はansibleを今さらやってみることにしよう。