Vagrant multiple vm with separate provisioning scripts

时间:2014-10-22 17:47:57

标签: vagrant provisioning vagrantfile

目前正在尝试使用单独的配置脚本设置多个VM。

Vagrantfile如下;

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

我还有一个/ web11-provision.sh文件(但没有web21或者学习机等价物),它基本上只打印出一条消息。

问题:当它调出web11时运行一个vagrant它会打印出该文件存在的消息(预期)。当它显示web21时,boxname在第一次出现时仍然是web11,因此运行配置脚本,然后再次进入并更改为web21,因此无法找到该文件。对于learninglocker也是如此 - 运行web11,然后运行web21,然后运行learninglocker。

对于我的.each声明,我显然是愚蠢的,但无法弄清楚它是什么......帮助?

这里是流浪者的全部输出......

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

1 个答案:

答案 0 :(得分:3)

该死的。

总是如此。提出问题,然后找到答案。

我有一些config.vm.provision而不是app_config.vm.provision

即。它应该是

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