在该机器在Vagrantfile中的定义结束后,是否可以在特定的Vagrant机器上执行配置器?

时间:2017-05-18 18:45:52

标签: vagrant ansible virtualbox

我希望能够立即为项目启动所有Vagrant虚拟机,然后在最后,在其中一个虚拟机上执行Ansible(使用ansible_local)。是否可以在您之前已定义的计算机上运行ansible_local等配置程序?

我想做的例子:

    # VM definitions
    config.vm.define "control" do |control|
        control.vm.hostname = "control.somedomain.local"
        control.vm.network "private_network", ip: "172.28.128.3"
        control.vm.provider "virtualbox" do |control_vbox|
            control_vbox.name = "Ansible Controller"
            control_vbox.cpus = 2
            control_vbox.memory = 1024
        end
    end
    config.vm.define "web01" do |web|
        control.vm.hostname = "web01.somedomain.local"
        control.vm.network "private_network", ip: "172.28.128.4"
        control.vm.provider "virtualbox" do |web_vbox|
            web_vbox.name = "Web Server 1"
            web_vbox.cpus = 2
            web_vbox.memory = 1024
        end
    end

    # run ansible_local provisioner from the control VM
    config.vm.provision "ansible_local" do |ansible|
        ansible.run_on_vm = "control"
        ansible.playbook = "some_playbook.yml"
    end

1 个答案:

答案 0 :(得分:0)

您可以在Vagrantfile中进行以下更改

# VM definitions
config.vm.define "control" do |control|
    control.vm.hostname = "control.somedomain.local"
    control.vm.network "private_network", ip: "172.28.128.3"
    control.vm.provider "virtualbox" do |control_vbox|
        control_vbox.name = "Ansible Controller"
        control_vbox.cpus = 2
        control_vbox.memory = 1024
    end

    # run ansible_local provisioner from the control VM
    control.vm.provision "ansible_local" do |ansible|
        ansible.run_on_vm = "control"
        ansible.playbook = "some_playbook.yml"
    end
end
config.vm.define "web01" do |web|
    control.vm.hostname = "web01.somedomain.local"
    control.vm.network "private_network", ip: "172.28.128.4"
    control.vm.provider "virtualbox" do |web_vbox|
        web_vbox.name = "Web Server 1"
        web_vbox.cpus = 2
        web_vbox.memory = 1024
    end
end

这样ansible供应商只能在 control VM上运行

其他可能性是

  • 按以下顺序运行vagrant命令

    $ vagrant up --no-provision // will spin up all the vms but will not provisiong
    $ vagrant provision control // will provision the control vm
    
  • 检查可用于运行特定命令的vagrant trigger plugin,我还没有使用多机环境进行测试