Image for post
Image for post
ChefDK Ruby integration with RBenv or RVM

ChefDK requires that you use their embedded ruby for running the Chef tools, but you may already have an existing ruby workflow that uses RBenv or RVM.

If the only time you use ruby is when you use Chef tools, there will come a time when you find your Chef code is no longer compatible with a recent release of ChefDK. This has even happened recently with the arrival of Chef 13.

In this later scenario, you will need to manage different versions of ruby to support different versions of Chef. Some of your cookbooks will be pinned or locked to use legacy version of Chef, while others will use a newer version. This is where you’ll need to find a way to integrate a tool such as RVM or RBenv to support switching between ChefDK and another ruby environment that supports the legacy Chef environment.

So there you have it, you have three ruby environments, and this guide briefly walks you through each of these:

  • Use ChefDK’s embedded ruby for everything
  • Integrate ChefDK with rbenv ruby manager.
  • Integrate ChefDK with RVM (ruby version manager)

If you have not done so already, install ChefDK:

On Windows, you can use a tool like Chocolately to fetch ChefDK: choco install -y chefdk.

On Mac OS X, you can use Brew Cask to install ChefDK: brew cask install chefdk.

If you do not use ruby outside of Chef, then just using the embedded ruby is fine. Chef can automate setting up your shell environment:

With RBenv, there’s a ChefDK plug-in that can point back to ChefDK.

The next time you log-in, you can run the following, and test for chef before and after switching to chefdk:

$ rbenv which ruby
$ gem list | grep 'chef\s'
$ rbenv global chefdk
$ rbenv which ruby
$ gem list | grep 'chef\s'
chef (13.8.5)

With RVM, we scissor in symlinks point back to chef’s embedded ruby.

Afterwards, we can select chefdk ruby, or use another ruby. Here’s testing before and after.

$ rvm use
Using /home/vagrant/.rvm/gems/ruby-2.3.6
$ gem list | grep 'chef\s'
$ rvm use chefdk
Using /home/vagrant/.rvm/gems/ext-chefdk-ruby
$ gem list | grep 'chef\s'
chef (13.8.5)

If you need to use ChefDK in your chef repository for all cookbooks, or maybe just a single cookbook, you can use .ruby-version mechanism to lock the version of ruby in that directory.

cd /path/to/your/cookbook
echo chefdk > .ruby-version

Afterward, any time you enter that directory, you’ll automatically be switched into ChefDK.

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store