This cookbook provides a way to materialize a data-structure into a Chef databag, and then easily retrieve it on another node. This can be useful if, for example, you want to amortize the cost of a large search one time, rather than repeat it on a thousand nodes.
- Any platform supported by Chef
- Chef 12.1+
- none
- Make this cookbook a dependency of the cookbook that will use either the read or write functions.
- Call the functions.
- There is no step three.
Lets say you have a cookbook with a big search, to build something like ssh_known_hosts. In a typical Chef cookbook, you might write the following:
data = []
search(:node, 'fqdn:* AND ipaddress:* AND keys_ssh_host_rsa_public:* AND host_dsa_public:*') do |n|
data << "#{n['fqdn']},#{n['ipaddress']} #{n['keys']['ssh']['host_rsa_public']}"
data << "#{n['fqdn']},#{n['ipaddress']} #{n['keys']['ssh']['host_dsa_public']}"
end
data = data.sortTo build up your data for the ssh_known_hosts file. This woudl result in a global search across every node in your infrastructure on every convergence, which, as you get larger, will be pretty brutal.
With this cookbook, you would do the following instead:
materialize('ssh_known_hosts') do
data = []
search(:node, 'fqdn:* AND ipaddress:* AND keys_ssh_host_rsa_public:* AND host_dsa_public:*') do |n|
data << "#{n['fqdn']},#{n['ipaddress']} #{n['keys']['ssh']['host_rsa_public']}"
data << "#{n['fqdn']},#{n['ipaddress']} #{n['keys']['ssh']['host_dsa_public']}"
end
data.sort
endThis would take the output of your search query and store it in a data bag called 'materialize', with the key of 'ssh_known_hosts'. You want to make sure this happens on one node only, rather than on every node. (For example, move it to another recipe, or have a node attribute, or check on node name - whatever. Just don't run it every time.)
To get your value back out, you would do something like this:
begin
ssh_known_hosts_content = retrieve('ssh_known_hosts').join("\n")
rescue
# Protect against empty cache
ssh_known_hosts_content = IO.read('/etc/ssh/ssh_known_hosts')
end
file "/etc/ssh/ssh_known_hosts" do
owner "root"
mode "0644"
content ssh_known_hosts_content
endThis cookbook comes with unit tests!
$ bundle install
$ bundle exec rspecAnd with functional tests!
$ kitchen test defaultAuthor: Cookbook Engineering Team (cookbooks@chef.io)
Copyright: 2011-2016, Chef Software, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.