【问题标题】:Vagrant multi-machine provisioningVagrant 多机配置
【发布时间】:2015-12-24 19:52:44
【问题描述】:

我正在尝试使用 Ansible 作为配置器在 Vagrant 中创建多机环境。

我的 Vagrantfile 如下所示:

   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" 只创建了第一台机器,provisioner 就会运行,因为其他 2 台没有创建而失败。 是否可以在创建所有机器后运行配置程序?或者我的方法不正确,我应该以其他方式执行此操作? 感谢您的宝贵时间。

【问题讨论】:

  • 首先,很明显,您的本地主机上是否安装了ansible?其次,您可以推迟配置,但 vagrant up --no-provision 和可选的 --no-destroy-on-error。最后,您的 main.yml 需要 --- 是有效的 yaml(尽管它可能不会导致它失败)。
  • Ansible 安装在 localhost 上并作为配置器运行,但它仅针对主机 192.168.77.10 运行 - 其他无法访问。似乎,vagrant 在创建每台机器后运行配置程序,而不是在创建 Vagrantfile 的所有机器之后

标签: vagrant ansible


【解决方案1】:

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

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

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

最后,每台机器都有一个不同的密钥,你必须通过它。因此,按照 Vagrantdocumented approach 使用 Ansible 进行多机并行处理并在 this work-around 的帮助下,我建议您的 Vagrantfile 如下所示:

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

【讨论】:

  • 你是对的 - 我已经创建了你在 main.yml 文件中看到的所有角色。目前他们什么都不做,只是“从某个角色回显'Hello'”,因为我刚刚开始学习 Vagrant 和 Ansible。我在您写的 Vagrant docs 解决方法中看到了。因此,我们有两个可能的选项来运行配置器 after all 不是每台机器创建的机器 - 使用某种循环(通过机器列表或示例中的数字范围) 或者使用 --no-provision 运行 vagrant up 然后分别运行 vagrant provision,对吗?
  • 实际上有很多方法可以解决这个问题。我不建议您在两步模式中使用 --no-provision。就个人而言,我喜欢有一个 hosts 文件来指定我的组,因为这是我使用生产 ansible-playbook 使用的方式。但是,如果您不需要或不想要静态清单文件,则可以使用 vagrant 对主机组的内置支持。请参阅How to generate Inventory Groups,我可以提供一个示例。
  • 我已经用上面帖子中的 hosts 文件尝试了你的 Vagrantfile,但遇到了新问题:现在 Ansible 成功连接到最后一台机器(192.168.77.12)但其他 2(192.168.77.10-11)没有可达:fatal: [192.168.77.11] =&gt; SSH Error: while connecting to 192.168.77.11:22 It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue. 看起来需要一些额外的 ssh 选项。你能告诉我在哪里看吗?
  • @PavloI。那是因为解决方法不起作用。您是否有机会将您的代码较少的专有数据和角色放入我可以查看的存储库中?我很乐意提供帮助。
  • 目前没有任何专有的东西。非常感谢您的宝贵时间!这是回购:GitHub
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多