I'm setting up a new Ansible controller and nodes on virtualbox with a Vargrantfile. I continue to get the error:
There is a syntax error in the following Vagrantfile. The syntax error
message is reproduced below for convenience:
/home/vagrant/ansible/Vagrantfile:19: syntax error, unexpected end-of-input, > expecting keyword_end
I'm changing the placement of the "end" keyword over and over, but still getting the same error on different lines. I'm sure this is simple and I'm just missing it, it's been one of those weeks...
Vagrant.configure("2") do |config|
config.vm.define "controller" do |controller|
controller.vm.box = "bento/ubuntu 16.04"
controller.vm.hostname = "controller"
controller.vm.network :private_network, ip: "10.10.10.10"
controller.vm.provider "virtualbox" do |vb|
vb.memory = "256"
end
end
(1..3).each do |i|
config.vm.define "node#{i}" do |node|
node.vm.box = "bento/ubuntu-16.04"
node.vm.hostname = "node#{i}"
node.vm.network :private_network, ip: "10.10.10.1#{i}"
node.vm.provider "virtualbox" do |vb|
vb.memory = "256"
end
end
I should be able to run vagrant up in the terminal and get an output of provisioning controllers/nodes in virtualbox. What am I missing?
You're missing 2 "end"s at the end of the file
Vagrant.configure("2") do |config|
config.vm.define "controller" do |controller|
controller.vm.box = "bento/ubuntu 16.04"
controller.vm.hostname = "controller"
controller.vm.network :private_network, ip: "10.10.10.10"
controller.vm.provider "virtualbox" do |vb|
vb.memory = "256"
end
end
(1..3).each do |i|
config.vm.define "node#{i}" do |node|
node.vm.box = "bento/ubuntu-16.04"
node.vm.hostname = "node#{i}"
node.vm.network :private_network, ip: "10.10.10.1#{i}"
node.vm.provider "virtualbox" do |vb|
vb.memory = "256"
end
end
end
end
Related
My Vagrantfile creates three VMs: k8s-master, ndoe-1, and node-2. How can I enforce that k8s-master is fully provisioned before the provisioning for node-1 and nod-2 starts?
Here is my Vagrantfile:
IMAGE_NAME = "generic/ubuntu1804"
N = 2
Vagrant.configure("2") do |config|
config.ssh.insert_key = false
config.vm.provider "libvirt" do |v|
v.memory = 1024
v.cpus = 2
end
config.vm.define "k8s-master" do |master|
master.vm.box = IMAGE_NAME
master.vm.network "private_network", ip: "192.168.50.10"
master.vm.hostname = "k8s-master"
master.vm.provision "ansible" do |ansible|
ansible.playbook = "kubernetes-setup/master-playbook.yml"
ansible.extra_vars = {
node_ip: "192.168.50.10",
}
ansible.compatibility_mode = "2.0"
end
end
(1..N).each do |i|
config.vm.define "node-#{i}" do |node|
node.vm.box = IMAGE_NAME
node.vm.network "private_network", ip: "192.168.50.#{i + 10}"
node.vm.hostname = "node-#{i}"
node.vm.provision "ansible" do |ansible|
ansible.playbook = "kubernetes-setup/node-playbook.yml"
ansible.extra_vars = {
node_ip: "192.168.50.#{i + 10}",
}
ansible.compatibility_mode = "2.0"
end
end
end
end
The multi-machine documentation mentions outside-in ordering, so I tried putting the for-each loop inside the k8s-master's define-block, but then the nodes are not created at all.
The following Vagrantfile configuration won't work as the next VM gets the same name as previous one. (BTW, not 100% sure, but I think this used to work in previous Vagrant versions).
Vagrant.configure(2) do |config|
config.vm.define "xfcevm" do |xfcevm|
xfcevm.vm.box = "generic/ubuntu1904"
xfcevm.vm.hostname = "xfcevm"
config.vm.provider "virtualbox" do |vb|
vb.name = "xfcevm"
end
end
config.vm.define "gnomevm" do |gnomevm|
gnomevm.vm.box = "generic/fedora30"
gnomevm.vm.hostname = "gnomevm"
config.vm.provider "virtualbox" do |vb|
vb.name = "gnomevm"
end
end
config.vm.provider "virtualbox" do |vb|
# vb.name = config.vm.hostname
vb.gui = true
vb.memory = "3072"
vb.cpus = 1
vb.customize ["modifyvm", :id, "--vram", "32"]
end
config.vm.provision "ansible" do |ansible|
ansible.verbose = "v"
ansible.compatibility_mode = "2.0"
ansible.playbook = "setup.yml"
end
config.vm.provision "ansible", run: 'always' do |ansible|
ansible.verbose = "v"
ansible.compatibility_mode = "2.0"
ansible.playbook = "tests.yml"
end
end
In the line # vb.name = config.vm.hostname, the assignment returns an object (it is printed with puts as #<Object:0x0000000001c191d8>) and I'm not familiar with Ruby and Vagrant enough to get a string attribute from it if that's even possible from that object.
P.S. a workaround (an alternative question to this one) would be to get the running VirtualBox machine name from Ansible playbook, as the goal has been calling VBoxManage on that virtual machine as local_action from inside the Ansible playbook.
You are running the virtualbox provider on the config global object.
# Wrong for multivm
# This sets a default name for all vms
config.vm.provider "virtualbox" do |vb|
vb.name = "gnomevm"
end
Simply call this on the current vm you are defining (example for gnomevm)
# Correct
# This sets the specific name for this vm only
gnomevm.vm.provider "virtualbox" do |vb|
vb.name = "gnomevm"
end
I have vagrant-hostmanager 1.8.6 installed, and when I run vagrant hostmanager I end up with the following error:
[vagrant-hostmanager:guest] Updating hosts file on the virtual machine puppet_server...
sh: 1: Syntax error: "(" unexpected
...and the /etc/hosts file is not updated. Is there a way to fix this?
Here is my VagrantFile:
Vagrant.configure(2) do |config|
config.vm.box = "blah/turnkey-lamp-14.2"
config.vm.provider "virtualbox" do |vb|
vb.cpus = 2
vb.gui = true
end
config.vm.boot_timeout = 10000
config.vm.network "private_network", type: "dhcp"
#config.vm.provision :hostmanager
config.ssh.insert_key = false
#config.ssh.private_key_path = "/mnt/vm_lab/vagrant_box_storage/.vagrant.d/insecure_private_key"
config.ssh.forward_agent = true
config.hostmanager.enabled = true
config.hostmanager.manage_guest = true
#config.hostmanager.manage_host = true
config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
if vm.id
`VBoxManage guestproperty get #{vm.id} "/VirtualBox/GuestInfo/Net/1/V4/IP".split()[1]`
end
end
config.vm.define :puppet_server do |srv|
srv.vm.hostname = "puppet-server"
srv.vm.network :private_network, ip: '10.0.3.15'
srv.vm.provision "shell", inline: $puppetServerScript
srv.vm.synced_folder "src/puppet-server", "/etc/puppet", create: true
end
config.vm.define :bareOSdirector do |srv|
srv.vm.hostname = "bareOSdirector"
srv.vm.network :private_network, ip: '10.0.3.10'
srv.vm.provision "shell", inline: $puppetClientBareOSdir
end
config.vm.define :webserver do |srv|
srv.vm.hostname = "webserver"
srv.vm.network :private_network, ip: '10.0.3.8'
srv.vm.provision "shell", inline: $puppetClientWebserver
end
end
I thought it had something to do with the hosts file itself, so I emptied it and re-ran the command, but it still doesn't update it.
sh: 1: Syntax error: "(" unexpected turned out to be a shell error because the ruby split was contained in the shell command by accident.
There were 2 problems...
The syntax was a little bit wrong there was a ``` in the wrong spot between the shell and the ruby syntax.
I was getting the IP address of the wrong card...
I tried running the command to obtain the from the command line with:
VBoxManage guestproperty get "puppet_server" "/VirtualBox/GuestInfo/Net/1/V4/IP
Value: 172.x.x.x
and it yielded the wrong ip address...so instead I tried:
VBoxManage guestproperty get "puppet_server" "/VirtualBox/GuestInfo/Net/2/V4/IP
Value: 10.0.3.15
and I got the address I was expecting.
The block:
config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
if vm.id
`VBoxManage guestproperty get #{vm.id} "/VirtualBox/GuestInfo/Net/1/V4/IP".split()[1]`
end
end
Should have been:
config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
if vm.id
`VBoxManage guestproperty get #{vm.id} "/VirtualBox/GuestInfo/Net/2/V4/IP"`.split()[1]
end
end
Then after bringing all the machines up, I ran vagrant hostmanager again, and all the /etc/hosts files were filled in as expected.
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
How do I tell vagrant to provision based on condition? I am spinning up multi VMs under a loop
Vagrantfile:
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# Iterate through entries in YAML file
servers.each do |servers|
config.vm.define servers["name"] do |srv|
srv.vm.hostname = servers["name"] + DOMAIN
srv.vm.box = servers["box"]
srv.vm.network "private_network", ip: servers["ip"]
srv.vm.synced_folder ".", "/data", id: "mapping", disabled: servers["share"]
srv.vm.provider :virtualbox do |vb|
vb.memory = servers["ram"]
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
end
end
end
I want to add something like this:
servers.each do |servers|
config.vm.define servers["name"] do |srv|
srv.vm.hostname = servers["name"] + DOMAIN
srv.vm.box = servers["box"]
srv.vm.network "private_network", ip: servers["ip"]
srv.vm.synced_folder ".", "/data", id: "mapping", disabled: servers["share"]
srv.vm.provider :virtualbox do |vb|
vb.memory = servers["ram"]
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
if servers["name"] == "server1"
config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = "#{cookbooks}"
chef.add_recipe "chef_blah::server"
end
else
puts("Skipping it...")
end
end
end
But every time it executes the provision block on all the VMs
That should be srv.vm.provision, i.e. you are defining it on only that server not the root Vagrant config.
I think I found the solution. It was my mistake
instead of:
config.vm.provision :chef_solo do |chef|
I should have used
srv.vm.provision :chef_solo do |chef|
In the srv loop