Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

はじめに

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
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした