Installing Gems From Your Gemfile When Developing Chef Cookbooks using the ChefDK / Chef Workstation

One of the great things I've found about working with Chef is the ChefDK / Chef Workstation setup, which ensures that you need just one install to be able to get up-and-running in a way that allows you to easily coordinate with others on version of core tools and libraries. This also translates nicely to running it in Docker, but something that's always been painful, and has plagued me for several years, is working with Ruby Gems.

When running your code against an actual node, i.e. using the chef_gem resource works wonderfully, but as someone who strongly believes in test-driven development with Chef, this doesn't solve the local testing issue.

The "best" solution I've got to is to create a Gem that packages the versions of the key Chef dependencies required, and then deal with the poor developer experience that running chef exec bundle exec rspec everywhere. This at least means we're not going to have to manually install gems locally, but has been a pretty poor experience, and makes Chef upgrades that bit harder.

Fortunately today I've found that we can use Bundler to install our Gems globally, which works transparently and allows us to utilise our Gems for tests without causing issues.

Note: I recommend this when you're either running on ephemeral infrastructure, like in a (Docker) container or on a non-persisted build agent, or when using this on your local machine. Please do not use this when you're i.e. using the same Jenkins agent as other people, as it can seriously mess things up!

For instance, when we've got a Gemfile in our repo:

$ chef exec rspec               # fails, as it can't find the Gem(s)
$ bundle config set system true # using `--system` is deprecated
$ bundle install
$ chef exec rspec               # works!

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #chef #ruby #chefdk #chef-workstation.

This post was filed under articles.

Related Posts

Other posts you may be interested in:

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.