Skip to content

Instantly share code, notes, and snippets.

@zzondlo
Forked from scottsbaldwin/version_set.rb
Created April 4, 2012 04:38
Show Gist options
  • Select an option

  • Save zzondlo/2297837 to your computer and use it in GitHub Desktop.

Select an option

Save zzondlo/2297837 to your computer and use it in GitHub Desktop.
knife plugin to set data bag attribute
# This knife plugin provides the capability to set an attribute
# value in a data bag item. By default the 'versions' data bag
# is used, but that can be overridden with the -b option.
# Copy this file to ~/.chef/plugins/knife to install the plugin.
module KnifePlugins
class DataBagVersionSet < Chef::Knife
deps do
require 'chef/data_bag'
end
banner "knife data bag version set ITEM ATTRIBUTE VALUE"
category 'data bag'
option :bag, :short => '-b', :long => '--bag', :description => 'The name of the versions data bag, default is: versions'
def run
if @name_args.length < 3
show_usage
ui.fatal("Please specify all arguments")
exit 1
end
@data_bag_item, @attribute, @value = @name_args
if config[:bag]
@data_bag_name = config[:bag]
else
@data_bag_name = 'versions'
end
if @data_bag_name.nil?
@data_bag_name = 'versions'
end
bag_item = Chef::DataBagItem.load(@data_bag_name, @data_bag_item)
bag_item[@attribute] = @value
bag_item.save
puts "#{@data_bag_name}/#{@data_bag_item}/#{@attribute} saved."
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment