Instantly share code, notes, and snippets.

What would you like to do?
A Knife plugin to set node environment
## Knife plugin to set node environment
# See
## Install
# Place in .chef/plugins/knife/set_environment.rb
## Usage
# Nick-Stielaus-MacBook-Pro:chef-repo nstielau$ knife node set_environment my_env
# Looking for
# Setting environment to my_env
# 1 items found
# Node Name:
# Environment: my_env
# IP:
# Run List: role[base], role[web_app]
# Roles: base, web_app
# Recipes timezone::default, hosts::default, sudo::default, web_app::default
# Platform: ubuntu 10.04
require 'chef/knife'
module SomeNamespace
class NodeSetEnvironment < Chef::Knife
deps do
require 'chef/search/query'
require 'chef/knife/search'
banner "knife node set_environment NODE ENVIRONMENT"
def run
unless @node_name = name_args[1]
ui.error "You need to specify a node"
exit 1
unless @environment = name_args[2]
ui.error "You need to specify an environment"
exit 1
puts "Looking for #{@node_name}"
searcher =
result =, "name:#{@node_name}")
knife_search =
node = result.first.first
if node.nil?
puts "Could not find a node named #{@node_name}"
exit 1
puts "Setting environment to #{@environment}"
knife_search =
knife_search.name_args = ['node', "name:#{@node_name}"]

This comment has been minimized.

mmzoo commented Sep 28, 2011

What I don't understand is: when I run just "knife node" without your plugin, I get the help overview. With the plugin in place, I get "You need to specify a node". How can the plugin break the default behavior of knife? Should the plugin not react once the "set_environment" argument is given?


This comment has been minimized.


nstielau commented Sep 28, 2011

It is a little magic how some of this works, but I think that was because the class name was 'Node'. Changing it should fix the issue.


This comment has been minimized.

eherot commented Dec 20, 2011

Not sure if it's something to do with my ruby version but I had to change name_args[1] and name_args[2] to name_args[0] and name_args[1] for this to work. Before I made this change, I would specify machine and environment and it would tell me:

ERROR: You need to specify an environment

$ knife -v
Chef: 0.10.6

$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]


This comment has been minimized.

miketheman commented Dec 29, 2011

@eherot I did the same, and that's what worked. Here:


This comment has been minimized.

ghost commented Aug 16, 2012

EDITOR="sed -e 's/_default/Production/' -i " knife node edit $(hostname)


This comment has been minimized.

simonmcc commented Apr 3, 2013

Just incase you're running knife from OSX (I kept getting sed -i stdin errors):

EDITOR="perl -p -i -e 's/_default/your-env/'" knife node create $(HOSTNAME)


This comment has been minimized.

paustin01 commented Apr 4, 2013

+1 on moving the name_args[] to 0 and 1


This comment has been minimized.

pconerly commented Aug 13, 2014

+1 name_args[0] and name_args[1]. It's too bad we can't edit this.


This comment has been minimized.

pconerly commented Aug 13, 2014

There's a bug with knife_search? I filed a ticket here: chef/chef#1822


This comment has been minimized.

sacwin commented Sep 25, 2014

I think, there are few changes required in the script

unless @node_name = name_args[1] -> unless @node_name = name_args[0]

unless @node_name = name_args[2] -> unless @node_name = name_args[1]

node = result.first.first -> node = result.first[0]

Finally, Instead of the following group of lines:
knife_search =
knife_search.name_args = ['node', "name:#{@node_name}"]

have these instead:
puts node.chef_environment

It Works perfectly now!!!

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