Knife node list output different to Opscode interface


#1

Hi All,

When I run ‘knife node list’ I only get a subset of nodes that are
showing in the Opscode web interface. I’m performing the knife command
as the same user as I’m logged into the interface with. I’m trying to
script actions with knife (checking against ec2 instances and deleting
the node if the instance isn’t still live on ec2) and this is making it
tricky, as I can get a full list of nodes back.

I’m doing the same thing with ‘knife client list’ and this works fine,
so it only seems to be nodes that experiences this problem.

Any help to save me manually having to clean nodes from the web
interface would be greatly appreciated!

Thanks
Nick


#2

Hi,

On Wed, Jul 18, 2012 at 1:43 PM, Nick Peirson nickpeirson@gmail.com wrote:

Hi All,

When I run ‘knife node list’ I only get a subset of nodes that are showing
in the Opscode web interface. I’m performing the knife command as the same
user as I’m logged into the interface with. I’m trying to script actions
with knife (checking against ec2 instances and deleting the node if the
instance isn’t still live on ec2) and this is making it tricky, as I can get
a full list of nodes back.

Do you have an environment set in your config file that might be
filtering out some of the results?

I’m doing the same thing with ‘knife client list’ and this works fine, so it
only seems to be nodes that experiences this problem.

This would support the environment hypothesis above.

Any help to save me manually having to clean nodes from the web interface
would be greatly appreciated!

Thanks
Nick

Cheers,

Steven


#3

On 18/07/2012 23:41, Steven Danna wrote:

Do you have an environment set in your config file that might be
filtering out some of the results?
Yup, that was it. Thanks for the quick reply.

Is there anyway to unset that from the command line? Most knife tasks I
want to run with the default environment I’ve specified in my knife.rb,
however I want to override this to operate on all environments in some
cases.

I could list environments and loop through them as a last resort, but
seems arduous and quite slow, as it has to make a few trips to opscode
to get all the nodes from all environments. I suppose separate knife.rb
files that include commonalities might be another solution.

Any thoughts on best practice?

Cheers
Nick


#4

You can write the knife.rb to read the chef environment value from a
shell environment variable. There seems to be a bit of a workaround
needed to make sure the current “if environment is set” logic works:

in knife.rb

unless ENV[‘CHEF_ENV’].empty?
environment ENV[‘CHEF_ENV’]
end

Then you can set it to get a default:

in your shell

export CHEF_ENV=prod
knife node list

(returns prod nodes)

and temporarily unset that with a little-used bash/zsh feature called
environment augmentation:

CHEF_ENV= knife node list

(returns all nodes)

knife node list

(returns prod nodes)


Aaron Peterson

On Thursday, July 19, 2012 at 2:16, Nick Peirson wrote:

On 18/07/2012 23:41, Steven Danna wrote:

Do you have an environment set in your config file that might be
filtering out some of the results?

Yup, that was it. Thanks for the quick reply.

Is there anyway to unset that from the command line? Most knife tasks I
want to run with the default environment I’ve specified in my knife.rb,
however I want to override this to operate on all environments in some
cases.

I could list environments and loop through them as a last resort, but
seems arduous and quite slow, as it has to make a few trips to opscode
to get all the nodes from all environments. I suppose separate knife.rb
files that include commonalities might be another solution.

Any thoughts on best practice?

Cheers
Nick


#5

Another option is to write a knife plugin; in this case it’s extremely trivial.

Drop the content of this gist https://gist.github.com/3149042 in
.chef/plugins/knife/node_list_all.rb
You now have a new option available: “knife node list all” which will
ignore the environment from your config file.
“knife node list” will work exactly as before.

Knife plugins are very powerful, everybody should learn to use them more :wink:

Andrea


#6

On 20/07/2012 07:29, Andrea Campi wrote:

Another option is to write a knife plugin; in this case it’s extremely trivial.

Drop the content of this gist https://gist.github.com/3149042 in
.chef/plugins/knife/node_list_all.rb
You now have a new option available: “knife node list all” which will
ignore the environment from your config file.
“knife node list” will work exactly as before.

Knife plugins are very powerful, everybody should learn to use them more :wink:

Andrea
That’s ideal, I’ll definitely take a further look into knife plugins.

Cheers
Nick