如何在 Vagrantfile 中呈现 YAML 列表

How to render a YAML list in a Vagrantfile

我有一个 Vagrantfile,它引用了一个 YAML 文件来简化多主机配置。

它大部分都在工作,但我使用的是 Ansible 供应器,我需要为 ansible.groups 项目引用 list/array。

YAML 如下所示:

hosts:
  - host:
      provision:
        ansible:
          enable: yes
          playbook_path: webserver.yml
          groups:
            - web
            - vagrant
            - live            

我正在尝试在 Vagrantfile 中引用它,使用:

if host['provision']['ansible']['enable'] == true
  vmhost.vm.provision "ansible" do |ansible|
    ansible.verbose = "vv"
    ansible.config_file = "./ansible.cfg"
    ansible.playbook = host['provision']['ansible']['playbook_path']
    ansible.tags = host['provision']['ansible']['tags']
    ansible.groups = host['provision']['ansible']['groups']
  end
end

但这在构建实际 VM 时给我这个错误:

undefined method `each_pair' for ["web", "vagrant", "dev"]:Array

虽然我在 Vagrantfile 中看到了各种读取 lists/arrays 的模式,但我进行了搜索但没有找到任何专门针对 ansible_groups 的内容。有什么想法吗?

Ansible 组不应该是一个数组,而是一个散列,将组映射到服务器名称。它应该看起来像:

hosts:
  - host:
      provision:
        ansible:
          enable: yes
          playbook_path: webserver.yml
          groups:
            web:
              - machine1
              - machine2
            vagrant:
              - machine3
              - machine4
            live:
              - machine5

有关如何使用它的更多详细信息,请参阅 the documentation