Testing chef cookbooks with Vagrant - vagrant

I want to test my cookbooks on different environments using Vagrant.
As I've found, there are chef-client and chef-solo provisioners, used to respectively download cookbooks from chef-server and to serve cookbooks locally.
I want to provide Vagrantifle inside each cookbook in order to provide way to test it for all knife developers. If cookbook was standalone, I'd just use chef-solo provisioner, but what if it has (multiple) dependencies of cookbooks stored on chef-server?
What's the best idea to resolve this problem:
Download all cookbooks locally and copy it to Vagrant
Push only tested cookbook and download rest of them from chef-server
What are advantages and disadvantages? How should I test my cookbooks on different systems?

This is indeed what Test Kitchen is for. It has a driver for Vagrant, or you can use almost any other VM/cloud system imaginable via another driver plugin. It integrates with Berkshelf and the Policyfile system to handle dependency management, so you would use one of those (which hopefully you already are).

Related

Unable to configure non-latest chef-dk install

Our organization has not upgraded to Chef 13 or 14, so we have to pin all our cookbooks to version 12. This means pinning to chef-dk version 1.6.11.
I'm spinning a centos7 vm in Vagrant with a cookbook and have set the version, but it will only install the Latest of chefdk, which results in the machine getting Chef 14. I've added a dependency in metadata.rb of chef_version ~> 12, so the provision fails, as Chef 14 is installed but the cookbook demands 12.
I should mention that the VM is for cookbook dev, so i want the right version of chef on it.
What am i missing to get the right version installed?
Thanks.
recipes/default.rb:
node.default['chef_dk']['version'] = '1.6.11'
node.default['chef_dk']['global_shell_init'] = true
include_recipe 'chef-dk'
metadata.rb:
depends 'chef-dk'
chef_version '~> 12.0'
berksfile:
cookbook 'chef-dk'
The part that is failing is the "outer" Chef, the thing running the recipe, not the ChefDK install (it never gets that far). We don't generally recommend using Chef to install ChefDK because installing both the chef-client and ChefDK installers on the same machine can lead to confusion as there are overlapping command line tools. I would provision the dev VM using a simpler system, probably a bash script or similar. We also do provide chef/chefdk Docker images on Hub for this kind of thing. (also we don't recommend doing cookbook development inside a VM at all, but I would guess that ship has sailed for you)

Chef-client run half copying cookbooks from site-cookbooks directory into cookbooks directory

I've got a custom cookbook that configures my Jenkins servers. There are 3 recipes in a custom cookbook. I'm using librarian-chef to manage my cookbook dependencies. Chef is being run in local mode and I use terraform to provision my nodes with Chef.
When I run chef-client -z it tries to copy my custom cookbook in the site-cookbooks directory into the cookbooks directory. The reason I say "tries" is because it only copies one of the recipes over, it doens't copy the other recipes or other cookbook files like metadata.rb. Ultimately I get errors on including the java cookbook in my recipe because it can't resolve that cookbooks dependencies.
If I manually log into the node and copy the directory over myself, everything runs perfectly.
This bug is resolved in latest stable version of chef. You can try that

How to setup test enviroment using the same vagrant provisioner

Currrently, we are setting up development environment using Vagrant. All software (php, mysql, apache, memcached..) are installed using Vagrant Chef solo provisioner.
Now we want to create a test environment using a physical machine (it has same OS to the Vagrant virtual machine).
I don't want to install all required softwares on the test server manually, instead of that I want to setup test server automatically using same Vagrant provisioner. Is it posible and how can I do that?
You can use/run chef-client in local mode on that server see https://docs.chef.io/ctl_chef_client.html#run-in-local-mode
Chef solo https://docs.chef.io/chef_solo.html is also an option but I think chef-client local mode is preferred now.
Another option is to create a free hosted Chef account, upload your cookbooks to hosted Chef, bootstrap your server and run the chef-client.
The best approach I think is to use an actual Chef server. Use hosted Chef or setup your own Chef server. Assuming you will want to also easily update the configuration of the test server as you make changes to your recipes, using an actual Chef server is the best approach.

chef workstation setup softwares required

I am trying to execute chef commands and build the server for that I did below setup on MacOS but observed that I need to have vagrant and virtualbox but I am not sure if I need to install it on host macOS machine or Virtual machine. Could you please suggest on this.
Setup which I did so far is as below:
On Mac OS I have installed VirtualBox and created a base ubuntu Virtual Machine
Then Inside virtual machine having ubuntu as OS installed ChefDk, Hosted chef server setup, knife and git
created chef-repo repository
In order to complete setup, Do I need to install vagrant and one more instance of Virtualbox inside virtual machine on ubuntu OS and do I need any additional installation?
Install ChefDK and Vagrant as well as VirtualBox on your workstation. This allows you to use any editor/IDE to edit cookbooks and then provision a VM using Vagrant and Virtualbox (or other supported hypervisors).
In general, I would recommend to follow Learn Chef.
Honestly, the question is a little confusion on your objective. But from a high level, i can assume that you are trying to get started with chef using the hosted chef server. First i agree with #StephenKing pointing you to the learn chef site. That's a good starting point. But here are some things that should clear up your question:
Typically there will be a development workstation. In your case its the MacBook. This is where you will install chefDK and write your fancy cookbooks. It should have the chef-repo, knife.rb etc to talk with the chef server.
Once your initial cookbook writting is over, you upload the cookbook and other params like environment, data_bags, roles etc to the chef server (in the chef-org)
Its here you need a node to apply your cookbook. As a starter, use some nodes from a VPS that's far more easier rather then trying to use vagrant nodes or something. As the saying goes "one step at a time". Do not try to learn chef and vagrant or even more (which is what you will encounter with chef) at the same time.
Once you have the node, bootstrap the node to your chef-org where the cookbooks were uploaded. Then run chef-client on the node.
And you are on your way to the chef world.

Chef: Upload and re-apply a cookbook to a Node

This is kind of a n00b question, but the Chef docs don't help.
Say I changed the code of a cookbook. I upload it to my chef server. Ho do I apply the changes to my nodes using this cookbook?
As Draco commented, you run Chef on your node(s). They will automatically download the changed files in the cookbook from the server.
sudo chef-client
If you're running Chef as a service (setup via Opscode's chef-client cookbook service recipe or otherwise), it will automatically run at some point. Since you're using a Chef Server, you can also use knife ssh to perform a search and run the command on multiple nodes at one time.
knife ssh "*:*" "sudo chef-client" -x youruser
Replace ":" with any Chef Search Query.
Also, this is where Environments are useful, in that you can pin particular versions of cookbooks to nodes in an environment, so they don't get the changes that were made in the cookbook until you modify the version they're allowed to use. For example, if you have version 1.0.0 of your cookbook, and pin "production" to that ("= 1.0.0"), then increment the version (1.0.1) before uploading, then production nodes won't get the new version until the version in the environment is updated to the newer version.

Resources