VMwareが無償化されたので使いやすくなった。今後のサポートが心配だが
記事:
find .vagrant.d/ -name vagrant_private_key | xargs chmod 600
エラー:
sudo apt update ... GPG error: https://apt.releases.hashicorp.com
修正:
sudo apt update && sudo apt install gpg wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg Enter new filename: /usr/share/keyrings/hashicorp-archive-keyring.gpg File '/usr/share/keyrings/hashicorp-archive-keyring.gpg' exists. Overwrite? (y/N) y # 確認: 798A EC65 4E5C 1542 8C8E 42EE AA16 FCBC A621 E701 なら正常 gpg --no-default-keyring --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg --fingerprint
vagrant up centos7 ... centos7: Warning: Connection refused. Retrying...
vagrant plugin install virtualbox_WSL2
New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow
vagrant reload centos7
vagrant ssh centos7
sudo service network restart
記事:
vmconfig.vm.synced_folder ".", "/vagrant", type: '9p', disabled: false
mount ... /dev/sdb on / type ext4 (rw,relatime,discard,errors=remount-ro,data=ordered) ... C:\ on /mnt/c type 9p (rw,noatime,dirsync,aname=drvfs;path=C:\;uid=1000;gid=1000;metadata;umask=22;fmask=11;symlinkroot=/mnt/,mmap,access=client,msize=65536,trans=fd,rfd=8,wfd=8) ...
# start
vagrant rsync-auto
vmconfig.vm.synced_folder "/mnt/c/tmp", "/vagrant", type: 'virtualbox', disabled: false
config.vm.network :forwarded_port, guest: 22, host: 2222, id: "ssh", auto_correct: true
vagrant port
手動でVMを消したりすると、「global-status」には残り続ける。
「.vagrant/」や「~/.vagrant/」に管理ファイルがある。
Vagrantfileがある場所以外からでも実行できる。
vagrant global-status --prune
p ARGV
vagrant box update ["box", "update"]
# 環境変数の表示 p ENV printf("env: USER: %s\n", ENV["USER"])
require 'pp' pp ENV
vagrant_plugins: - vagrant-vbguest - vagrant-s3auth
# -*- 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"])
Linux上ではVirtualBoxよりKVMを使った方が起動等が早い。
初期構築に時間がかかる場合、初期構築済みのboxを保存する事で時間を短縮できる。
config.push.define "local-exec" do |push| push.inline = <<-SCRIPT [ -d example ] || git clone git@github.com:example/example.git SCRIPT end
vagrant push
provision: ansible_playbook: playbook.vagrant.provision.centos.yml ansible_groups: "all:children": - "example" "example": - "web01"
# 三項演算子を使う場合 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
case param[0] param_sym = param[0] when ':id' then param_sym = :id end
- hosts: all gather_facts: true become: false tasks: - debug: var: "{{ item }}" with_items: - group_names - groups
hosts: - hostname: "centos6" box: "centos/6" private_network_ip: "192.168.56.10" forwarded_port: - guest: 80 host: 18080 memory: 512 provision: ansible_playbook: playbook.vagrant.provision.centos.yml - hostname: "centos7" box: "centos/7" private_network_ip: "192.168.56.11" forwarded_port: - guest: 80 host: 28080 memory: 512 provision: ansible_playbook: playbook.vagrant.provision.centos.yml
# -*- 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 \ host.has_key?("synced_folder") && host["synced_folder"].has_key?("src") ? host["synced_folder"]["src"] : '.', host.has_key?("synced_folder") && host["synced_folder"].has_key?("dest") ? host["synced_folder"]["dest"] : '/vagrant', type: host.has_key?("synced_folder") && host["synced_folder"].has_key?("type") ? host["synced_folder"]["type"] : 'virtualbox', disabled: host.has_key?("synced_folder") && host["synced_folder"].has_key?("disabled") ? host["synced_folder"]["disabled"] : false 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
vagrant ssh [default] -c 'chkconfig --list'
vagrant ssh [default]
vagrant ssh-config [default] > ssh-config # ssh ssh -F ssh-config default # scpでファイルコピー scp -F ssh-config dummy.txt default:/tmp/
shellより楽に設定できる。
|-- Vagrantfile |-- /provisioning | |-- /group_vars | |-- all.yml | |-- example.yml | |-- /host_vars | |-- web01.yml | |-- /roles | |-- /bar | |-- /foo | |-- playbook.yml
.vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory
ansible.groups = { "all:children" => ["example"] }
- hosts: example
ansible.groups = { "all:children" => ["example"], "example" => ["web01"], }
require 'yaml' group_vars = YAML.load_file File.join(Dir.pwd, 'group_vars/all.yml')
Vagrant box centos/7 - Vagrant Cloud を使った場合、ホストOSから見た「/vagrant」が空の場合がある。
vagrant plugin install vagrant-vbguest # up済みvmのバージョンをチェック vagrant vbguest [vmname] --status # 更新 vagrant vbguest [vmname] # vm再起動 vagrant reload [vmname]
# ヘルプ 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
# help vagrant box # インストール済みboxの一覧 vagrant box list centos/7 (virtualbox, 1902.01)
config.vm.synced_folder ".", "/vagrant"
Apple Silicon(M1/M2):
Intel macOS:
brew install virtualbox brew install vagrant
sudo apt update sudo apt list vagrant -a sudo apt install vagrant=2.2.16
cat ~/.bash.d/vagrant-wsl.sh export PATH="$PATH:/mnt/c/Program Files/Oracle/VirtualBox" 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
vagrant plugin install vagrant-vbguest
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
# 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