![]() |
Ansible Vagrant のバックアップ(No.3) |
[~]$ vagrant version
Installed Version: 1.8.1
Latest Version: 1.8.1
You're running an up-to-date version of Vagrant!
[~]$ VBoxManage -v
5.0.14r105127
[~/sourcetree/AnsibleExam/centos7]$ vagrant init centos/7; vagrant up --provider virtualbox
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.
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Box 'centos/7' could not be found. Attempting to find and install...
default: Box Provider: virtualbox
default: Box Version: >= 0
==> default: Loading metadata for box 'centos/7'
default: URL: https://atlas.hashicorp.com/centos/7
==> default: Adding box 'centos/7' (v1603.01) for provider: virtualbox
default: Downloading: https://atlas.hashicorp.com/centos/boxes/7/versions/1603.01/providers/virtualbox.box
==> default: Successfully added box 'centos/7' (v1603.01) for 'virtualbox'!
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' is up to date...
==> default: Setting the name of the VM: centos7_default_1460477307404_68841
==> 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: Warning: Remote connection disconnect. Retrying...
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: Rsyncing folder: /Users/atsushi/sourcetree/AnsibleExam/centos7/ => /home/vagrant/sync
[~/sourcetree/AnsibleExam/centos7]$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.11.3
BuildVersion: 15D21
[~/sourcetree/AnsibleExam/centos7]$ vagrant ssh
[vagrant@localhost ~]$ cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[~/sourcetree/AnsibleExam/centos7]$ vagrant ssh-config
Host default
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile "/Users/atsushi/sourcetree/AnsibleExam/centos7/.vagrant/machines/default/virtualbox/private_key"
IdentitiesOnly yes
LogLevel FATAL
[~/sourcetree/AnsibleExam/centos7]$ ssh vagrant@127.0.0.1 -p 2222 -i .vagrant/machines/default/virtualbox/private_key
こうしてもいい
[~/sourcetree/AnsibleExam/centos7]$ vagrant ssh-config >> ~/.ssh/config
[~/sourcetree/AnsibleExam/centos7]$ ssh default
[~/sourcetree/AnsibleExam/centos7]$ vagrant destroy
default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...
[~/sourcetree/AnsibleExam/centos7]$ cat Vagrantfile | egrep -v "^[ ]+#.*$" | egrep -v "^$"
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
config.vm.box = "centos/7"
end
まぁ雛形ですね。コメントてんこ盛りMEMO
仮想マシンを起動 vagrant up
仮想マシンにログイン vagrant ssh
仮想マシンの状態を確認 vagrant status
仮想マシンを停止 vagrant halt
仮想マシンを削除 vagrant destroy
Vagrantfile を生成 vagrant init [BOX-NAME] [BOX-URL]
vagrant reload halt+up
vagrant suspend
vagrant resume
vagrant ssh-config
vagrant ssh-config >> ~/.ssh/config
--
仮想マシンへの実験的な変更を何度も行いたい時に使う
$ vagrant plugin install sahara
$ vagrant plugin list
サンドボックスに入る vagrant sandbox on
サンドボックスの状態 vagrant sandbox status
サンドボックスのロールバック vagrant sandbox rollback
サンドボックスのコミット vagrant sandbox commit
サンドボックスから出る vagrant sandbox off
commit, rollback しても、サンドボックスモードは続く
--
Vagrant.configure(2) do |config|
config.vm.box="centos/7"
config.vm.network :private_network, ip:"192.168.33.10"
config.vm.provision :shell, :inline => <<-EOT
yum update -y
service iptables stop
chkconfig iptables off
yum -y install httpd
chkconfig httpd on
service httpd start
EOT
config.vm.provider :virtualbox do |vb|
vb.gui = true
end
end
provisioning は、初回の vagrant up の後のみ
--
一つの Vagrantfile で複数の仮想マシンを立ち上げることができる
Vagrant.configure(2) do |config|
config.vm.box="centos/7"
config.vm.define :web do |web|
web.vm.hostname = "web"
web.vm.network :private_network, ip:"192.168.33.11"
end
config.vm.define :db do |web|
db.vm.hostname = "db"
db.vm.network :private_network, ip:"192.168.33.12"
end
end
--
box を作る
vagrant package
vagrant box add NAME URL URL には、HTTP の他、 vagrant package で作った box ファイルも指定可能
vagrant box list
vagrant box remove
vagrant box repackage $NAME $PRIVIDER vagrant が内部で管理している box をファイルに書きだす
vagrant plugin install $NAME
vagrant plugin license $NAME $LICENSE-FILE
vagrant plugin list
vagrant plugin uninstall $NAME
vagrant plugin update $NAME
uninsatll 出来なかったら ~/.vagrant.d/plugins.json をいじる
----
ログレベル
export VAGRANT_LOG=${debug|info|warn|error}