読者です 読者をやめる 読者になる 読者になる

はやさがたりない。

へっぽこぷろぐらまのメモログ

vagrantの基本操作

Vagrantお勉強めも

きっほーん

  • VagrantにCentOS6.5のBoxを追加
$ vagrant box add centos6.5 https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box 

==> box: Adding box 'centos6.5' (v0) for provider: 
    box: Downloading: https://github.com/2creatives/vagrant-centos/releases/download/v6.5.3/centos65-x86_64-20140116.box
==> box: Successfully added box 'centos6.5' (v0) for 'virtualbox'!
  • 初期化
$ vagrant init centos6.5
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: Importing base box 'centos6.5'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: ansible_default_1402122327618_53164
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (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: Warning: Remote connection disconnect. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => /Users/akira/vagrant/ansible
  • つなげてみる
$ vagrant ssh
[vagrant@vagrant-centos65 ~]$ hostname
vagrant-centos65.vagrantup.com
[vagrant@vagrant-centos65 ~]$ whoami
vagrant
[vagrant@vagrant-centos65 ~]$ pwd  
/home/vagrant
[vagrant@vagrant-centos65 ~]$ ls -l 
total 0
[vagrant@vagrant-centos65 ~]$ exit
logout
Connection to 127.0.0.1 closed.
  • 破棄してみる
$ 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...
  • 共有フォルダ デフォルトは/vagrantらしい ここのフォルダはvagrant destroy`しても消えないとのこと?
 vagrant ssh
[vagrant@vagrant-centos65 ~]$ ls -l /vagrant/
total 8
-rw-r--r-- 1 vagrant vagrant 4816 Jun  7 06:24 Vagrantfile

設定ファイルではここになる。共有されるか試してみる。

config.vm.synced_folder "../data", "/vagrant_data"
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
There are errors in the configuration of this machine. Please fix
the following errors and try again:

vm:
* The host path of the shared folder is missing: ../data

$ mkdir ../data
$ vagrant up
$ vagrant ssh
Last login: Sat Jun  7 06:49:57 2014 from 10.0.2.2
[vagrant@vagrant-centos65 ~]$ ls -l /vagrant_data/
total 0
[vagrant@vagrant-centos65 ~]$ touch /vagrant_data/hoge
[vagrant@vagrant-centos65 ~]$ ls -l /vagrant_data/     
total 0
-rw-r--r-- 1 vagrant vagrant 0 Jun  7 07:15 hoge
[vagrant@vagrant-centos65 ~]$ exit
logout
Connection to 127.0.0.1 closed.
$ ls -l ../data/
total 0
-rw-r--r--  1 akira  staff  0  6  7 16:15 hoge
  • forward設定 ゲストOSの8080をホストの80にふぉわーど。
  config.vm.network "forwarded_port", guest: 80, host: 8080

起動してlocalhost:8080にアクセス

$ vagrant ssh
Last login: Sat Jun  7 07:14:44 2014 from 10.0.2.2
[vagrant@vagrant-centos65 ~]$ cd /vagrant
[vagrant@vagrant-centos65 vagrant]$ sudo python -m SimpleHTTPServer 80
Serving HTTP on 0.0.0.0 port 80 ...
10.0.2.2 - - [07/Jun/2014 07:27:57] "GET / HTTP/1.1" 200 -
  • 停止
$ vagrant halt
$ vagrant suspend
  • プロビジョニング
    プロビジョニング自体は初回のvagrant upのときしか動かないとのこと
    なので必要であれば明示的にやってあげる必要がある
$ vagrant provision
$ vagrant up --provision
$ vagrant up --no-provision
$ vagrant up --provision-with=chef