Search code examples
vagrantprovisioningvagrantfile

Vagrant multiple vm with separate provisioning scripts


Currently trying to set up multiple VMs, with separate provisioning scripts.

Vagrantfile is as follows;

dir = Dir.pwd
vagrant_dir = File.expand_path(File.dirname(__FILE__))

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

    box_name = "puppetlabs/centos-6.5-64-puppet"

    # our web/app servers and their IPs - format 192.168.1.8##
    app_servers = {

        'web11' => {
            :name => 'web11',
            :ip => '192.168.1.811',
            :box => box_name,
            :forwarded_port_guest => 811,
            :forwarded_port_host => 8811
        },

        'web21' => {
            :name => 'web21',
            :ip => '192.168.1.821',
            :box => box_name,
            :forwarded_port_guest => 821,
            :forwarded_port_host => 8821
        },

        'learninglocker' => {
            :name => 'learninglocker',
            :ip => '192.168.1.899',
            :box => box_name,
            :forwarded_port_guest => 899,
            :forwarded_port_host => 8899
        }

    }

    app_servers.each do |key,value|

        boxname = value[:name]

        config.vm.define boxname do |app_config|

            app_config.vm.provision "shell", inline: "echo !!!thisname!!!! #{key}"
            app_config.vm.provision "shell", inline: "echo !!!thisip!!!! #{value[:ip]}"

            app_config.vm.box = value[:box]
            app_config.vm.host_name = "%s.vagrant" % value[:name]

            app_config.vm.network "private_network", ip: value[:ip]
            app_config.vm.network "forwarded_port", guest: value[:forwarded_port_guest], host: value[:forwarded_port_host]

            app_config.ssh.forward_agent = true

            provision_filename = key.to_s + "-provision.sh"
            config.vm.provision "shell", inline: "echo #{provision_filename}"

            # provisioning
            if File.exists?(File.join(vagrant_dir,'provision',boxname + "-provision.sh")) then
                config.vm.provision "shell", inline: "echo +++exists+++"
                config.vm.provision :shell, :path => File.join( "provision", boxname + "-provision.sh" )
            else
                config.vm.provision "shell", inline: "echo PROVISION FILE DOES NOT EXIST!"
            end

            # Shared NFS folder
            # config.vm.synced_folder "shared/nfs/", "/vagrant/", type: "nfs"
            config.vm.synced_folder "shared/nfs/", "/vagrant/"

        end # config.vm.define opts[:name] do |config|

    end # app_servers.each

I also have a provision/web11-provision.sh file (but no web21 or learninglocker equivalents) which basically just prints out a message for now.

The problem: When running a vagrant up when it brings up web11 it prints the message that the file exists (expected). When it brings up web21, boxname is still web11 on first go around and so runs the provisioning script, then it goes again and changes to web21 so doesn't find the file. The same thing happens for learninglocker - runs web11, then web21, then learninglocker.

I'm clearly being dumb in terms of my .each statement, but can't figure out what it is... help?

Here's the full output of the vagrant up...

vagrant up     
Bringing machine 'web11' up with 'virtualbox' provider...
Bringing machine 'web21' up with 'virtualbox' provider...
Bringing machine 'learninglocker' up with 'virtualbox' provider...
Bringing machine 'db22' up with 'virtualbox' provider...
Bringing machine 'db23' up with 'virtualbox' provider...
Bringing machine 'loadbalancer' up with 'virtualbox' provider...
==> web11: Importing base box 'puppetlabs/centos-6.5-64-puppet'...
==> web11: Matching MAC address for NAT networking...
==> web11: Checking if box 'puppetlabs/centos-6.5-64-puppet' is up to date...
==> web11: Setting the name of the VM: verf_web11_1413999007267_1427
==> web11: Clearing any previously set network interfaces...
==> web11: Preparing network interfaces based on configuration...
    web11: Adapter 1: nat
    web11: Adapter 2: hostonly
==> web11: Forwarding ports...
    web11: 22 => 2222 (adapter 1)
    web11: 811 => 8811 (adapter 1)
==> web11: Booting VM...
==> web11: Waiting for machine to boot. This may take a few minutes...
    web11: SSH address: 127.0.0.1:2222
    web11: SSH username: vagrant
    web11: SSH auth method: private key
    web11: Warning: Connection timeout. Retrying...
==> web11: Machine booted and ready!
==> web11: Checking for guest additions in VM...
==> web11: Setting hostname...
==> web11: Configuring and enabling network interfaces...
==> web11: Mounting shared folders...
    web11: /vagrant => /Users/mbcx9rvt/Sites/UBC/CTLT/installs/spaces/vagrant/verf/shared/nfs
==> web11: Running provisioner: shell...
    web11: Running: inline script
web11-provision.sh
==> web11: Running provisioner: shell...
    web11: Running: inline script
+++exists+++
==> web11: Running provisioner: shell...
    web11: Running: /var/folders/c6/1yd6r3px03j_l3f4g0kfkjfm0000gn/T/vagrant-shell20141022-35420-ulkjfx
Check for apt packages to install...
httpd,
php,
php-gd,
php-pear,
php-xml,
php-xmlrpc,
php-mbstring,
php-mcrypt,
php-tidy,
curl,
curl-devel,
php-pecl-apc
No yum packages to install.

==> web11: Running provisioner: shell...
    web11: Running: inline script
!!!thisname!!!! web11
==> web11: Running provisioner: shell...
    web11: Running: inline script
!!!thisip!!!! 192.168.1.811
==> web11: Checking for host entries
==> web11: adding to (/etc/hosts) : 192.168.1.811  web11.vagrant  # VAGRANT: 71c96f7d0c293c081643fe8aeec588e3 (web11) / 1e8cff2f-4e26-4b70-99eb-e62e9b976413
==> web21: Importing base box 'puppetlabs/centos-6.5-64-puppet'...
==> web21: Matching MAC address for NAT networking...
==> web21: Checking if box 'puppetlabs/centos-6.5-64-puppet' is up to date...
==> web21: Setting the name of the VM: verf_web21_1413999051481_37940
==> web21: Fixed port collision for 22 => 2222. Now on port 2200.
==> web21: Clearing any previously set network interfaces...
==> web21: Preparing network interfaces based on configuration...
    web21: Adapter 1: nat
    web21: Adapter 2: hostonly
==> web21: Forwarding ports...
    web21: 22 => 2200 (adapter 1)
    web21: 821 => 8821 (adapter 1)
==> web21: Booting VM...
==> web21: Waiting for machine to boot. This may take a few minutes...
    web21: SSH address: 127.0.0.1:2200
    web21: SSH username: vagrant
    web21: SSH auth method: private key
    web21: Warning: Connection timeout. Retrying...
==> web21: Machine booted and ready!
==> web21: Checking for guest additions in VM...
==> web21: Setting hostname...
==> web21: Configuring and enabling network interfaces...
==> web21: Mounting shared folders...
    web21: /vagrant => /Users/mbcx9rvt/Sites/UBC/CTLT/installs/spaces/vagrant/verf/shared/nfs
==> web21: Running provisioner: shell...
    web21: Running: inline script
web11-provision.sh
==> web21: Running provisioner: shell...
    web21: Running: inline script
+++exists+++
==> web21: Running provisioner: shell...
    web21: Running: /var/folders/c6/1yd6r3px03j_l3f4g0kfkjfm0000gn/T/vagrant-shell20141022-35420-1bv1h0u
Check for apt packages to install...
httpd,
php,
php-gd,
php-pear,
php-xml,
php-xmlrpc,
php-mbstring,
php-mcrypt,
php-tidy,
curl,
curl-devel,
php-pecl-apc
No yum packages to install.

==> web21: Running provisioner: shell...
    web21: Running: inline script
web21-provision.sh
==> web21: Running provisioner: shell...
    web21: Running: inline script
PROVISION FILE DOES NOT EXIST!
==> web21: Running provisioner: shell...
    web21: Running: inline script
!!!thisname!!!! web21
==> web21: Running provisioner: shell...
    web21: Running: inline script
!!!thisip!!!! 192.168.1.821
==> web21: Checking for host entries
==> web21: adding to (/etc/hosts) : 192.168.1.821  web21.vagrant  # VAGRANT: ed18947cb0a37a2d7811d08949b44331 (web21) / b33cde0c-da4f-4739-9f0e-332f0bfd1b67
==> learninglocker: Importing base box 'puppetlabs/centos-6.5-64-puppet'...
==> learninglocker: Matching MAC address for NAT networking...
==> learninglocker: Checking if box 'puppetlabs/centos-6.5-64-puppet' is up to date...
==> learninglocker: Setting the name of the VM: verf_learninglocker_1413999103267_74239
==> learninglocker: Fixed port collision for 22 => 2200. Now on port 2201.
==> learninglocker: Clearing any previously set network interfaces...
==> learninglocker: Preparing network interfaces based on configuration...
    learninglocker: Adapter 1: nat
    learninglocker: Adapter 2: hostonly
==> learninglocker: Forwarding ports...
    learninglocker: 22 => 2201 (adapter 1)
    learninglocker: 899 => 8899 (adapter 1)
==> learninglocker: Booting VM...
==> learninglocker: Waiting for machine to boot. This may take a few minutes...
    learninglocker: SSH address: 127.0.0.1:2201
    learninglocker: SSH username: vagrant
    learninglocker: SSH auth method: private key
    learninglocker: Warning: Connection timeout. Retrying...
==> learninglocker: Machine booted and ready!
==> learninglocker: Checking for guest additions in VM...
==> learninglocker: Setting hostname...
==> learninglocker: Configuring and enabling network interfaces...
==> learninglocker: Mounting shared folders...
    learninglocker: /vagrant => /Users/mbcx9rvt/Sites/UBC/CTLT/installs/spaces/vagrant/verf/shared/nfs
==> learninglocker: Running provisioner: shell...
    learninglocker: Running: inline script
web11-provision.sh
==> learninglocker: Running provisioner: shell...
    learninglocker: Running: inline script
+++exists+++
==> learninglocker: Running provisioner: shell...
    learninglocker: Running: /var/folders/c6/1yd6r3px03j_l3f4g0kfkjfm0000gn/T/vagrant-shell20141022-35420-17ptv1r
Check for apt packages to install...
httpd,
php,
php-gd,
php-pear,
php-xml,
php-xmlrpc,
php-mbstring,
php-mcrypt,
php-tidy,
curl,
curl-devel,
php-pecl-apc
No yum packages to install.

==> learninglocker: Running provisioner: shell...
    learninglocker: Running: inline script
web21-provision.sh
==> learninglocker: Running provisioner: shell...
    learninglocker: Running: inline script
PROVISION FILE DOES NOT EXIST!
==> learninglocker: Running provisioner: shell...
    learninglocker: Running: inline script
learninglocker-provision.sh
==> learninglocker: Running provisioner: shell...
    learninglocker: Running: inline script
PROVISION FILE DOES NOT EXIST!
==> learninglocker: Running provisioner: shell...
    learninglocker: Running: inline script
!!!thisname!!!! learninglocker
==> learninglocker: Running provisioner: shell...
    learninglocker: Running: inline script
!!!thisip!!!! 192.168.1.899
==> learninglocker: Checking for host entries
==> learninglocker: adding to (/etc/hosts) : 192.168.1.899  learninglocker.vagrant  # VAGRANT: 36d4e82ec2f09084a7e5cd6ebb66c900 (learninglocker) / c338507a-aa8e-41fa-af79-04deb0e124d2

Solution

  • Damn it.

    Always the way. Ask a question and then find the answer.

    I have config.vm.provision a few times in each rather than app_config.vm.provision

    i.e. it should be

    app_servers.each do |key,value|
    
        boxname = value[:name]
        config.vm.provision :shell, inline: 'echo boxname: ' + boxname
    
        config.vm.define boxname do |app_config|
    
            app_config.vm.provision "shell", inline: "echo !!!thisname!!!! #{key}"
            app_config.vm.provision "shell", inline: "echo !!!thisip!!!! #{value[:ip]}"
    
            app_config.vm.box = value[:box]
            app_config.vm.host_name = "%s.vagrant" % value[:name]
    
            app_config.vm.network "private_network", ip: value[:ip]
            app_config.vm.network "forwarded_port", guest: value[:forwarded_port_guest], host: value[:forwarded_port_host]
    
            app_config.ssh.forward_agent = true
    
            provision_filename = key.to_s + "-provision.sh"
            app_config.vm.provision "shell", inline: "echo #{provision_filename}"
    
            # provisioning
            if File.exists?(File.join(vagrant_dir,'provision',boxname + "-provision.sh")) then
                app_config.vm.provision "shell", inline: "echo +++exists+++"
                app_config.vm.provision :shell, :path => File.join( "provision", boxname + "-provision.sh" )
            else
                app_config.vm.provision "shell", inline: "echo PROVISION FILE DOES NOT EXIST!"
            end
    
            # Shared NFS folder
            # app_config.vm.synced_folder "shared/nfs/", "/vagrant/", type: "nfs"
            app_config.vm.synced_folder "shared/nfs/", "/vagrant/"
    
        end # config.vm.define opts[:name] do |config|
    
    end # app_servers.each