LoginSignup
30
28

More than 5 years have passed since last update.

vagrant 1.2を使ってみる

Last updated at Posted at 2013-05-31

vagrantでEC2をあげたりさげたりしたかったので、1.0.7から1.2.2にバージョンをあげてみた。
ちなみに利用している環境は32bitマシンで動いているUbuntu 12.04.2 LTS。
もともと入っていたvagrant 1.0.7はgem uninstallした。

まずはvagrant1.2.2のインストール。どうやら1.1以降からパッケージインストールになった模様。
公式サイトよりパッケージをダウンロードしてインストールする。

$ wget http://files.vagrantup.com/packages/7e400d00a3c5a0fdf2809c8b5001a035415a607b/vagrant_1.2.2_i686.deb
$ sudo dpkg -i vagrant_1.2.2_i686.deb
$ which vagrant
/usr/bin/vagrant

VirtualBoxでCentOS環境を起動する

vagrantで仮想環境の素になるものをboxという。boxは以下で公開されている。自分で作る事も出来る。
http://www.vagrantbox.es

boxの追加にはvagrant box addを使用する。
$ vagrant box add {title} {url}

$ sudo vagrant box add cent64_minimal_i386 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-i386-v20130427.box
$ vagrant box list
cent64_minimal_i386  (virtualbox)

vagrantの設定ファイルであるVagrantfileを生成する。vagrant initで生成できる。
$ vagrant init {boxtitle}

$ mkdir centos64
$ cd centos64
$ vagrant init cent64_minimal_i386
$ ls
Vagrantfile

Vagrantfileは以下のような感じに設定した。
1.0.7と結構異なる点があって、手こずった。。。

Vagrantfile
Vagrant.configure("2") do |config|
  #box名
  config.vm.box = "cent64_minimal_i386"
  #VMのhostname
  config.vm.hostname = "vmcent1"
  #HostOnlyAdapterを追加
  config.vm.network :private_network, ip: "192.168.56.100"
  config.vm.provider :virtualbox do |vb|
    #GUI上の名前
    vb.name = "vmcent1"
    #GUIを有効にする
    vb.gui = true
    #Memoryを512Mに設定
    vb.customize ["modifyvm", :id, "--memory", "512"]
  end
end

vagrant upで起動してvagrant sshでssh接続する。

$ sudo vagrant up
$ sudo vagrant ssh

OSをshutdownする時はhalt、廃棄する時はdestroyを指定し実行する。

$ sudo vagrant halt
$ sudo vagrant destroy

VirtualBoxでEC2を起動する

ここから本題。

AWSを扱うためのPluginをインストールする。

$ sudo vagrant plugin install vagrant-aws

EC2の場合ダミーのboxを用意する必要がある。

$ sudo vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
$ vagrant box list
cent64_minimal_i386  (virtualbox)
dummy                (aws)

Vagrantfileを作成する。

$ mkdir aws
$ cd aws
$ vagrant init

Vagrantfileを編集する。設定項目は割と分かりやすい感じ。

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "dummy"

  config.vm.provider :aws do |aws, override|
    aws.access_key_id     = ENV['AWS_ACCESS_KEY_ID']
    aws.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']
    aws.keypair_name = "uchi"
    aws.instance_type = "t1.micro"
    aws.region = "ap-northeast-1"
    aws.ami = "ami-173fbf16"
    aws.security_groups = [ 'uchi-secritygroup' ]
    aws.tags = { 
      'Name' => 'uchi-test',
      'Description' => 'vagrant test'
    }   
    #sshに使うキーとユーザー名をvagrantのものからAWSのものへオーバーライドする
    override.ssh.private_key_path = "~/.ssh/uchi.pem"
    override.ssh.username = "ec2-user"
  end 
end

--provider=aws で Provider を指定してvagrant upする。

$ vagrant up --provider=aws     
Bringing machine 'default' up with 'aws' provider...
[default] Warning! The AWS provider doesn't support any of the Vagrant
high-level network configurations (`config.vm.network`). They
will be silently ignored.
[default] Launching an instance with the following settings...
[default]  -- Type: t1.micro
[default]  -- AMI: ami-218e0620
[default]  -- Region: ap-northeast-1
[default]  -- Keypair: uchi
[default]  -- Security Groups: ["uchi-secritygroup"]
[default] Waiting for instance to become "ready"...
[default] Waiting for SSH to become available...
[default] Machine is booted and ready for use!
[default] Rsyncing folder: /home/uchi/vagrant/aws/ => /vagrant

ちゃんとvagrant sshでssh出来る。destroyはうまく行ったがhaltが失敗した。

vagrant ssh
Last login: Fri May 31 13:43:52 2013 from ...

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2013.03-release-notes/
There are 2 total update(s) available
Run "sudo yum update" to apply all updates.
$ exit
$ vagran halt
 Vagrant attempted to call the action 'halt' on the provider
'AWS (i-129f1610)', but this provider doesn't support this action. This
is probably a bug in either the provider or the plugin calling this
action, and should be reported.
$ vagran destroy
[default] Terminating the instance...

ハマったところ

EC2用のVagrantfileを書くにあたり、鍵の場所とユーザー名を以下のように指定していたが、
vagrant upした際に「Waiting for SSH to become available..」で止まってしまった。

aws.ssh.private_key_path = "~/.ssh/uchi.pem"
aws.ssh.username = "ec2-user"

なぜかVagrantユーザーとキーを使おうとしていたので、以下のように指定して解消した。

override.ssh.private_key_path = "~/.ssh/uchi.pem"
override.ssh.username = "ec2-user"

sshは成功しても、その後のコマンドがfailした。

[default] Rsyncing folder: /home/uchi/vagrant/aws/ => /vagrant
The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

mkdir -p '/vagrant'

デバッグログを出してみたら、sudoでttyがないよと怒られていた。
/etc/sudoersの「Defaults requiretty」行をコメントアウトしたAMIから起動して解消。

$ VAGRANT_LOG=debug vagrant up --provider=aws
DEBUG ssh: Exit status: 1
DEBUG ssh: stderr: sudo: sorry, you must have a tty to run sudo

詰まった時はデバッグログ出しながら調べるととてもいい感じ。

30
28
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
28