Vagrantfile: loop around shell

308 views
Skip to first unread message

Kevin Kruzich

unread,
Jan 4, 2018, 4:46:12 PM1/4/18
to Vagrant

I have the Vagrantfile below. It may look funny but it gets the job done. I'm concerned about repeated runs of the provisioner. 

For the first host, foo02, this will print once. For bar02, it prints twice. For omg02, it prints thrice... and so forth

==> omg02: Running provisioner: shell...
    omg02
: Running: inline script
[ ... ]


I realize this is probably due to the placement of 'config.vm.provision' but after moving it around a few places, the Vagrantfile either won't validate or there's some subsequent error. 

Can you help me understand the logic here and how the structure might be improved. 


BOX_IMAGE = "centos7-2018010301"
ENV
["BRIDGE"] = "eno1"


systems
= {
       
'foo02' => '',
       
'bar02' => '',
       
'omg02' => '',
       
}


Vagrant.configure("2") do |config|
 systems
.each do |i,x|
    config
.vm.define "#{i}" do |subconfig|
      subconfig
.vm.box = BOX_IMAGE
      subconfig
.ssh.username = "vagrant"
      subconfig
.ssh.password = "lulz"
      subconfig
.vm.hostname = "#{i}"
      subconfig
.vm.network :public_network, auto_config: false, bridge: "#{ENV['BRIDGE']}"
        config
.vm.provision "shell", inline: <<-SHELL
         
/opt/puppetlabs/bin/puppet resource service puppet ensure=running enable=true
        SHELL
       config
.vm.provider "virtualbox" do |v|
         v
.name = "#{i}"
         v
.customize ["modifyvm", :id, "--paravirtprovider", "kvm"]
         v
.customize ["modifyvm", :id, "--autostart-enabled", "on"]
       
end
   
end
 
end
end

Alvaro Miranda Aguilera

unread,
Jan 5, 2018, 7:45:36 AM1/5/18
to vagra...@googlegroups.com
Hello!

yes its confusing but not hard.


This is what your provision did:


==> omg02: Running provisioner: shell...

    omg02: Running: inline script

    omg02: hi

==> omg02: Running provisioner: shell...

    omg02: Running: inline script

    omg02: hi

==> omg02: Running provisioner: shell...

    omg02: Running: inline script

    omg02: hi


The reason is because vagrant is read top to bottom , outside to inside.

So you got a config 3 times.
The fix is to rename it to subconfig like all in that block.

So, I did update a bit and now:


==> foo02: Running provisioner: shell...

    foo02: Running: inline script

    foo02: stdin: is not a tty

    foo02: hi

==> bar02: Running provisioner: shell...

    bar02: Running: inline script

    bar02: stdin: is not a tty

    bar02: hi

==> omg02: Running provisioner: shell...

    omg02: Running: inline script

    omg02: stdin: is not a tty

    omg02: hi


What I did change:

            subconfig.vm.provision "shell", inline: <<-SHELL
                echo hi
                #/opt/puppetlabs/bin/puppet resource service puppet ensure=running enable=true
            SHELL

same for virtualbox if you want to apply all that to every subconfig

there is one nice thing is you can create a variable at define level like this:
config.vm.define vm_name = "#{i}" do |subconfig|

and then you can use vm_name in that code

the whole thing now:

#BOX_IMAGE = "centos7-2018010301"
BOX_IMAGE = "hashicorp/precise64"

#ENV["BRIDGE"] = "eno1"
ENV["BRIDGE"] = "en0: Wi-Fi (AirPort)"

systems = {
        'foo02' => '',
        'bar02' => '',
        'omg02' => '',
        }

Vagrant.configure("2") do |config|
    systems.each do |i,x|
        config.vm.define vm_name = "#{i}" do |subconfig|
            subconfig.vm.box = BOX_IMAGE
            subconfig.ssh.username = "vagrant"
            #subconfig.ssh.password = "lulz"
            subconfig.vm.hostname = "#{i}"
            subconfig.vm.network :public_network, auto_config: false, bridge: "#{ENV['BRIDGE']}"
            subconfig.vm.provision "shell", inline: <<-SHELL
                echo hi from #{vm_name}
                #/opt/puppetlabs/bin/puppet resource service puppet ensure=running enable=true
            SHELL
            subconfig.vm.provider "virtualbox" do |v|
                v.name = vm_name
                v.customize ["modifyvm", :id, "--paravirtprovider", "kvm"]
                v.customize ["modifyvm", :id, "--autostart-enabled", "on"]
            end
        end
    end
end


Enjoy!
Alvaro


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/13ef6f00-01de-402b-8512-b32650c12778%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Reply all
Reply to author
Forward
0 new messages