1
votes

I am using an cookbook github.com opscode-cookbooks/openldap. I wrote an wrapper cookbook "lab_openldap" that includes "openldap::server" recipe.

The server.rb recipe uses following clausule to upload the PEM file from cookbooks files/ssl/*.pem to server to the location node['openldap']['ssl_cert'].

if node['openldap']['tls_enabled'] && node['openldap']['manage_ssl']
  cookbook_file node['openldap']['ssl_cert'] do
    source "ssl/#{node['openldap']['server']}.pem"
    mode 00644
    owner "root"
    group "root"
  end
end

The PEM is tried to be read from "openldap" cookbook file/ssl/#{node['openldap']['server']}.pem" location.

I have my PEM file in wrapper "lab_openldap" cookbook file/ssl/#{node['openldap']['server']}.pem" location.

Is it possible to modify the "lab_openldap::server.rb" recipe to load PEM from a wrapper cookbook?

Notes: I am aware of https://github.com/bryanwb/chef-rewind but it does not seem to manage this situation.

Update

The provided answer using r.resource is correct.

Actually the issue in the particular code is on "source" keyword that according to http://docs.opscode.com/resource_cookbook_file.html refers to the location of a file in the /files directory in a cookbook located in the chef-repo.

r = resources("cookbook_file[#{node['openldap']['ssl_cert']}]")
r.cookbook('lab_openldap')

cookbook_file node['openldap']['ssl_cert'] do
    source "ssl/#{node['openldap']['server']}.pem"
    mode 00644
    owner "root"
    group "root"
end
2
Please don't forget to mark an answer as correct :) - sethvargo

2 Answers

1
votes

Of course it is! You just need to set the cookbook attribute on the resource when you wrap it. By default, it's "the current cookbook", but you can change it:

r = resources("cookbook_file[#{node['openldap']['ssl_cert']}]")
r.cookbook('my_wrapper_cookbook')

If you look at Bryan's Chef Rewind, you'll see it does the same thing

2
votes

You can do this now in chef directly:

include_recipe "openldap::server" 

edit_resource(:cookbook_file, node['openldap']['ssl_cert']) do
  cookbook cookbook_name
end

Note that to avoid this situation from needing to be used, library cookbooks like openldap should be written as custom resources, rather than as recipes. They should then export properties allowing their templates to be overwritten, using the pattern in this answer:

https://stackoverflow.com/a/63570830/506908