LoginSignup
24
25

More than 5 years have passed since last update.

vagrantのansibleプロビジョニングのサンプル

Last updated at Posted at 2013-08-15

Vagrant Documentation を参考にしつつ、Best PracticesDirectory Layoutに沿った形でサンプルを作ってみました。
サンプルコードは
https://github.com/hnakamur/vagrant-ansible-provisioning-example
にあります。

vagrantのboxは以下のpacker templateで作成します。CentOS 6.4とansibleをインストール済みのboxが作られます。
https://github.com/hnakamur/my-packer-template-files
(2013-08-17追記) guest側にはansibleのインストールは不要でした。ansibleはホスト側から実行されます。ホストからゲストにsshで接続できるようにVagrantfileとansible_hostsなどを設定しておく必要があります。

vagrant upの初回実行時は~/.ssh/known_hostsに登録されていないので、以下のように"Are you sure you want to continue connecting (yes/no)?"と途中で表示されるのでyesと入力して続行します。

GATHERING FACTS *************************************************************** 
The authenticity of host '192.168.33.13 (192.168.33.13)' can't be established.
RSA key fingerprint is 62:16:91:14:d2:67:f5:84:59:f5:f9:5e:53:67:8d:ae.
Are you sure you want to continue connecting (yes/no)? yes
ok: [192.168.33.13]

二回目以降のvagrant upでは以下のように一切入力なしで起動とプロビジョニングを実行できます。

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos6.4'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Fixed port collision for 22 => 2222. Now on port 2201.
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2201 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
[default] Running provisioner: ansible...

PLAY [vagrant] **************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [192.168.33.13]

TASK: [install ntp] *********************************************************** 
ok: [192.168.33.13]

TASK: [check ntpd service is stopped] ***************************************** 
changed: [192.168.33.13]

TASK: [ntpdate] *************************************************************** 
changed: [192.168.33.13]

TASK: [ntp config file] ******************************************************* 
changed: [192.168.33.13]

TASK: [start ntpd service] **************************************************** 
changed: [192.168.33.13]

NOTIFIED: [restart ntpd] ****************************************************** 
changed: [192.168.33.13]

PLAY RECAP ******************************************************************** 
192.168.33.13              : ok=7    changed=5    unreachable=0    failed=0   

Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.

ansibleの実行結果はfailed=0と失敗していないのに、vagrant側が"Ansible failed"と認識しているのがいまいちですが、とりあえずこれで使えてはいます。

24
25
2

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
24
25