Vagrant 多机配置 [英] Vagrant multi-machine provisioning

查看:25
本文介绍了Vagrant 多机配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Ansible 作为供应商在 Vagrant 中创建多机环境.

I'm trying to create multi-machine environment in Vagrant using Ansible as provisioner.

我的 Vagrantfile 如下所示:

My Vagrantfile looks like next:

   Vagrant.configure("2") do |config|

    config.vm.provision "ansible" do |ansible|
       ansible.limit = "all"
       ansible.playbook = "main.yml"
       ansible.inventory_path = "staging"
       ansible.verbose = "-vvvv"
     end

    config.vm.define "machine1" do |machine1| 
        machine1.vm.box = "ubuntu/trusty64"
        machine1.vm.network "private_network", ip:"192.168.77.10"
        machine1.vm.hostname = "machine1"
        machine1.vm.provider :virtualbox do |vb|
           vb.name = "machine1"
        end
    end    

    config.vm.define "machine2" do |machine2| 
        machine2.vm.box = "ubuntu/trusty64"
        machine2.vm.network "private_network", ip:"192.168.77.11"
        machine2.vm.hostname = "machine2"
        machine2.vm.provider :virtualbox do |vb|
            vb.name = "machine2"
        end
    end    

    config.vm.define "machine3" do |machine3| 
        machine3.vm.box = "ubuntu/trusty64"
        machine3.vm.network "private_network", ip:"192.168.77.12"
        machine3.vm.hostname = "machine3"
        machine3.vm.provider :virtualbox do |vb|
           vb.name = "machine3"
        end
    end      
end

库存:

[AppServers]
192.168.77.10
192.168.77.11
192.168.77.12

[WebServers]
192.168.77.11
192.168.77.12

[WebLoadBalancers]
192.168.77.10

[SlaveDbServers]
192.168.77.10
192.168.77.12

[MasterDbServers]
192.168.77.11

[DbLoadBalancers]
192.168.77.11

main.yml:

- hosts: all
  roles:
  - Common
  - ConsulServer
  - ConsulAgent  

- hosts: WebServers
  roles:
  - WebServer

- hosts: WebLoadBalancers
  roles:
  - LoadBalancer

- hosts: MasterDbServers
  roles:
  - DbServer

我想要 3 台机器.它们都必须包含通用软件(Consul 服务器和代理、vim 等).还有一些额外的 - 每台机器都拥有.但是一旦我运行 "vagrant up" 只创建第一台机器,配置程序运行,失败,因为其他 2 没有创建.是否可以在创建所有机器后运行配置程序?还是我的方法不正确,我应该以其他方式执行此操作?感谢您抽出宝贵时间.

I want to get 3 machines. All of them have to contain common soft(Consul servers and agents, vim etc). And some additional - own for each machine. But once i'm running "vagrant up" only first machine created, provisioner runs, fails because other 2 not created. Is it possible to run provisioner after all machines created? Or my approach is incorrect and i should perform this in other way? Thank you for your time.

推荐答案

我遇到的第一个问题是ERROR: cannot find role in....我假设您拥有这些角色并为简洁起见将它们排除在外.我的建议是在测试时准备一个简单的 Ansible 手册:

The first problem I had was ERROR: cannot find role in.... I'm assuming you have these roles and excluded them for brevity. My advice here is to have a simple Ansible playbook when you are testing this:

---
- hosts: all
  gather_facts: false
  tasks:
  - command: hostname -f

其次,手头的问题围绕着静态清单文件的使用和其中的注意事项.您看到一个错误,因为 Ansible 配置程序 在第一台机器启动后运行时未能找到所有主机,但其他机器没有.

Secondly, the problem at hand surround the use of static inventory file and caveats therein. You are seeing an error because the Ansible provisioner is failing to find all hosts when it runs after the first machine is up but the others are not.

最后,每台机器都有不同的密钥,您必须通过.因此,遵循 Vagrant文档化方法- 在 此解决方法的帮助下,使用 Ansible 实现机器并行化,这里是我推荐你的 Vagrantfile 的样子:

Lastly, each machine will have a different key, which you must pass. So, following Vagrant's documented approach for multi-machine parallelism with Ansible and with help from this work-around, here is what I recommend your Vagrantfile look like:

Vagrant.configure("2") do |config|
  N = 3

  VAGRANT_VM_PROVIDER = "virtualbox"
  ANSIBLE_RAW_SSH_ARGS = []

  (1..N-1).each do |machine_id|
    ANSIBLE_RAW_SSH_ARGS << "-o IdentityFile=.vagrant/machines/machine#{machine_id}/#{VAGRANT_VM_PROVIDER}/private_key"
  end

  (1..N).each do |machine_id|
    config.vm.define "machine#{machine_id}" do |machine|
      machine.vm.box = "ubuntu/trusty64"
      machine.vm.hostname = "machine#{machine_id}"
      machine.vm.network "private_network", ip: "192.168.77.#{10+machine_id-1}"

      # Only execute once the Ansible provisioner,
      # when all the machines are up and ready.
      if machine_id == N
        machine.vm.provision :ansible do |ansible|
          # Disable default limit to connect to all the machines
          ansible.limit = "all"
          ansible.playbook = "main.yml"
          ansible.inventory_path = "staging"
          ansible.verbose = "-v"
          ansible.raw_ssh_args = ANSIBLE_RAW_SSH_ARGS
        end
      end
    end
  end
end

这篇关于Vagrant 多机配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆