Provisioning scripts always run twice?

265 views
Skip to first unread message

Paul Hoffman

unread,
Jun 6, 2017, 6:02:50 PM6/6/17
to vagra...@googlegroups.com
Greetings. I suspect I'm missing something obvious, but "vagrant up --provision" or "vagrant provision" cause my script to run twice. I write the script out each time I run either of these, but I would hope that wouldn't be the cause of the problem.

Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "main"
  config.vm.provision "shell", path: "run_at_start.sh"
end


run_at_start.sh

# Instructions for unbound-1.6.2
[ -e /res_binaries/unbound-1.6.2 ] && echo "The binary for unbound-1.6.2 already exists." && exit
. . . other shell steps here . . .

Adding "#!/usr/bin/env bash" to the top of run_at_start.sh didn't help.

Clues appreciated!

Alvaro Miranda Aguilera

unread,
Jun 7, 2017, 3:30:17 AM6/7/17
to vagra...@googlegroups.com

On Wed, Jun 7, 2017 at 12:02 AM, Paul Hoffman <paul.h...@gmail.com> wrote:
Vagrant.configure("2") do |config|
  config.vm.box = "main"
  config.vm.provision "shell", path: "run_at_start.sh"
end

Hello

It shouldn't.

So I will suggest.

1. check with a different box, ie hashicorp/precise64

2. The way Vagrant works, you have 3 Vagrantfiles (if present) and they get merge in memory.

So check if there is a Vagrantfile at:

~/.vagrant.d/Vagrantfile  < this is a global one that will be loaded into all the commands

~/.vagrant.d/boxes/<thebox>/<somepath>/Vagrantfile < this will be used in all the VMs that use this base box.

Also you can test with:

#   config.vm.provision "shell", path: "run_at_start.sh"
  config.vm.provision "shell", inline: " echo we run run_at_start.sh"

That will display how many runs will happen. 

If this display only one time, then maybe the script is calling himself?

There aren't many options.




--
Alvaro

Paul Hoffman

unread,
Jun 9, 2017, 2:12:07 PM6/9/17
to vagra...@googlegroups.com
Sorry for the long delay in getting back to you on this. I've been bashing my head against the desk and just now figured it out.

The box has a Vagrantfile. That file has 'config.vm.provision "shell', path: "run_at_start.sh"" in it. The Vagrant machine *also* has the same Vagrantfile. Because of the way that Vagrant pulls in the box's Vagrantfile after parsing its own, it now has two (identical) 'config.vm.provision "shell' lines and it decides that I meant to run it twice.

I could consider this a bug, but I can also see that "having two identical shell lines means run the command twice" is logical if you squint hard.

I have a solution for my use case: remove the shell line from the machine's Vagrantfile. Hopefully this doesn't burn other people or, if it does, that they can find this thread.

--Paul Hoffman

--
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/CAHqq0ezUrhwB7bQtOcdVRjP4Bu2FW2KWVdq%2BvwD3GHXqz1_%2BPg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Brian Cain

unread,
Jun 13, 2017, 7:33:26 PM6/13/17
to vagra...@googlegroups.com
Hello Paul!

You are correct. Because the box has the same Vagrantfile, it gets loaded twice when you run Vagrant (see https://www.vagrantup.com/docs/vagrantfile/#load-order-and-merging)

I've been working this week to fix the bug in Vagrant, and I've put up a pull request which will be included in the next release. However, there is a workaround if you can't wait. The bug itself is due to your provision block not having a name. If you don't want them running twice, you can fix it by giving it a name like this:

`config.vm.provision "my shell script", type: "shell", ....`

Then the script should only run once. Hopefully that helps!

Cheers.
- Brian Cain


For more options, visit https://groups.google.com/d/optout.



--
Brian Cain

Paul Hoffman

unread,
Jun 13, 2017, 7:41:19 PM6/13/17
to vagra...@googlegroups.com
Thanks for the followup. After staring at my setup a while, I realized that it was easy to have different Vagrantfiles in my box and in my images. That fixed it. :-)
Reply all
Reply to author
Forward
0 new messages