LoginSignup
23
24

More than 5 years have passed since last update.

VagrantのプロビジョニングにAnsibleを利用する設定手順

Last updated at Posted at 2016-05-05

はじめに

Ansible実行をVagrantから行うための設定方法のメモです
参考: Vagrant公式ドキュメント

分けて起動する場合

  1. $ vagrant up
  2. $ ansible-playbook -i hosts site.yml --sudo -k

-i: Inventoryファイルを指定する
hosts: Inventoryファイル
site.yml: play-bookファイル
-kオプション: SSH のパスワードを尋ねる(プロンプトが出る)

Vagrantから直接プロビジョニングする場合

Ansible用プロビジョニング設定をVagrantfile


Vagrant.configure(2) do |config|

  config.vm.define "web" do |node|
    node.vm.box = "centos/7"
    node.vm.hostname = "web"
    node.vm.network :private_network, ip: "192.168.33.40"
  end
  config.vm.define "dbserver" do |node|
    node.vm.box = "centos/7"
    node.vm.hostname = "dbserver"
    node.vm.network :private_network, ip: "192.168.33.41"
  end

  #
  # Run Ansible from the Vagrant Host
  #
  config.vm.provision "ansible" do |ansible|
    ansible.ask_sudo_pass = true
    ansible.playbook = "site.yml"
    ansible.groups = {
      "group1" => ["web"]
      "group2" => ["dbserver"]
    }
  end

end
  • ansible.ask_sudo_pass = trueにより-kオプションと同等
  • ansible.playbook = "site.yml"によりplay-bookファイルを指定
    • currentディレクトリのからパスで指定する
  • ansible.groupによりInventoryファイルにおけるグループを作成
    • 上記の例では実行により.vagrant/provisioners/ansible/inventory/ディレクトリ上にvagrant_ansible_inventoryというInventoryファイルが以下のように作成される
# Generated by Vagrant

web ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222
dbserver ansible_ssh_host=127.0.0.1 ansible_ssh_port=2222

[group1]
web
[group2]
dbserver

Vagrantでのgroup名、host名をAnsibleでのコンポーネントのものと一致させる

実行

$ vagrant up/reload --provision
$ vagrant provision
23
24
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
23
24