Skip to content

Instantly share code, notes, and snippets.

@mpasternacki
Created August 19, 2011 17:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpasternacki/1157440 to your computer and use it in GitHub Desktop.
Save mpasternacki/1157440 to your computer and use it in GitHub Desktop.
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
@FilBot3
Copy link

FilBot3 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.

@FilBot3
Copy link

FilBot3 commented Dec 7, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment