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
There's a multi-machine Vagrant setup (truncated here to two machines) like the following:
Vagrant.configure(2) do |config|
config.vm.define "xfcevm" do |xfcevm|
xfcevm.vm.box = "generic/ubuntu1904"
xfcevm.vm.hostname = "xfcevm"
end
config.vm.define "gnomevm" do |gnomevm|
gnomevm.vm.box = "generic/fedora30"
gnomevm.vm.hostname = "gnomevm"
end
config.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.memory = "2048"
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
# halt here
end
If the tests playbook passes without errors then I want that machine to be halted just after the tests.yml playbook is finished. How I can do that from Vagrantfile or by creating another Ansible task?
You can issue a shutdown command at the end of your test playbook. It will only be played if the rest of the tasks were successful.
- name: shutdown machine
become: true
command: shutdown -h now
See shutdown --help to adapt the command to your specific need (e.g. using halt instead of poweroff)
I'm using Vagrant to create three hosts. I want to give them the name node01, node02 and node03.
Using the following Vagrantfile:
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.define "node01" do |node01|
node01.vm.box = "ubuntu/trusty64"
node01.vm.hostname = "node01"
end
config.vm.define "node02" do |node02|
node02.vm.box = "ubuntu/trusty64"
node02.vm.hostname = "node02"
end
config.vm.define "node03" do |node03|
node03.vm.box = "ubuntu/trusty64"
node03.vm.hostname = "node03"
end
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
end
end
On virtualbox, the names are cryptic (like ubuntu-3nodes-node01-timestamp ...). How can I ensure they are just called node01, node02 and node03
I know I can do in the config.vm.provider "virtualbox" but I would ideally do it in the config.vm.define "node01" sections
you can do something like this
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
(1..3).each do |i|
config.vm.define "node0#{i}" do |node|
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.name = "node0#{i}"
end
node.vm.hostname = "node0#{i}"
end
end
end
This is some ruby to loop on the node.
the box if its the same in all machine can be defined at the upper block level
You must make the name of the name of VirtualBox VM in the virtualbox block as it depends on virtualbox (vagrant can manage other provider and this would be different)
You can also separate loop from define node method.
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
def define_node(config, node_number)
config.vm.define "node0#{node_number}" do |node|
node.vm.hostname = "node0#{node_number}"
node.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
vb.name = "node0#{node_number}"
end
end
end
for node_number in 1..3
define_node config, node_number
end
end
I'm trying to create multi-machine environment in Vagrant using Ansible as provisioner.
My Vagrantfile looks like next:
Vagrant.configure("2") do |config|
config.vm.provision "ansible" do |ansible|
ansible.limit = "all"
ansible.playbook = "main.yml"
ansible.inventory_path = "staging"
ansible.verbose = "-vvvv"
end
config.vm.define "machine1" do |machine1|
machine1.vm.box = "ubuntu/trusty64"
machine1.vm.network "private_network", ip:"192.168.77.10"
machine1.vm.hostname = "machine1"
machine1.vm.provider :virtualbox do |vb|
vb.name = "machine1"
end
end
config.vm.define "machine2" do |machine2|
machine2.vm.box = "ubuntu/trusty64"
machine2.vm.network "private_network", ip:"192.168.77.11"
machine2.vm.hostname = "machine2"
machine2.vm.provider :virtualbox do |vb|
vb.name = "machine2"
end
end
config.vm.define "machine3" do |machine3|
machine3.vm.box = "ubuntu/trusty64"
machine3.vm.network "private_network", ip:"192.168.77.12"
machine3.vm.hostname = "machine3"
machine3.vm.provider :virtualbox do |vb|
vb.name = "machine3"
end
end
end
Inventory:
[AppServers]
192.168.77.10
192.168.77.11
192.168.77.12
[WebServers]
192.168.77.11
192.168.77.12
[WebLoadBalancers]
192.168.77.10
[SlaveDbServers]
192.168.77.10
192.168.77.12
[MasterDbServers]
192.168.77.11
[DbLoadBalancers]
192.168.77.11
main.yml:
- hosts: all
roles:
- Common
- ConsulServer
- ConsulAgent
- hosts: WebServers
roles:
- WebServer
- hosts: WebLoadBalancers
roles:
- LoadBalancer
- hosts: MasterDbServers
roles:
- DbServer
I want to get 3 machines. All of them have to contain common soft(Consul servers and agents, vim etc). And some additional - own for each machine. But once i'm running "vagrant up" only first machine created, provisioner runs, fails because other 2 not created.
Is it possible to run provisioner after all machines created? Or my approach is incorrect and i should perform this in other way?
Thank you for your time.
The first problem I had was ERROR: cannot find role in.... I'm assuming you have these roles and excluded them for brevity. My advice here is to have a simple Ansible playbook when you are testing this:
---
- hosts: all
gather_facts: false
tasks:
- command: hostname -f
Secondly, the problem at hand surround the use of static inventory file and caveats therein. You are seeing an error because the Ansible provisioner is failing to find all hosts when it runs after the first machine is up but the others are not.
Lastly, each machine will have a different key, which you must pass. So, following Vagrant's documented approach for multi-machine parallelism with Ansible and with help from this work-around, here is what I recommend your Vagrantfile look like:
Vagrant.configure("2") do |config|
N = 3
VAGRANT_VM_PROVIDER = "virtualbox"
ANSIBLE_RAW_SSH_ARGS = []
(1..N-1).each do |machine_id|
ANSIBLE_RAW_SSH_ARGS << "-o IdentityFile=.vagrant/machines/machine#{machine_id}/#{VAGRANT_VM_PROVIDER}/private_key"
end
(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.#{10+machine_id-1}"
# Only execute once the Ansible provisioner,
# when all the machines are up and ready.
if machine_id == N
machine.vm.provision :ansible do |ansible|
# Disable default limit to connect to all the machines
ansible.limit = "all"
ansible.playbook = "main.yml"
ansible.inventory_path = "staging"
ansible.verbose = "-v"
ansible.raw_ssh_args = ANSIBLE_RAW_SSH_ARGS
end
end
end
end
end
I've got a vagrant file that builds a local VM. I want to add the EC2 provider and have the option of either provisioning a local VM or one on EC2.
Can I create configs for multiple providers in the same Vagrantfile and somehow choose which to run when I do vagrant up?
You can use a multi-vm environment, where every VM can be provisioned with a different provider and you can choose on commandline which one you want to vagrant up <machine>.
add box for each provider
> vagrant box add precise64 http://file.vagrantup.com/precise64.box
> vagrant box add precise64 http://file.vagrantup.com/precise64_vmware_fusion.box
and your Vagrantfile should look like
Vagrant.configure(2) do |config|
config.vm.box="precise64"
config.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--memory", "2048"]
end
config.vm.provider "vmware_fusion" do |v|
v.vmx["memsize"] = "2048"
end
end
then create on each provider using following commands
> vagrant up --provider=virtualbox
> vagrant up --provider=vmware_fusion
You can choose which provider you want to run by --provider parameter.
Here is ruby code in Vagrantfile which can run different VM depending which provider you have chosen:
require 'getoptlong'
# Parse CLI arguments.
opts = GetoptLong.new(
[ '--provider', GetoptLong::OPTIONAL_ARGUMENT ],
)
provider='virtualbox'
begin
opts.each do |opt, arg|
case opt
when '--provider'
provider=arg
end # case
end # each
rescue
end
# Vagrantfile API/syntax version.
Vagrant.configure(2) do |config|
config.vm.hostname = "vagrant"
config.vm.define "default-#{provider}"
config.vm.provider "virtualbox" do |vbox, override|
vbox.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
vbox.name = "test.local"
override.vm.box = "ubuntu/wily64"
override.vm.network "private_network", ip: "192.168.22.22"
end
config.vm.provider :aws do |aws, override|
aws.ami = "ami-7747d01e"
aws.instance_type = "m3.medium"
override.vm.box = "dummy"
override.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
override.ssh.username = "ubuntu"
end
end
So by the default provider is virtualbox, but you can specify from the command line, like:
vagrant up --provider=aws
To run VM locally you can run:
vagrant up --provider=virtualbox
and if you'd like to run VM using different provider then you can use:
vagrant up --provider=aws
However, remember that you have to install appropriate provider plugin before you will use it.
Please check this gist posted by #tknerr which works with all providers such as virtualbox, AWS and managed in combination with the vagrant-omnibus plugin. Here is the code from Vagrantfile:
#
# Vagrantfile for testing
#
Vagrant::configure("2") do |config|
# the Chef version to use
config.omnibus.chef_version = "11.4.4"
def configure_vbox_provider(config, name, ip, memory = 384)
config.vm.provider :virtualbox do |vbox, override|
# override box url
override.vm.box = "opscode_ubuntu-13.04_provisionerless"
override.vm.box_url = "https://opscode-vm.s3.amazonaws.com/vagrant/opscode_ubuntu-13.04_provisionerless.box"
# configure host-only network
override.vm.hostname = "#{name}.local"
override.vm.network :private_network, ip: ip
# enable cachier for local vbox vms
override.cache.auto_detect = true
# virtualbox specific configuration
vbox.customize ["modifyvm", :id,
"--memory", memory,
"--name", name
]
end
end
def configure_aws_provider(config)
config.vm.provider :aws do |aws, override|
# use dummy box
override.vm.box = "aws_dummy_box"
override.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
# override ssh user and private key
override.ssh.username = "ubuntu"
override.ssh.private_key_path = "#{ENV['HOME']}/.ssh/mccloud_rsa"
# aws specific settings
aws.access_key_id = "XXXX"
aws.secret_access_key = "XXXXX"
aws.ami = "ami-524e4726"
aws.region = "eu-west-1"
aws.availability_zone = "eu-west-1c"
aws.instance_type = "m1.small"
aws.security_groups = [ "mccloud", "http" ]
aws.keypair_name = "mccloud-key-tlc"
end
end
def configure_managed_provider(config, server)
config.vm.provider :managed do |managed, override|
# use dummy box
override.vm.box = "managed_dummy_box"
override.vm.box_url = "https://github.com/tknerr/vagrant-managed-servers/raw/master/dummy.box"
# link with this server
managed.server = server
end
end
#
# define a separate VMs for the 3 providers (vbox, aws, managed)
# because with Vagrant 1.2.2 you can run a VM with only one provider at once
#
[:aws, :vbox, :esx].each do |provider|
#
# Sample VM per provider
#
config.vm.define :"sample-app-#{provider}" do | sample_app_config |
case provider
when :vbox
configure_vbox_provider(sample_app_config, "sample-app", "33.33.40.10")
when :aws
configure_aws_provider(sample_app_config)
when :esx
configure_managed_provider(sample_app_config, "33.33.77.10")
end
sample_app_config.vm.provision :chef_solo do |chef|
chef.cookbooks_path = [ './cookbooks/sample-app-0.1.0' ]
chef.add_recipe "sample-app"
chef.json = {
:sample_app => {
:words_of_wisdom => "Vagrant on #{provider} Rocks!"
}
}
end
end
end
end
From the Vagrant docs:
Multiple config.vm.provision methods can be used to define multiple provisioners. These provisioners will be run in the order they're defined.
eg.:
First install puppet in the machine and then run some puppet manifests:
$script = "
wget http://apt.puppetlabs.com/puppetlabs-release-precise.deb
sudo dpkg -i puppetlabs-release-precise.deb
sudo apt-get update
sudo aptitude -yy install puppet
"
config.vm.provision "shell", inline: $script
config.vm.provision "puppet" do |puppet|
puppet.manifests_path = "manifest/puppet"
puppet.manifest_file = "init.pp"
end
config.vm.provision "shell", inline: "echo Second shell provisioner"
Yes, you can specify multiple machines by using the config.vm.define method call, for example:
Vagrant.configure("2") do |config|
config.vm.provision "shell", inline: "echo Hello"
config.vm.define "web" do |web|
web.vm.box = "apache"
end
config.vm.define "db" do |db|
db.vm.box = "mysql"
end
end
See: Defining multiple machines at Vagranup Docs and Providers