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
You need to pass the private key so you can successfully login.
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
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.
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
Related
Whenever I run Vagrant up, I am getting the following error.
Following is my vagrant file
default_box = "generic/opensuse42"
Vagrant.configure("2") do |config|
config.vm.define "master" do |master|
master.vm.box = default_box
master.vm.box_version = "v3.4.4"
master.vm.hostname = "master"
master.vm.network 'private_network', ip: "192.168.0.200", virtualbox__intnet: true
master.vm.network "forwarded_port", guest: 22, host: 2222, id: "ssh", disabled: true
master.vm.network "forwarded_port", guest: 22, host: 2000 # Master Node SSH
master.vm.network "forwarded_port", guest: 6443, host: 6443 # API Access
for p in 30000..30100 # expose NodePort IP's
master.vm.network "forwarded_port", guest: p, host: p, protocol: "tcp"
end
master.vm.provider "virtualbox" do |v|
v.memory = "3072"
v.name = "master"
end
master.vm.provision "shell", inline: <<-SHELL
sudo zypper refresh
sudo zypper --non-interactive install bzip2
sudo zypper --non-interactive install etcd
sudo zypper --non-interactive install apparmor-parser
curl -sfL https://get.k3s.io | sh -
SHELL
end
Any hints will be much appreciated.
The error message provides the info you require:
in 'parse': Illformed requirement ["v3.4.4"] (Gem::Requirement::BadRequirementError)
It basically advises that there is a problem with your version of vm requested from your Vagrantfile:
default_box = "generic/opensuse42"
Vagrant.configure("2") do |config|
config.vm.define "master" do |master|
master.vm.box = default_box
master.vm.box_version = "v3.4.4"
master.vm.hostname = "master"
The problem is that you have used 'v' to define the version, which cannot be parsed for the value attained for the 'config.vm.box_version' attribute variable - it should be :
master.vm.box_version = "3.4.4"
You can always double check your config for a vm from the Vagrant website, in your case generic/opensuse42 - v3.4.4, it shows that it should be:
Vagrant.configure("2") do |config|
config.vm.box = "generic/opensuse42"
config.vm.box_version = "3.4.4"
end
I found that generic/opensuse42 has been expired so changing the box to opensuse/Leap-15.2.x86_64, solved my issue
I have the following vagrant configuration:
Vagrant.configure("2") do |config|
config.vagrant.plugins = "vagrant-docker-compose"
config.vm.provision :docker
config.vm.provision :docker_compose
config.vm.box = "ubuntu/bionic64"
config.vm.provider "virtualbox" do |v|
v.memory = 10240
v.cpus = 4
end
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y make
SHELL
end
vagrant: 2.2.18
here is the output of vagrant up --debug: https://pastebin.com/SXZaS9Yp
The memory and the cpus parts are ignored.
Why?
Thanks
I have a question related to Hashicorp Vagrant. I would like to create a multi environment machine. I was wondering if we could share common configuration for multiple machines then override each with the specific requirements.
Imagine I have the following vagrant file.
Vagrant.configure("2") do |config|
#
config.vm.define "webserver" do |webserver|
webserver.vm.box = "ubuntu/focal64"
webserver.vm.network :private_network, ip: "10.0.0.10"
webserver.vm.provision "shell", inline: "apt-get update"
webserver.vm.provision "shell", inline: "script specific to web server "
end
#
config.vm.define "db" do |db|
db.vm.box = "ubuntu/focal64"
db.vm.network :private_network, ip: "10.0.0.11"
db.vm.provision "shell", inline: "apt-get update"
db.vm.provision "shell", inline: "script specific to db"
end
end
As we could see, we have some commons. And I would like to know if it was possible to make it a little bit shoter and more flexible. Maybe there is a way to override?
Also, what if I have a third machine that is not ubuntu but red hat instead? Would that be possible?
You can solve this by create a list of vm's you want to create. This is a very nice way to configure multiple machines in an easy to read way.
# List of machine specific details
nodes = {
"webserver" => ["10.0.0.10", "script specific to web server"],
"db" => ["10.0.0.11", "script specific to db"],
Vagrant.configure("2") do |config|
# Iterate over the machines
nodes.each do | (name, cfg) |
ip_address, script_name
config.vm.define name do |server|
server.vm.box = "ubuntu/focal64"
server.vm.network :private_network, ip: ip_address
server.vm.provision "shell", inline: "apt-get update"
server.vm.provision "shell", inline: script_name
end
end
end
I am trying to export a machine I created with vagrant to either an OVA file for Virtualbox or a package to host in vagrantcloud. The machine exports successfully, but then when I start it up, the directories which should have my files are empty. Seems like a vagrant noob question.
Why would "/usr/local/fieldpapers/" be empty when I start up my exported VM?
What can I do to keep files present in that directory after export?
Vagrant File:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.synced_folder "./", "/usr/local/fieldpapers/", id: "vagrant-root",
owner: "vagrant",
group: "www-data",
mount_options: ["dmode=775,fmode=664"]
config.vm.provider :virtualbox do |vb, override|
vb.memory = 1024
vb.cpus = 1
vb.name = "Field Papers"
override.vm.box = "precise64"
override.vm.box_url = "http://files.vagrantup.com/precise64.box"
override.vm.network :private_network, ip: "192.168.33.10"
override.vm.provision :ansible, :playbook => "provisioning/playbook.yml"
end
end
First check your vagrant version, if it is latest.
Second, delete below line, vagrant will automatically mount your local folder to remote vagrant instance at /vagrant.
config.vm.synced_folder "./", "/usr/local/fieldpapers/", id: "vagrant-root",
owner: "vagrant",
group: "www-data",
mount_options: ["dmode=775,fmode=664"]
I am pretty new using Vagrant and Chef. I am trying to change the Hostname of my Vagrant image using Chef. For some reason, I do not see a changed Hostname when I do vagrant ssh
What am I missing?
Here is my Cheffile
site "http://community.opscode.com/api/v1"
cookbook 'apt'
cookbook 'build-essential'
cookbook 'hostname'
Here is my Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Use Ubuntu 14.04 Trusty Tahr 64-bit as our operating system
config.vm.box = "ubuntu/trusty64"
# Configurate the virtual machine to use 2GB of RAM
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048"]
end
# Forward the Rails server default port to the host
# config.vm.network :forwarded_port, guest: 3000, host: 3000
# Use Chef Solo to provision our virtual machine
config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = ["cookbooks", "site-cookbooks"]
chef.add_recipe "apt"
chef.add_recipe "hostname"
# Install Ruby 2.1.2 and Bundler
# Set an empty root password for MySQL to make things simple
chef.json = {
hostname: {
set_fqdn: 'Olympus'
}
}
end
end
As defined in the documentation you should set the attribute at the node level, not in node['hostname'].
In your case it will be
chef.json = {
set_fqdn: 'Olympus'
}
You can find more information in The hostname documentation