How to use policyfiles for local/testing w supermarket cookbooks?

I want to write a blog to show how to develop/test locally with Chef and Vagrant using cookbooks from the Supermarket. So I need an easy way to fetch external cookbooks for local test.

I came across Policyfiles, but this seems to be a server only thing? How would you do this locally with chef zero?

From my research, I found this:

  • Berkshelf can work locally, but the source authors recommend Policyfiles
  • Vagrant had support for Berkshelf with vagrant-berkshelf, but this project was archived after support using Vagrant as a gem was pulled.
  • There was a wrapper for chef-solo and chef-zero, but now gives 404 error: https://github.com/nkadel/nkadel-chef-solo-wrapper
  • There's no equivalent for these, such as vagrant-policyfile or other automation to use external cookbooks in local testing.
1 Like

It is correct that Policyfiles make it easy to reference external cookbooks for local testing with Test Kitchen. It might be easier to walk through with you with the number of questions that you have. Are you available on our Chef Community Slack to have a chat / huddle? If so, find me there.

1 Like

We actually do exactly that, a pretty simple thing to do. If help is needed, we can probably help some..

I was using Vagrant, which automates the chef-client installation and well as provisioning.

Vagrant.configure("2") do |config|
  config.vm.box = "generic/ubuntu2204"
  config.vm.network "forwarded_port", guest: 80, host: 8085

  config.vm.provision "chef_zero" do |chef|
    chef.cookbooks_path = "../cookbooks"
    chef.add_recipe "hello_web"
    chef.nodes_path = "nodes"

    chef.log_level = "debug"
    chef.arguments = "--chef-license accept-silent"
  end
end

This used an internal cookbook, but I will remove that and use the community cookbook for nginx. However, I am not sure how or where to specify this to get this all to work.

The automation with vagrant will create a dna.json with something like this:

{
  "run_list": [
    "recipe[hello_web]"
  ]
}

And a client.rb with this (substitution COOKBOOK_PATH and NODE_PATH for dynamically mounted directories):

cookbook_path ["$COOKBOOK_PATH"]
role_path []
log_level :debug
verbose_logging false
enable_reporting false
encrypted_data_bag_secret nil
data_bag_path []
chef_zero.enabled true
local_mode true
node_path ["$NODE_PATH"]

So I am fuzzy how to fetch nginx from the supermarket and use it here. Where would I put a Policyfile for this to be fetched out.

I can also use a chef_client and point it to a Chef Server, which I can install locally and reference it at chefserver01.local.

As I understand, the Policyfile is used on the server and fetched from there so that it is available locally with chef client, but if I had a minimal chef zero setup, how would I then use Policyfile in this setup, not sure.

Side note. For the above parts, I used this in my blog, and now I want to extend this to not use an internal cookbook, but use an external cookbook.