vagrant - memory and cpus definitions are not working - vagrant

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

Related

Vagrant multiple machines override

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

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
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

Vagrant Synced Folders not showing

I want to sync my OSX dev folder which contains my applications to my VM. My VagrantFile (based off Phansible):
Vagrant.require_version ">= 1.5"
Vagrant.configure("2") do |config|
config.vm.provider :virtualbox do |v|
v.name = "default"
v.customize [
"modifyvm", :id,
"--name", "default",
"--memory", 512,
"--natdnshostresolver1", "on",
"--cpus", 1,
]
end
config.vm.box = "ubuntu/trusty64"
config.vm.network :private_network, ip: "192.168.33.99"
config.ssh.forward_agent = true
if which('ansible-playbook')
config.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible/playbook.yml"
ansible.inventory_path = "ansible/inventories/dev"
ansible.limit = 'all'
ansible.extra_vars = {
private_interface: "192.168.33.99",
hostname: "default"
}
end
else
config.vm.provision :shell, path: "ansible/windows.sh", args: ["default"]
end
config.vm.synced_folder "/Users/xylar/Code", "/vagrant", type: "nfs"
end
When I vagrant up:
==> default: Exporting NFS shared folders...
==> default: Preparing to edit /etc/exports. Administrator privileges will be required...
==> default: Mounting NFS shared folders...
==> default: Running provisioner: ansible...
There are no error messages and when I vagrant ssh and view contents of the vagrant folder I only see some dot files (ansible, bash etc). Is there something I have missed?
I was being foolish. Once I had ssh'd into the box I thought I was in the synced folder (as it was called vagrant) however I was in /home/vagrant and the synced location was in /vagrant.
You need to specify the mount options:
config.vm.synced_folder "/Users/xylar/Code", "/vagrant", "nfs" => { :mount_options => ['dmode=777', 'fmode=777'] }

Vagrant + Chef + Hostname

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

Vagrant complaints about "customize"

I'm having a weird problem with Vagrant. Changing the default RAM of the virtual machine would have to be easy but I don't know why I am not able to do it.
My code is very simple:
# -*- 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.define "mimeticStack" do |v|
v.vm.box = "precise64"
v.vm.box_url = "http://files.vagrantup.com/precise64.box"
v.vm.network "private_network", ip: "192.168.33.10"
v.vm.network "forwarded_port", guest: 80, host: 8080
v.vm.hostname = "dev.mimetic.local"
v.vm.customize ["modifyvm", :id, "--memory", "512"]
end
end
Then if I run "vagrant up", Vagrant returns:
vm:
* The following settings shouldn't exist: customize
The issue was fixed:
# -*- 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.define "mimeticStack" do |v|
v.vm.box = "precise64"
v.vm.box_url = "http://files.vagrantup.com/precise64.box"
v.vm.network "private_network", ip: "192.168.33.10"
v.vm.network "forwarded_port", guest: 80, host: 8080
v.vm.hostname = "dev.mimetic.local"
v.vm.provider :virtualbox do |vb|
vb.customize ['modifyvm', :id,'--memory', '512']
end
end
end
I left the code here for Vagrant beginners like me.
I have tried #MikeD's suggestion with
config.vm.provider "virtualbox" do |vb|
vb.memory = "<some size>"
vb.cpus = "<some number>"
end
and it works as expected. I can ssh into my vagrant box and run lscpu and cat /proc/meminfo which give me the values I specified above.

Resources