Create a recipe bag for a chef

How to create a data packet from a recipe and avoid an exception when this data packet already exists?

The documentation shows that the creation of a data packet is as follows:

new_databag = Chef::DataBag.new new_databag.name('unique_name') new_databag.save 

This works when the database does not exist yet, but how to make it work if the database already exists so that it does not interrupt the work of the chef?

+6
source share
1 answer

Try using the list method for Chef :: DataBag and see if your database name is present:

 require 'chef/data_bag' unless Chef::DataBag.list.key?('unique_name') new_databag = Chef::DataBag.new new_databag.name('unique_name') new_databag.save end 

I use this to make my recipes more reliable or cause a friendlier error if the expected data file cannot be found on the Chef server.

+12
source

Source: https://habr.com/ru/post/920511/


All Articles