Vagrant:ansible provisioner 的多个剧本

Vagrant: multiple playbooks for ansible provisioner

以下形式的 vagrant ansible provisioner 是否可以/有效 运行 多个剧本:

 config.vm.define "repo", primary: true do |d|
    d.vm.hostname = "some.hostname"
    # Create a private network, which allows host-only access to the machine
    # using a specific IP.
    d.vm.network :private_network, ip: "10.10.2.90"
    d.vm.provision 'ansible' do |ansible|
      ansible.config_file = 'ansible/ansible.cfg'
      ansible.playbook = 'ansible/playbook1.yml'
      ansible.playbook = 'ansible/playbook2.yml'
      ansible.sudo = true
      ansible.inventory_path = 'ansible/inventory/site'
      ansible.host_key_checking = false
    end
  end

不,它将无效

如果你想 运行 2 playbook,你需要 运行 ansible provisioner 两次,这可以像

 config.vm.define "repo", primary: true do |d|
    d.vm.hostname = "some.hostname"
    # Create a private network, which allows host-only access to the machine
    # using a specific IP.
    d.vm.network :private_network, ip: "10.10.2.90"

    # First playbook
    d.vm.provision  "playbook1", type:'ansible' do |ansible|
      ansible.config_file = 'ansible/ansible.cfg'
      ansible.playbook = 'ansible/playbook1.yml'
      ansible.sudo = true
      ansible.inventory_path = 'ansible/inventory/site'
      ansible.host_key_checking = false
    end

    # Second playbook
    d.vm.provision  "playbook2", type:'ansible' do |ansible|
      ansible.config_file = 'ansible/ansible.cfg'
      ansible.playbook = 'ansible/playbook2.yml'
      ansible.sudo = true
      ansible.inventory_path = 'ansible/inventory/site'
      ansible.host_key_checking = false
    end
  end

您也可以使用角色代替剧本,角色包含指向角色子目录中定义的多个剧本的指针。例如 playbook.yml 包含

---
- name: BaseOS configuration
  hosts: all
  become: yes
  roles:
    - baseos
    - users

baseOS和users都在roles子目录下,调用playbook.yml时会依次执行。