Vagrant †
- http://www.vagrantup.com/
- 仮想マシンの VirtualBox, Amazon EC2 + VPC, VMware Fusion, Rackspace Cloud のCUIフロントエンドに相当する、rubyで書かれたツール。プログラミングでサーバ起動/終了/設定ができる
- Vagrantfile は Ruby スクリプト。rubyの構文、関数が使える
rootパスワード †
- user: root
- password: vagrant
WSLでの問題 †
WSL2でsynced_folderが機能しない †
- vagrant 2.2.14 でWSL2のマウントタイプ "9p" がサポートされた。
- WSL2ではWindowsドライブのマウントタイプがdrvfs -> 9pfsに変わったため。
forwarded_port: ポートフォワード †
- 「auto_correct: true」host側のポートが使用済みの場合、host側の未使用のポートを使う
- 「id: "name"」 forwarded_portを複数定義する時に必要。複数定義時に、id:が未設定だとエラーになる。
config.vm.network :forwarded_port, guest: 22, host: 2222, id: "ssh", auto_correct: true
- 使用中のguest port を表示
vagrant port
手動で消したVMをvagrant global-statusからも消す †
手動でVMを消したりすると、「global-status」には残り続ける。
「.vagrant/」や「~/.vagrant/」に管理ファイルがある。
Vagrantfileがある場所以外からでも実行できる。
vagrant global-status --prune
Vagrantfile内 †
- Vagrantfile内でrubyの関数が使える。
- 「exit」で終了できる。
- 関数が定義できる。
引数の取得 †
- ARGV[0]: sub command
- ARGV[1]: option1
- Vagrantfile
p ARGV
デバッグ †
- コマンド実行時に「--debug」オプションを付ける
- ppを使って変数を見やすくダンプ
require 'pp'
pp ENV
pluginの自動インストール †
- Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
vgconfig = YAML.load_file File.join(Dir.pwd, 'vagrant.yml')
def vagrant_plugins(plugins)
plugins.each do |plugin|
unless Vagrant.has_plugin?(plugin)
# Attempt to install ourself. Bail out on failure so we don't get stuck in an
# infinite loop.
system('vagrant plugin install ' + plugin) || exit!
# Relaunch Vagrant so the plugin is detected. Exit with the same status code.
exit system('vagrant', *ARGV)
end
end
end
vagrant_plugins(vgconfig["vagrant_plugins"])
KVM用仮想マシンの作成 †
Linux上ではVirtualBoxよりKVMを使った方が起動等が早い。
構築済み環境のコピー †
初期構築に時間がかかる場合、初期構築済みのboxを保存する事で時間を短縮できる。
cloud-initを使う †
local-exec: ホストOSで実行 †
loopで複数VMの構築 †
- デフォルトパラメータを指定して、yaml側のパラメータを減らしたい
# 三項演算子を使う場合
vb.gui = host.has_key?("gui") ? host["gui"] : false
# 「var || false」等でデフォルトを指定
vmconfig.vm.network "forwarded_port", guest: fwd_port["guest"], host: fwd_port["host"], id: fwd_port["id"], auto_correct: fwd_port["auto_correct"] || false
- Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
vgconfig = YAML.load_file File.join(__dir__, 'vagrant.yml')
Vagrant.configure("2") do |config|
config.vm.box_check_update = true
config.ssh.forward_agent = true
vgconfig["hosts"].each do |host|
config.vm.define host["hostname"] do |vmconfig|
vmconfig.vm.hostname = host["hostname"]
vmconfig.vm.box = host["box"]
host["forwarded_port"].each do |fwd_port|
vmconfig.vm.network "forwarded_port", guest: fwd_port["guest"], host: fwd_port["host"]
end
if host["private_network_ip"] != ""
vmconfig.vm.network "private_network", ip: host["private_network_ip"]
end
vmconfig.vm.provider "virtualbox" do |vb|
vb.memory = host["memory"]
vb.customize ["modifyvm", :id].concat(host["customize"])
end
vmconfig.vm.synced_folder ".", "/vagrant", type: 'virtualbox'
vmconfig.vm.provision :ansible do |ansible|
ansible.playbook = host["provision"]["ansible_playbook"]
if host["provision"].has_key?("ansible_groups")
ansible.groups = host["provision"]["ansible_groups"]
end
end
end
end
end
プラグイン †
ssh経由でのコマンド実行 †
sshでの接続 †
Ansibleを使う †
shellより楽に設定できる。
- group
- 「group_vars/all.yml」または「group_vars/all/*.yml」「host_vars/<hostname>.yml」はansibleの仕様で読み込まれる。
- group nameを明示的に指定する場合。「group_vars/all.yml」「group_vars/example.yml」「host_vars/<hostname>.yml」が読み込まれる
ansible.groups = {
"all:children" => ["example"]
}
- playbook.yml に以下のようにグループ指定してある場合、ansibleのマジック変数「group_names」に「example」が必要になる。
- hosts: example
ansible.groups = {
"all:children" => ["example"],
"example" => ["web01"],
}
/vagrantディレクトリが空の場合 †
Vagrant box centos/7 - Vagrant Cloud を使った場合、ホストOSから見た「/vagrant」が空の場合がある。
コマンドラインオプション †
- vm名を省略すると「default」が指定された事になる。
# ヘルプ
vagrant -h
# Vagrantfileの検証
vagrant validate
# vagrantの状態
vagrant status
# 起動
vagrant up
# 一時停止、スリープ
vagrant suspend
# suspend状態からの復帰
vagrant resume
# Vagrantfileの変更反映(halt -> up)
vagrant reload
# provision部分の再実行(shell, ansible部分だけの実行)
vagrant provision
# 電源off
# 裏でVMが動いていると重いので、使い終わったら停止したほうが良い
vagrant halt
# vmの削除. boxのイメージは残ったまま
vagrant destroy
インストール †
- CIDRは「192.168.56.1/24」
- VirtualBoxのファイル > ホストネットワークマネージャ > アダプタを手動で設定のIPv4を確認。
- 任意の値が指定できるが、NICが増えるので注意。増えすぎると分かりにくい。
macOS †
brew install virtualbox
brew install vagrant
WSL環境 †
- Win10 Pro 1803
- WSL
- Ubuntu 18.04.2 LTS
- vagrant 2.2.14
- Memo/Windows/PackageManagement#y905b7e1 でWindowsアプリとして、VirtualBoxのインストール
- WSLのUbuntu上でvagrantのインストール。Download - Vagrant by HashiCorp で最新バージョンを確認
VAGRANT_VER=2.2.14
wget https://releases.hashicorp.com/vagrant/${VAGRANT_VER}/vagrant_${VAGRANT_VER}_x86_64.deb
sudo apt install ./vagrant_${VAGRANT_VER}_x86_64.deb
vagrant -v
Vagrant 2.2.14
- bash用の環境設定
cat ~/.bash.d/vagrant-wsl.sh
export VBOX_MSI_INSTALL_PATH="/mnt/c/Program Files/Oracle/VirtualBox"
export PATH="$PATH:$VBOX_MSI_INSTALL_PATH"
export VAGRANT_WSL_ENABLE_WINDOWS_ACCESS="1"
export VAGRANT_WSL_WINDOWS_ACCESS_USER_HOME_PATH="/mnt/c/local/home/${USER}/vagrant"
chmod +x ~/.bash.d/vagrant-wsl.sh
source ~/.bash.d/vagrant-wsl.sh
- ruby warningを抑えるために、/etc/wsl.confの設定。Memo/Windows/10/WSL#bef978d4
- pluginの追加
vagrant plugin install vagrant-vbguest
centos/7 †
mkdir ~/vagrant
cd vagrant
vagrant init centos/7
vim Vagrantfile
--
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.network "private_network", ip: "192.168.56.101"
config.vm.synced_folder ".", "/vagrant"
config.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.memory = "512"
end
end
--
vagrant up
vagrant status
vagrant ssh
[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
exit
vagrant halt
centos/7 + nginx †
- 例: CentOS 7のboxを追加して、nginxをインストール
# Vagrantfileを作成。テンプレートを作成してくれる。1ディレクトリに1ファイル。
vagrant init centos/7
# 適当にカスタマイズ
cat Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
config.vm.box = "centos/7"
config.vm.network "forwarded_port", guest: 80, host: 18080
config.vm.network "private_network", ip: "192.168.56.101"
config.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.memory = "512"
end
config.vm.synced_folder ".", "/vagrant"
config.vm.provision "shell", inline: <<-SHELL
yum install -y vim epel-release
yum install -y vim nginx
service nginx restart
chkconfig nginx on
SHELL
end
# Vagrantfileの検証
vagrant validate
Vagrantfile validated successfully.
# 起動。初回は時間がかかる。「.vagrant/」「.vagrant.d/」ディレクトリができる
vagrant up
# ステータスの確認
vagrant status
...
default running (virtualbox)
# sshでのログイン
vagrant ssh [default]
[vagrant@localhost ~]$ pwd
/home/vagrant
[vagrant@localhost ~]$ exit
- http://192.168.56.101/ を開いてnginxのデフォルトページが表示されるはず