Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
130
Help us understand the problem. What is going on with this article?
@progre

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

More than 5 years have passed since last update.

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>

130
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
progre

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
130
Help us understand the problem. What is going on with this article?