125
130

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AnsibleをゲストOSに押し込んでVagrant環境構築する

Last updated at Posted at 2014-02-02

Windows上でVagrantをAnsibleでprovisionしようとすると、Windows上のAnsible環境で頭を悩ませることになります。(調べた限り無理らしい)

そこで、Ansibleは大人しくゲストマシンに押し込んでしまいましょう。

リポジトリはこちら
progre/template-vagrant

ディレクトリ構成はこんな感じ

ansible/
  hosts
  playbook.yml
provision.sh
Vagrantfile

Playbookを好きなように書きます。

ansible/playbook.yml
---
- hosts: 127.0.0.1
  connection: local
  sudo: yes
  tasks:
    - name: install ruby
      apt: pkg=ruby update_cache=yes
    - name: install rubygems
      apt: pkg=rubygems update_cache=yes
    - name: install bundler
      command: gem install bundler

hostsも用意しておきます

ansible/hosts
127.0.0.1 ansible_connection=local

ゲストOSでAnsibleを実行するスクリプトを書きます。

provision.sh
#!/usr/bin/env bash

if ! [ `which ansible` ]; then
	apt-get update -y
	apt-get install -y ansible
fi

ansible-playbook -i /vagrant/ansible/hosts /vagrant/ansible/playbook.yml

あとはVagrantfileですね。

Vagrantfile
Vagrant.configure("2") do |config|
  config.vm.box = "precise64"
  config.vm.box_url = "http://files.vagrantup.com/precise64.box"
  config.vm.synced_folder ".", "/vagrant", :mount_options => ['dmode=775', 'fmode=664']
  config.vm.provision "shell", :path => "provision.sh"
end

(補足)
共有フォルダはデフォルトだと実行可能属性になり、それだとhostsファイルが読めなくなります。
synced_folderの:mount_optionsで共有フォルダの属性を指定できます(garbagetownさんpullリクありがとうございます)

これでWindowsでもド安定でAnsibleで環境が作れます。

PS E:\Developments\vagrant\rubyist> vagrant provision
[default] Running provisioner: shell...
DL is deprecated, please use Fiddle
[default] Running: C:/Users/progre/AppData/Local/Temp/vagrant-shell20140202-12404-w7zith
stdin: is not a tty

PLAY [127.0.0.1] **************************************************************

GATHERING FACTS ***************************************************************
ok: [127.0.0.1]

TASK: [install ruby] **********************************************************
ok: [127.0.0.1]

TASK: [install rubygems] ******************************************************
ok: [127.0.0.1]

TASK: [install bundler] *******************************************************
changed: [127.0.0.1]

PLAY RECAP ********************************************************************
127.0.0.1                  : ok=4    changed=1    unreachable=0    failed=0

PS E:\Developments\vagrant\rubyist>

125
130
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
125
130

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?