Cristian Cristian - 21 days ago 8
Ruby Question

Transfer a file from one VM to another VM

The problem is at subconfig1.vm.provision ... , any advices on how should i proceed ? After initializing the VM's Vagrant tells me that SSH faild to do the job, because connection is refused. I can make this work, but only manually, inserting the password, my big question here is how do I overpass the password to make this workflow automatically.

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

config.vm.define "Machine2" do |subconfig2|

subconfig2.vm.box = "ubuntu/trusty64"
subconfig2.vm.hostname = "Machine2"
subconfig2.vm.network :private_network, ip: "172.16.10.101"
subconfig2.vm.network "forwarded_port", guest: 80, host: 4445, host_ip: "127.0.0.1"

end

#config.vm.provision "file", source: "~/.vagrant/machines/Machine2/virtualbox/private_key", destination: "/home/vagrant/ssh"


config.vm.define "Machine1" do |subconfig1|
subconfig1.vm.box = "ubuntu/trusty64"
subconfig1.vm.hostname = "Machine1"
subconfig1.vm.network :private_network, ip: "172.16.10.100"

# subconfig1.vm.provision "file", source: "~/.vagrant/machines/Machine2/virtualbox/private_key", destination: "/home/vagrant"

subconfig1.vm.provision "shell", privileged: false, inline: <<-SHELL
touch vagrantvm1-info
ifconfig | grep "HWaddr" | cut -b 32-55 >> vagrantvm1-info
# ssh -o StrictHostKeyChecking=no vagrant@127.0.0.1 uptime

cp /vagrant/.vagrant/machines/Machine2/virtualbox/private_key ~/.ssh
scp -P 2222 -i ~/.ssh/private_key vagrant@127.0.0.1:/home/vagrant/vagrantvm1-info /home/vagrant

# ssh vagrant@172.16.10.101
# scp vagrant@172.16.10.101:/home/vagrant/vagrantvm1-info /home/vagrant

SHELL
end




config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
vb.gui = true
# Customize the amount of memory on the VM:
vb.memory = "1024"
end

config.vm.provision "shell", privileged: false, inline: <<-SHELL
apt-get install -y avahi-daemon libnss-mdns
apt-get update
apt-get install -y apache2
SHELL

end

Answer Source

You need to pass the private key so you can successfully login.

  1. retrieve which private key is used

Generally the file is under .vagrant/machines/<machine_name>/provider/private_key

You can also run vagrant ssh-config to find out which private key is being used for all machines:

fhenri@machine:/Volumes/VM/vagrant/golang (master)$ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Volumes/VM/vagrant/golang/.vagrant/machines/default/virtualbox/private_key
  IdentitiesOnly yes
  LogLevel FATAL

In this case the private key is /Volumes/VM/vagrant/golang/.vagrant/machines/default/virtualbox/private_key

  1. copy the key in the VM.

Since you want to run the copy VM to VM, the origin VM needs to know the private key so it will connect to the destination VM.

Use a file provisioner or a copy file from a shell script to copy the private key used to login to destination VM into the origin VM.

  1. scp using the private key - see scp man page

    scp -i ~/.ssh/private_key \
    /home/vagrant/vagrantvm1-info \
    vagrant@172.16.10.101:/home/vagrant
    

The updated Vagrantfile I used to test this is the following

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|

  config.vm.define "Machine2" do |subconfig2|

    subconfig2.vm.box = "ubuntu/trusty64"
    subconfig2.vm.hostname = "Machine2"
    subconfig2.vm.network :private_network, ip: "172.16.10.101"
    subconfig2.vm.network "forwarded_port", guest: 80, host: 4445, host_ip: "127.0.0.1"

  end

   #config.vm.provision "file", source: "~/.vagrant/machines/Machine2/virtualbox/private_key", destination: "/home/vagrant/ssh"


  config.vm.define "Machine1" do |subconfig1|
    subconfig1.vm.box = "ubuntu/trusty64"
    subconfig1.vm.hostname = "Machine1"
    subconfig1.vm.network :private_network, ip: "172.16.10.100"

   # subconfig1.vm.provision "file", source: "~/.vagrant/machines/Machine2/virtualbox/private_key", destination: "/home/vagrant"

    subconfig1.vm.provision "shell", privileged: false, inline: <<-SHELL
      touch vagrantvm1-info
      ifconfig | grep "HWaddr" | cut -b 32-55 >> vagrantvm1-info
    #  ssh -o StrictHostKeyChecking=no vagrant@127.0.0.1 uptime

      cp /vagrant/.vagrant/machines/Machine2/virtualbox/private_key ~/.ssh && chmod 0644 ~/.ss/private_key
      # make sure to accept the server keys
      ssh-keyscan 172.16.10.101 >> ~/.ssh/known_hosts
      scp -i ~/.ssh/private_key /home/vagrant/vagrantvm1-info vagrant@172.16.10.101:/home/vagrant

    #  ssh vagrant@172.16.10.101
    #  scp vagrant@172.16.10.101:/home/vagrant/vagrantvm1-info /home/vagrant

   SHELL
  end




  config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
   #vb.gui = true
# Customize the amount of memory on the VM:
   vb.memory = "1024"
  end

  config.vm.provision "shell", inline: <<-SHELL
    apt-get install -y avahi-daemon libnss-mdns
    apt-get update
    apt-get install -y apache2
  SHELL

end