What is chef-apply?
One often overlooked tool that is installed with the ChefDK is chef-apply
. This tool basically allows you to execute chef to converge a single recipe on your local machine. There is no Chef server involved. Everything is local.
Note that chef-apply is not a tool for the deployment of production nodes. It's just a quick and easy way to use Chef to configure your local system.
Why is that useful?
One use case for chef-apply is to learn what a recipe does. You can use chef-apply
to run a recipe in a "test only" mode by using the -W
or --why-run parameter. This will converge the recipe, showing you everything that would happen with the recipe without making actual changes to your system.
You can also execute some one-liner scripts. You can easily use chef-apply
in a scripting-like manner to install software. For example, if you need to use Git on your system and you find it's not installed. Just issue the command:
sudo chef-apply -e "package 'git'"
This will check to see if Git is currently installed. If it is, then the command will just inform you that that is the case and exit. If Git is not installed, then it will go about using the correct installer (aka Provider) to obtain and install the package.
Testing this on my Ubuntu workstation, you can see that Git was not installed initially. Then, by using the chef-apply
command, the git
package is installed. Then to confirm that nothing happens if you try to use chef-apply
to install a package that is already installed, I ran the same chef-apply
again to show what happens:
How about creating a "workstation" recipe to set up all the "missing" items from a new ChefDK workstations setup? You create the recipe that defines the state of having your editor installed and integrated with Chef, defines the state of having git
installed, and defines the state of having your custom OS X "tree" script created in /usr/local/bin
. You create this recipe once, and you (and anyone in your organization) can quickly complete their workstation setup by using chef-apply with your workstation recipe.
References
- The link to get the details of an interesting use case for chef-apply posted by Chris Doherty on chef.io is https://www.chef.io/blog/2015/01/16/convert-your-bash-scripts-with-chef-apply/.
- The link to the chef.io documentation on chef-apply is https://docs.chef.io/ctl_chef_apply.html.