LoginSignup
3
5

More than 5 years have passed since last update.

Windows + vagrant でansibleを簡単に使う

Last updated at Posted at 2018-02-04

結論

ちょっとずる(?)です。shellを使います。
「邪道だ!」と思うかもしれませんが一番簡単でした。

経緯

今まではvagrant + chef で環境構築していましたが、世が「ansible」「ansible」というので挑戦してみようと思った次第です。
yamlでの設定はchefより確かに簡単にかける気がします。また、数多くの方がさまざまなplaybookを公開してくださっているので、必要なモジュールをすぐに組み込めて楽です。(これはchefも一緒か)

で、実際に取り掛かったみたらWindows故のエラーになりましたので頭を切り替えて簡単に解決させようとした備忘録です。

作業内容

vagrant + ansible 環境の設定ファイル作成

基本的にこちらの「vagrant+ansibleで開発環境を構築する(OSインストール編)」を参考に準備しました。

この記事の「Ansibleの動作確認」まで進めたところで以下のエラーに見舞われました。

C:\Users\Project\vagrant>vagrant provision
==> default: Running provisioner: ansible...
Windows is not officially supported for the Ansible Control Machine.
Please check https://docs.ansible.com/intro_installation.html#control-machine-requirements
…略

読んで字のごとくWindowsではansibleがサポートされていない、とのこと。。
これだから開発PCでWindowsしか支給できない会社はくそ

そんなことを報告したら先輩から一言
「shellは使えるんだから、mount先のvmでansible実行すれば?」

Σ(゚д゚)

vmでansibleを実行するshellを実装

ansible.sh
#!/bin/bash

if ! [`which ansible`]; then
    yum install -y epel-release
    yum install -y ansible
fi

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

で、これをprovisionで実行するようVagrntfileを修正

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|

(中略)

  # Ansible provisioning
  config.vm.provision "shell", :path => "ansible.sh"

end

満を持してvagrnt upを実行すると、無事にansibleでの環境構築ができました!

shellのyum install ~~の部分はホストOSに応じて変更が必要なのが難点ですね。。

以上でWindows + vagrant でもansibleでの環境構築が可能となりました。

3
5
0

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
3
5