I'm digging into what it will take to migrate my development environments to Vagrant and I'm having some trouble getting a handle on the VM provisioning process with chef. I've never used chef before and the Vagrant docs in this area intentionally weak (out of scope) so I could use a hand.
I'd like to make this as portable as possible, so I thought it made sense to load cookbooks from a URL so this is what I'm trying in my Vagrantfile
:
config.vm.provision :chef_solo do |chef| chef.recipe_url = 'https://github.com/opscode/cookbooks/tarball/master' chef.add_recipe 'nginx' chef.add_recipe 'mysql' chef.add_role 'web' # You may also specify custom JSON attributes: # chef.json = { :mysql_password => '' } end
I don't think that there's any question that I'm simply misunderstanding something, but I haven't found a documentation source that seems like it fits this Vagrant context.
Thanks.
Provisioners in Vagrant allow you to automatically install software, alter configurations, and more on the machine as part of the vagrant up process. This is useful since boxes typically are not built perfectly for your use case.
The Vagrant Chef Solo provisioner allows you to provision the guest using Chef, specifically with Chef Solo. Chef Solo is ideal for people who are already experienced with Chef, already have Chef cookbooks, or are looking to learn Chef.
Chef Zero is a simple, easy-install, in-memory Chef server that can be useful for Chef Client testing and chef-solo-like tasks that require a full Chef Server. It IS intended to be simple, Chef 11+ compliant, easy to run and fast to start. It is NOT intended to be secure, scalable, performant or persistent.
The way you show above will certainly work fine.
However, if you're going to use Chef solo, I recommend embedding the cookbooks directly within the repository that goes into version control. This makes it so that at every point in time when you commit, the Vagrantfile is aligned with the proper versions of cookbooks. So in 2 years when you check out code from today, the cookbooks should still technically work.
If you want to go this route, create a directory, such as cookbooks
, which contains the cookbooks. Then configure Vagrant like so:
config.vm.provision :chef_solo do |chef| chef.cookbooks_path = "cookbooks" chef.add_recipe 'nginx' chef.add_recipe 'mysql' chef.add_role 'web' # You may also specify custom JSON attributes: # chef.json = { :mysql_password => '' } end
Unfortunately, there isn't a lot of documentation on Vagrant in combination with Chef. I recommend having a look into using librarian to manage your cookbooks for you. I typically have a block similar to this in my Vagrantfile:
config.vm.provision :chef_solo do |chef| chef.cookbooks_path = "chef/cookbooks" chef.roles_path = "chef/roles" # ... end
I create a new directory in the root of my project called chef
and add the following lines to my .gitignore
:
# Ignore librarian bundled cookbooks and cache /chef/cookbooks /chef/tmp
In the chef
directory, you can run librarian-chef init
and begin adding cookbooks to the created Cheffile
in a way you would with a Gemfile:
cookbook "apt", :git => "git://github.com/fnichol/chef-apt.git", :ref => "cacher-client-solo-support" cookbook "imagemagick", "0.2.2" cookbook "mongodb", :git => "git://github.com/edelight/chef-cookbooks.git", :ref => "0.11.0"
You get the benefits of being able to specify git repositories, file paths, and versions to bundle which is extremely helpful when you're using cookbooks from multiple sources. When you run librarian-chef install
librarian will fetch all of the specified cookbooks and store them in the cookbooks
directory, relative to the location of your Cheffile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With