Provision Vagrant Linux VM with another Vagrant Li

2019-05-13 17:47发布

问题:

I know Ansible has issues running on windows. Which is why, I want to avoid using it for my host. I want to provision a local linux vm running in VirtualBox.

I was wondering if anyone can tell me if it is possible, to use vagrant to bring up two independent VMs on the same box. Then install Ansible on one of those VMs, then using SSH log into that VM. From there, use the Linux VM with Ansible as the host, to provision another Linux VM, that was created via the windows host machine. So, this is not a VM inside a VM. It is just two VMs running on windows using vagrant, then SSH to one of those VMs to use Ansible to provision the other VM.

Steps:

  1. Vagrant VM 1 and install Ansible
  2. Vangrant VM 2
  3. SSH to VM 1
  4. Use Ansible to provision VM 2 using VM 1.

Can that be done? Sorry if that sounded confusing.

回答1:

There is now a new Ansible local provisioner in Vagrant 1.8.0, which you can use in your scenario.

Especially, look at "Tips and Tricks" section of the documentation, there is an exact solution (which worked for me).

Below is my Vagrantfile for this scenario (slightly different from the one in the documentation), which also solves potential problems with the ssh permissions and "executable" inventory file (if you're using Cygwin):

Vagrant.configure(2) do |config|
  config.vm.synced_folder "./", "/vagrant", 
     owner: "vagrant",
     mount_options: ["dmode=775,fmode=600"]

  config.vm.define "vm2" do |machine|
    machine.vm.box = "box-cutter/ubuntu1404-desktop"
    machine.vm.network "private_network", ip: "172.17.177.21"
  end

  config.vm.define 'vm1' do |machine|
    machine.vm.box = "ubuntu/trusty64"
    machine.vm.network "private_network", ip: "172.17.177.11"    

    machine.vm.provision :ansible_local do |ansible|
      ansible.provisioning_path = "/vagrant"
      ansible.playbook = "provisioning/playbook.yml"      
      ansible.limit = "vm2"
      ansible.inventory_path = "inventory"
      ansible.verbose = "vvv"
      ansible.install = true    
    end
  end
end

and inventory file:

vm1 ansible_connection=local
vm2 ansible_ssh_host=172.17.177.21 ansible_ssh_private_key_file=/vagrant/.vagrant/machines/vm2/virtualbox/private_key


回答2:

In order to provision a box you don't necessary need to do it using another box, in this windows scenario you could simply write your playbooks, share it to your guest and hit it with ansible-playbook using shell provisioning.

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

$script = <<SCRIPT
sudo apt-get install -y software-properties-common
sudo apt-add-repository -y ppa:ansible/ansible
sudo apt-get update
sudo apt-get install -y ansible
ansible-playbook /home/vagrant/provisioning/playbook.yml
SCRIPT

config.vm.synced_folder "./provisioning", "/home/vagrant/provisioning"

config.vm.provision "shell", inline: $script

end

The first lines will get ansible on your box then it will target the playbook that you have shared to your box and run the playbooks.

This is an example, I once used this approach to provision my working vagrant box, hope this idea can help you.