Skip to content

Instantly share code, notes, and snippets.

@millisami
Forked from mhorbul/gist:3726422
Created September 16, 2012 09:14
Show Gist options
  • Save millisami/3731707 to your computer and use it in GitHub Desktop.
Save millisami/3731707 to your computer and use it in GitHub Desktop.
set an attribute on the node
# Knife plugin to set an attribute on the node
# Usage:
# $knife node exec [nodename] 'node.set[:somevalue] = true'
class NodeExec < Chef::Knife
banner "knife node exec NODE CODE (options)"
deps do
require 'chef/search/query'
end
def run
if name_args.size == 2
node_name = name_args.first
code = name_args.last
else
ui.fatal "Please provide a node name & code"
exit 1
end
q = Chef::Search::Query.new
q.search('node', "name:#{node_name}") do |node|
if node.run_list.empty?
ui.warn("#{node.name}'s run list is empty.")
exit 1
end
ui.msg("The following piece of code will be executed against node #{node_name}:")
ui.output(code)
ui.confirm("Are you sure?")
ui.output eval(code)
node.save
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment