Vagrantfile - multible vm showing up with the same hostname - vagrant

thanks for taking the time to look at my question.
I've created a Vagrantfile which launches two vm's, an ubuntu and a centos. I've stated in the Vagrantfile the hostname of both vm's but when I run vagrant up they both come up with the same hostname of centos90.
What am I doing wrong here.
Vagrant.configure("2") do |config|
config.vm.define "ub80" do |ub80|
ub80.vm.box = "hashicorp/precise32"
config.vm.hostname = "ubuntu80"
config.vm.network "private_network", ip: "192.168.33.80"
end
config.vm.define "cos90" do |cos90|
cos90.vm.box = "centos/7"
config.vm.hostname = "centos90"
config.vm.network "private_network", ip: "192.168.33.90"
end

me again!
syntax was wrong I was using config.vm.xxx for configurations instead of the name given in the config.vm.define command.
Below is the corect syntax:
Vagrant.configure("2") do |config|
config.vm.define "ub80" do |ub80|
ub80.vm.box = "hashicorp/precise32"
ub80.vm.hostname = "ubuntu80"
ub80.vm.network "private_network", ip: "192.168.33.80"
end
config.vm.define "cos90" do |cos90|
cos90.vm.box = "centos/7"
cos90.vm.hostname = "centos90"
cos90.vm.network "private_network", ip: "192.168.33.90"
end

Related

How do I run different playbooks in different guests in a vagrant environment

I have a Vagrantfile that sets up two guests. I would like to provision different playbooks against each guest.
Vagrant.configure(2) do |config|
config.vm.define "awx" do |awx|
awx.vm.box = "centos/7"
awx.vm.hostname ="awx"
awx.vm.network "private_network", ip: "192.168.10.10"
config.vm.provision "ansible" do |master|
master.playbook = "awx.yml"
end
end
config.vm.define "test" do |test|
test.vm.box = "centos/7"
test.vm.hostname = "test"
test.vm.network "private_network", ip: "192.168.10.11"
config.vm.provision "ansible" do |slave|
slave.playbook = "httpd-server.yml"
end
end
end
The the first playbook executes against the first node as expected. However, both playbooks execute against the second node.
The problem here is that you are using the outer scope iteration variable config instead of the inner scope iteration variables for your method invocations. These would lock down the scope of your Ansible provisioner to only vagrant machines defined within that scope. For example, to rectify your problem we would do:
Vagrant.configure(2) do |config|
config.vm.define "awx" do |awx|
awx.vm.box = "centos/7"
awx.vm.hostname ="awx"
awx.vm.network "private_network", ip: "192.168.10.10"
awx.vm.provision "ansible" do |master| # inner scope iter var awx
master.playbook = "awx.yml"
end
end
config.vm.define "test" do |test|
test.vm.box = "centos/7"
test.vm.hostname = "test"
test.vm.network "private_network", ip: "192.168.10.11"
test.vm.provision "ansible" do |slave| # inner scope iter var test
slave.playbook = "httpd-server.yml"
end
end
end

How to allow HTTP traffic and port 27017 on GCE instance with vagrant?

I created a MongoDB GCE instance using Vagrantfile. Then enabled 'Allow HTTP traffic' and added protocol:port tcp:27017 using the console. Everything works fine, but I want to avoid using the console.
Can anyone help me enable 'Allow HTTP traffic' and add 'port tcp:27017' with Vagrantfile?
Here is part of my Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "google/gce"
config.ssh.forward_agent = true
config.vm.provider :google do |google, override|
google.google_project_id = "projectxx"
google.google_client_email = "xx-compute#developer.gserviceaccount.com"
google.google_json_key_location = "~/gcp_service_keys/xx.json"
google.name = "namex"
google.zone = "us-central1-c"
google.image_family = 'ubuntu-1804-lts'
override.ssh.username = "me"
override.ssh.private_key_path = "~/.ssh/gce"
end
config.vm.provision :shell, path: "install.sh"
end
you will have to add a firewall rule and add a "target tag" on it e.g. test-1, then on your vagrant file you will have to use this line google.tags = ['test-1']
Vagrant.configure("2") do |config|
config.vm.box = "google/gce"
config.ssh.forward_agent = true
config.vm.provider :google do |google, override|
google.google_project_id = "projectxx"
google.google_client_email = "xx-compute#developer.gserviceaccount.com"
google.google_json_key_location = "~/gcp_service_keys/xx.json"
google.name = "namex"
google.zone = "us-central1-c"
google.image_family = 'ubuntu-1804-lts'
google.tags = ['test-1'] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
override.ssh.username = "me"
override.ssh.private_key_path = "~/.ssh/gce"
end
config.vm.provision :shell, path: "install.sh"
end
Did you check the vagrant documentation on forwarded ports?
Should be something like:
Vagrant.configure("2") do |config|
config.vm.box = "google/gce"
config.vm.network "forwarded_port", guest: 80, host: 27017
#... rest of your config
end
Adding network tags does the job.
google.tags = ['http-server']

Increase memory of a particular vagrant box

I have this Vagrantfile. Here I defined the memory to be 2048 for all VMs. But I want my puppet master to have 4096 and agents to have 2048. How to do that?
Vagrant.configure("2") do |config|
config.vm.synced_folder ".", "/vagrant", type: "virtualbox"
config.vm.provider "virtualbox" do |v|
v.memory = 2048
v.cpus = 2
end
config.vm.define "puppetmaster" do |pm|
pm.vm.box = "centos/7"
pm.vm.network "private_network", ip: "192.168.33.10"
pm.vm.hostname = "puppetmaster"
end
config.vm.define "puppet-agent-centos" do |pac|
pac.vm.box = "centos/7"
pac.vm.network "private_network", ip: "192.168.33.11"
pac.vm.hostname = "centos-agent"
end
config.vm.define "puppet-agent-ubuntu" do |pau|
pau.vm.box = "ubuntu/xenial64"
pau.vm.network "private_network", ip: "192.168.33.12"
pau.vm.hostname = "ubuntu-agent"
end
end
Thanks!
You can easily do that by overriding the value for a specific VM
config.vm.define "puppetmaster" do |pm|
pm.vm.box = "centos/7"
pm.vm.network "private_network", ip: "192.168.33.10"
pm.vm.hostname = "puppetmaster"
pm.vm.provider "virtualbox" do |pmv|
pmv.memory = 4096
end
end
so your whole file becomes
Vagrant.configure("2") do |config|
config.vm.synced_folder ".", "/vagrant", type: "virtualbox"
config.vm.provider "virtualbox" do |v|
v.memory = 2048
v.cpus = 2
end
config.vm.define "puppetmaster" do |pm|
pm.vm.box = "centos/7"
pm.vm.network "private_network", ip: "192.168.33.10"
pm.vm.hostname = "puppetmaster"
pm.vm.provider "virtualbox" do |pmv|
pmv.memory = 4096
end
end
config.vm.define "puppet-agent-centos" do |pac|
pac.vm.box = "centos/7"
pac.vm.network "private_network", ip: "192.168.33.11"
pac.vm.hostname = "centos-agent"
end
config.vm.define "puppet-agent-ubuntu" do |pau|
pau.vm.box = "ubuntu/xenial64"
pau.vm.network "private_network", ip: "192.168.33.12"
pau.vm.hostname = "ubuntu-agent"
end
end

update IP Address in Vagrant VMs

I have 5 VMs that I spin up using vagrant.
3 load Balancers, and 2 web servers.
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.define "lb0" do |lb0|
lb0.vm.box = "ubuntu/trusty64"
lb0.vm.hostname = "lb0"
lb0.vm.network :private_network, ip: "10.11.13.50"
end
config.vm.define "lb01" do |lb01|
lb01.vm.box = "ubuntu/trusty64"
lb01.vm.hostname = "lb01"
lb01.vm.network :private_network, ip: "10.11.13.51"
end
config.vm.define "lb02" do |lb02|
lb02.vm.box = "ubuntu/trusty64"
lb02.vm.hostname = "lb02"
lb02.vm.network :private_network, ip: "10.11.13.52"
end
config.vm.define "web01" do |web01|
web01.vm.box = "ubuntu/trusty64"
web01.vm.hostname = "web01"
web01.vm.network :private_network, ip: "10.11.12.53"
end
config.vm.define "web02" do |web02|
web02.vm.box = "ubuntu/trusty64"
web02.vm.hostname = "web02"
web02.vm.network :private_network, ip: "10.11.12.54"
end
end
I just recently updated the IPs of my load balancers from 10.11.12.x --> 10.11.13.x due to the new requirement.
I run vagrant up again, I don't see new IPs updated on my lb0, lb1, and lb2. :( Did I miss anything ?
What do I do to update my existing VMs IP without having to destroy them and recreate them ?
After adjusting the new IP in Vagrantfile
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.define "lb0" do |lb0|
lb0.vm.box = "ubuntu/trusty64"
lb0.vm.hostname = "lb0"
lb0.vm.network :private_network, ip: "10.11.13.50"
end
config.vm.define "lb01" do |lb01|
lb01.vm.box = "ubuntu/trusty64"
lb01.vm.hostname = "lb01"
lb01.vm.network :private_network, ip: "10.11.13.51"
end
config.vm.define "lb02" do |lb02|
lb02.vm.box = "ubuntu/trusty64"
lb02.vm.hostname = "lb02"
lb02.vm.network :private_network, ip: "10.11.13.52"
end
config.vm.define "web01" do |web01|
web01.vm.box = "ubuntu/trusty64"
web01.vm.hostname = "web01"
web01.vm.network :private_network, ip: "10.11.12.53"
end
config.vm.define "web02" do |web02|
web02.vm.box = "ubuntu/trusty64"
web02.vm.hostname = "web02"
web02.vm.network :private_network, ip: "10.11.12.54"
end
end
I've learn that I have to run vagrant reload
Then, I noticed my IP are updated as expected.
This is weird, normally if you change the network configuration from the Vagrantfile, vagrant should update accordingly in your VM.
If you enable something like config.vm.network :private_network, ip: "10.11.13.52" you should see at the end of file /etc/network/interfaces something like
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
auto eth1
iface eth1 inet static
address 10.11.13.52
netmask 255.255.255.0
#VAGRANT-END
If you do not see your updated IP in the file, change it manually and run sudo /etc/init.d/networking restart to have the new IP available
Update your Vagrantfile with the ip address available and vagrant reload to effect the changes.

Why isn't vagrant private network not set up when configured within provider block?

I'm trying to understand why putting the network options in the provider block of Vagrantfile doesn't work. For example, this seems to work:
config.vm.define "mymachine" do |vbox_config|
vbox_config.vm.network :private_network, ip: "10.10.10.10"
vbox_config.vm.provider :virtualbox do |vb|
vb.ssh.forward_agent = true
end
end
but this doesn't work (network is not accessible):
config.vm.define "mymachine" do |vbox_config|
vbox_config.vm.provider :virtualbox do |vb|
vb.ssh.forward_agent = true
vb.vm.network :private_network, ip: "10.10.10.10"
end
end
It seems like I would want to set the network options to be specific to a provider and structure the Vagrantfile like the latter. Any ideas why it's not working?
If you want to set non-provider specific configuration inside a provider block you have to use a different syntax:
config.vm.define "mymachine" do |vbox_config|
vbox_config.vm.provider :virtualbox do |vb, override|
override.ssh.forward_agent = true
override.vm.network :private_network, ip: "10.10.10.10"
end
end
Indeed, the first argument is for provider specific configuration (e.g. vb.customize), while the second is for overrides. Please see the documentation for more information.

Resources