せっかくなのでAnsibleで作った環境をVagratでも使ってみる

January 12, 2015

昨日までDigitalOceanにAnsibleで環境を作っていたけれど、Vagrantでも同じことができると思うので、とりあえず慣れるためにやってみました。

必要なもの

  • Vagrant
  • Ansible
  • Ansibleで作ったplaybook

    • 今回は、前記事で作成したplaybookを利用しました。hostsの部分だけ書き換えています(後述)。

手順1 - ansible-playbookコマンドで環境構築

今回は、chefが公開しているchef/centos-7.0を利用することにしました。

command

$ cd /path/to/vagrant_ansible $ vagrant init $ vi Vagrantfile # *1 $ vagrant up $ vagrant ssh-config # *2 $ ansible-playbook -s -i hosts site.yml # *3

* 1 Vagrantfile

# -*- mode: ruby -*-

vi: set ft=ruby :

VAGRANTFILE_API_VERSION = “2”

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = “chef/centos-7.0” end

* 2 ssh-config

ssh-configをして下記の結果が得られました。ここから、HostNameとPortをhostsファイルに反映させました。

Host default HostName 127.0.0.1 User vagrant Port 2222 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile /Users/xxxxxx/.vagrant.d/insecure_private_key IdentitiesOnly yes LogLevel FATAL

* 3 hosts

-sはsudoで実行するための引数です。元のファイルがrootユーザ前提で書かれていたため、これが必要でした。

hostsをvagrant用に書きました。site.ymlも対象hostをvagrantに変更しています。

[vagrant] 127.0.0.1

[vagrant:vars] ansible_ssh_user=vagrant ansible_ssh_port=2222 ansible_ssh_private_key_file=~/.vagrant.d/insecure_private_key

手順2 - vagrantのprovisionで環境構築

現時点で、vagrantでCentOS 7.0を利用すると、private_networkの設定がうまくいかないようなので、fixのpluginを先にインストールしておきます(参考)。

vagrant plugin install vagrant-centos7_fix

vagrantのprovisionで設定してみました。まずVagrantfileを書き換えます。

Vagrantfile

# -*- mode: ruby -*-

vi: set ft=ruby :

VAGRANTFILE_API_VERSION = “2”

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = “chef/centos-7.0” config.vm.network “private_network”, ip: “192.168.33.20”

config.vm.provision “ansible” do |ansible| ansible.limit = ‘all’ ansible.inventory_path = “hosts” ansible.playbook = “site.yml” ansible.sudo = true end end

hostsも書き換えました。site.ymlも対象hostをvagrantに変更しています。

[vagrant] 192.168.33.20

[vagrant:vars] ansible_ssh_user=vagrant

$ vagrant up

これで完了です。

感想

時間がかかりすぎる…。やっぱりDigital Oceanでやったほうが良かったな。

元にしたplaybook

morizotter/ansible-trial

参考


Profile picture

Written by morizotter who lives and works in Tokyo building useful things. You should follow them on Twitter