Vagrantを使ってみる
お疲れ様です。伊藤です。
自動化について調べていくとVagrant(ベイグラント)によくあたります。要はVirtualBoxを使った仮想マシンの作成→OSインストールがすべてワンクリックで出来る!というものらしいです。よくわかってなかったのでこれを機に使ってみたいと思います。
まずは公式HPからダウンロード。
www.vagrantup.com
で、ダウンロードしたファイルをクリックしてインストール。ここで注意!インストール後強制的にWindowsが再起動されます。うかつに進めると再起動走るので注意してください。(この記事も消えかけた)
インストール後は下記からBoxファイルというOSイメージが入ったファイルをダウンロード
https://atlas.hashicorp.com/search
って、ちょっとこれは怖いので今回はやめておきます。OSイメージとかは正規のものを用意して、自前でBoxファイルを作ります。
とりあえずVirtualBoxを起動して、いったんRHEL7.3をインストールした仮想マシンを用意します。それを基にBoxファイルを作成していきましょう。
~仮想マシン構築部分は割愛~
実際には↑の作業は20~30分かかるのでこの作業がどれくらいに短縮できるかは楽しみです。
仮想マシンの用意が終わりました。仮想マシン名はrhel7_3です。
では早速Vagrantを動かしていきます。
まずはインストール確認
C:\Users\test>vagrant -v Vagrant 1.9.7
無事インストールされているようです。
C:\Users\test>mkdir C:\Vagrant & cd C:\Vagrant C:\Vagrant> C:\Vagrant>mkdir rhel73 & cd rhel73 C:\Vagrant\rhel73>
Boxファイルがない事を確認
C:\Vagrant\rhel73>vagrant box list There are no installed boxes! Use `vagrant box add` to add some.
Boxファイル作成
C:\Vagrant\rhel73>vagrant package --base rhel7_3 ==> rhel7_3: Exporting VM... ==> rhel7_3: Compressing package to: C:/Vagrant/rhel73/package.box
ちょっと時間がかかりますがこれでBoxファイルが出来ました。
package.boxというファイルが530Mbyteでできています。
これをVagrantに登録
C:\Vagrant\rhel73>vagrant box add --name rhel7.3-x86_64-ja-20170802 package.box ==> box: Box file was not detected as metadata. Adding it directly... ==> box: Adding box 'rhel7.3-x86_64-ja-20170802' (v0) for provider: box: Unpacking necessary files from: file://C:/Vagrant/rhel7_3/package.box box: Progress: 100% (Rate: 890M/s, Estimated time remaining: --:--:--) ==> box: Successfully added box 'rhel7.3-x86_64-ja-20170802' (v0) for 'virtualbox'!
上手く行ったっぽい。-nameのあとの名前は適当です。
C:\Vagrant\rhel73>vagrant box list rhel7.3-x86_64-ja-20170802 (virtualbox, 0)
これを基にVagrantファイルを作成。
C:\Vagrant\rhel73>vagrant init rhel7_3 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.
Vagrantfileというファイルが出来ました。早速マシンを作って起動してみましょう。
その前に先ほど作った仮想マシンは削除して。。。。実行!
C:\Vagrant\rhel73>vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'rhel7_3' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Box file was not detected as metadata. Adding it directly... ==> default: Adding box 'rhel7_3' (v0) for provider: virtualbox default: Downloading: rhel7_3 default: An error occurred while downloading the remote file. The error message, if any, is reproduced below. Please fix this error and try again. Couldn't open file /C:/Vagrant/rhel73/rhel7_3
あれえ。。。
なんかファイルが開けないって言われました。どうやら先ほどのpackage.boxを
rhel7_3という名前で読みに行きたい様子。ファイル名を変更して再開!
C:\Vagrant\rhel73>vagrant up Bringing machine 'default' up with 'virtualbox' provider... ==> default: Box 'rhel7_3' could not be found. Attempting to find and install... default: Box Provider: virtualbox default: Box Version: >= 0 ==> default: Box file was not detected as metadata. Adding it directly... ==> default: Adding box 'rhel7_3' (v0) for provider: virtualbox default: Unpacking necessary files from: file://C:/Vagrant/rhel73/rhel7_3 default: Progress: 100% (Rate: 972M/s, Estimated time remaining: --:--:--) ==> default: Successfully added box 'rhel7_3' (v0) for 'virtualbox'! ==> default: Importing base box 'rhel7_3'... ==> default: Matching MAC address for NAT networking... ==> default: Setting the name of the VM: rhel7_3_default_1501639887630_57662 ==> 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 Timed out while waiting for the machine to boot. This means that Vagrant was unable to communicate with the guest machine within the configured ("config.vm.boot_timeout" value) time period. If you look above, you should be able to see the error(s) that Vagrant had when attempting to connect to the machine. These errors are usually good hints as to what may be wrong. If you're using a custom box, make sure that networking is properly working and you're able to connect to the machine. It is a common problem that networking isn't setup properly in these boxes. Verify that authentication configurations are also setup properly, as well. If the box appears to be booting properly, you may want to increase the timeout ("config.vm.boot_timeout") value.
...なんかタイムアウトになってる。。
おそらくこれは元のマシンがネットワークの設定とかしてないのでsshの設定を勝手にやろうとして失敗している様子。てかVagrantってSSHの設定やっちゃうのね。。
Virtualboxを開いてみるとrhel7_3_default_1501639887630_57662という名前の仮想マシンが出来て起動しています。
とりあえずこれで自動的にマシンを作ることはできました。
時間もかなり短縮されそうです(おそらくマシンを作るだけなら3分ぐらいで終わっている様子)ただし、色々と設定する項目があるようですね。
とりあえず今回は以上です。お疲れ様でした。