Vagrant boxes gives SSH error when trying to connect

655 Views Asked by At

I'm trying to create 3 ubuntu vagrant boxes: machine1,machine2,machine3. machine3 should run an ansible playbook which installs something on machine1 and machine2. However, keep getting an error when trying to ssh (ssh [email protected] -p 2221)to the machines:

ssh: connect to host 192.168.77.21 port 2221: Connection refused 

This is the Vagrantfile:

Vagrant.configure("2") do |config|
  $script = <<-SCRIPT
  sudo apt update
  sudo apt install software-properties-common
  sudo add-apt-repository --yes --update ppa:ansible/ansible
  sudo apt -y install ansible
  ansible --version
  SCRIPT

  N = 3
  VAGRANT_VM_PROVIDER = "virtualbox"

  (1..N).each do |machine_id|
    config.vm.define "machine#{machine_id}" do |machine|
      machine.vm.box="ubuntu/trusty64"
      machine.vm.hostname = "machine#{machine_id}"
      machine.vm.network "private_network", ip: "192.168.77.#{20+machine_id}"
      machine.vm.network "forwarded_port", id: "ssh", guest: 22, host: 2220+machine_id
      machine.vm.synced_folder ".", "/home/vagrant/data"
      # Only execute once on the Ansible provisioner,
      # when all the machines are up and ready.
      if machine_id == N
        machine.vm.provision "shell", inline: $script
        machine.vm.provision "shell",
          inline: "sudo cp /home/vagrant/data/hosts /etc/ansible/hosts"
        machine.vm.provision "shell",
          inline: "ansible-playbook /home/vagrant/data/playbook.yml -vvv"
      end
    end
  end
end

How to solve this issue?

0

There are 0 best solutions below