Questions about Policyfiles and cookbook dependencies


I think there are a couple of things about Policyfiles that I'm still not quite grasping, so am hoping somebody can enlighten me. I have Chef Workstation 20.9.136 locally, and am using the Chef Infra version provided by the AWS OpsWorks Automate service.

First, I took this syntax for adding a cookbook to a Policyfile directly from the instructions in Chef Supermarket, however when I try to install or update the policy I get the error shown below.

cookbook 'apache2', '~> 8.5.0', :supermarket
Error: Failed to generate Policyfile.lock
Reason: (Solve::Errors::NoSolutionError) Unable to satisfy the following requirements:

- `apache2 (~> 8.5.0)` required by `user-specified dependency`

Instead, I have to use this syntax, which works.

default_source :supermarket do |s|
  s.preferred_for "apache2"

cookbook 'apache2', '~> 8.5.0'

Should the first syntax I showed work? (Note that I'm just using apache2 as an example; this happens with any cookbook I try to include from the Supermarket.)

Second, I notice that, at least when using test-kitchen, it's not sufficient to have a cookbook, such as apache2, listed in my Policyfile. I also have to have it in my wrapper cookbook's metadata file, or my wrapper cookbook will complain that it can't find resources such as, in our example, apache2_install. It seems redundant to me to have to list the cookbook depencies twice. Is there something I'm not getting here?

I hope somebody can clarify for me (and I hope that I've been clear). I have read and several times but was still surprised by the behaviors detailed above.