Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Knife plugin to create data bags from YAML files
#
# Author:: Maciej Pasternacki (<maciej@pasternacki.net>)
# Copyright:: Copyright (c) 2010 Maciej Pasternacki
# License:: Apache License, Version 2.0
#
# 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.
#
require 'chef/knife'
class Chef
class Knife
class DataBagFromYaml < Knife
deps do
require 'chef/knife/data_bag_from_file'
Chef::Knife::DataBagFromFile.load_deps
require 'yaml'
require 'tempfile'
end
banner "knife data bag from yaml BAG FILE (options)"
category "data bag"
def run
databag_from_file = Chef::Knife::DataBagFromFile.new
data_bag, item_path = name_args[0], name_args[1]
data = YAML::load_file(
databag_from_file.loader.find_file("data_bags", data_bag, item_path))
tmpf = Tempfile.new([data_bag, '.json'])
tmpf.puts Chef::JSONCompat.to_json(data)
tmpf.close
databag_from_file.config = config
databag_from_file.name_args = [ data_bag, tmpf.path ]
databag_from_file.run
end
end
end
end
@naphthalene

This comment has been minimized.

Copy link

naphthalene commented Apr 29, 2014

Thanks for writing this! it's beautifully simple and will work nicely with https://github.com/cbitlabs/knife-bs

@developerinlondon

This comment has been minimized.

Copy link

developerinlondon commented Jul 15, 2015

@mpasternacki where do you need to add this file? and do you have some examples of how we can use it?

i wanted to have an ability so i could have my databags created as one yaml file that can map onto all the individual item json files instead of having a file per item, but also to have it so it can also have encrypted values in the yaml.

@developerinlondon

This comment has been minimized.

Copy link

developerinlondon commented Jul 15, 2015

also i get:
NoMethodError: undefined method deps' for #<Class:0x007fb3f41d23d8>::Chef::Knife::DataBagFromYaml ~/chef-nolio/.chef/knife.rb:18:inclass:DataBagFromYaml'
~/chef-nolio/.chef/knife.rb:17:in <class:Knife>' ~f/chef-nolio/.chef/knife.rb:16:inclass:Chef'
~/chef-nolio/.chef/knife.rb:15:in `from_string'
Relevant file content:
17: class DataBagFromYaml < Knife
18: deps do
19: require 'chef/knife/data_bag_from_file'

@predatorian3

This comment has been minimized.

Copy link

predatorian3 commented Nov 10, 2017

Not sure if you figured this out yet @developerinlondon but do you have this file in the correct spot?

yes this should be a Gem, or a Knife Plugin package rather than a file.

@predatorian3

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.