Reputation: 4809
I'm wanting to run a separate provisioning block in my Vagrant file on the newly provisioned server. At present when I run this from my CI server
vagrant up
the following blocks are executed successfully
config.vm.provider :linode do |provider, override|
#creates a new instance etc .. the following block runs on this instance
end
config.vm.provision :chef_solo do |chef|
chef.provisioning_path = "/tmp/deploy"
chef.cookbooks_path = ["cookbooks"]
chef.add_recipe = "mydeployagent"
end
now i want to run a separate provisioner afterwards. (a separate task in CI server) i.e.
config.vm.provision :chef_solo do |chef|
chef.provisioning_path = "/tmp/deploy"
chef.cookbooks_path = ["cookbooks"]
chef.add_recipe = "mydeploydatabaseagent"
end
I'm trying to figure out what I need to
run vagrant up so that it only executes the 1st provision block
run vagrant so that it will only run the 2nd provisioner block on the instance that was created in 1.
Thanks in advance
Upvotes: 2
Views: 2357
Reputation: 15784
Chef is about managing a desired state so you should be wanting it to ensure your two recipes are run at each time (and they should be idempotent).
I don't know any way to tell vagrant to use twice the same provisoner with different parameters (I can only think about ugly hacks in the vagrant file for that)
If the second recipe depends on the first one to have been executed then you may add a guard to skip this second recipe until the first one has run.
With chef-solo you may do something like this:
[...Whatever your recipe has to do (agent install, configuration, etc...]
# On a particular resource on your first recipe,
# trigger a notification to make a file which will be used as a guard for second recipe
remote_file "/opt/target/mydeplaoyagent" do
source [.. your source..]
notifies :create,"file[/opt/mydeployagent.initialized]"
end
file "/opt/mydeployagent.initialized" do
action :nothing
end
#start the recipe by a guard to avoid doing anythng if first recipe has not been run
return unless ::File::exists?("/opt/mydeployagent.initialized")
[... your second recipe code here ...]
And in your vagrant file your can add the two recipes to your provisioner like:
config.vm.provision :chef_solo do |chef|
chef.provisioning_path = "/tmp/deploy"
chef.cookbooks_path = ["cookbooks"]
chef.add_recipe = "mydeployagent"
chef.add_recipe = "mydeploydatabaseagent"
end
Obviously the guard can be used on top of the first recipe too if it is not idempotent, but I highly encourage to rethink it to be able to run multiples times, you'll be happy to have it running when you'll have a configuration change to propagate in a file managed by this recipe (and trust me, you will have an update like this to manage one day).
Upvotes: 0
Reputation: 21216
From Vagrant docs.
Vagrant.configure("2") do |config|
# ... other configuration
config.vm.provision "bootstrap", type: "shell" do |s|
s.inline = "echo hello"
end
end
The --provision-with flag can be used if you only want to run a specific provisioner if you have multiple provisioners specified. For example, if you have a shell and Puppet provisioner and only want to run the shell one, you can do vagrant provision --provision-with shell. The arguments to --provision-with can be the provisioner type (such as "shell") or the provisioner name (such as "bootstrap" from above).
Upvotes: 7