# Knife node list output different to Opscode interface

**URL:** <https://discourse.chef.io/t/knife-node-list-output-different-to-opscode-interface/2930>\
**Category:** Chef Infra (archive)\
**Created:** [July 18, 2012, 8:43pm UTC](https://discourse.chef.io/t/knife-node-list-output-different-to-opscode-interface/2930 "2012-07-18T20:43:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adfasdfasdfasdf](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@adfasdfasdfasdf](https://discourse.chef.io/u/adfasdfasdfasdf)\
**Post date:** [July 18, 2012, 8:43pm UTC](https://discourse.chef.io/t/knife-node-list-output-different-to-opscode-interface/2930/1 "2012-07-18T20:43:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steven\_Danna](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/steven_danna/32/195_2.png) [@Steven\_Danna](https://discourse.chef.io/u/Steven_Danna)\
**Post date:** [July 18, 2012, 10:41pm UTC](https://discourse.chef.io/t/knife-node-list-output-different-to-opscode-interface/2930/2 "2012-07-18T22:41:38Z")

</div>

Hi,

On Wed, Jul 18, 2012 at 1:43 PM, Nick Peirson [nickpeirson@gmail.com](mailto: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

---

<div class="post-metadata">

**Author:** ![adfasdfasdfasdf](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@adfasdfasdfasdf](https://discourse.chef.io/u/adfasdfasdfasdf)\
**Post date:** [July 19, 2012, 9:16am UTC](https://discourse.chef.io/t/knife-node-list-output-different-to-opscode-interface/2930/3 "2012-07-19T09:16:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aaron](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/aaron/32/236_2.png) [@aaron](https://discourse.chef.io/u/aaron)\
**Post date:** [July 19, 2012, 1:48pm UTC](https://discourse.chef.io/t/knife-node-list-output-different-to-opscode-interface/2930/4 "2012-07-19T13:48:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Andrea\_Campi](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/andrea_campi/32/514_2.png) [@Andrea\_Campi](https://discourse.chef.io/u/Andrea_Campi)\
**Post date:** [July 20, 2012, 6:29am UTC](https://discourse.chef.io/t/knife-node-list-output-different-to-opscode-interface/2930/5 "2012-07-20T06:29:31Z")

</div>

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](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 😉

Andrea

---

<div class="post-metadata">

**Author:** ![adfasdfasdfasdf](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@adfasdfasdfasdf](https://discourse.chef.io/u/adfasdfasdfasdf)\
**Post date:** [July 22, 2012, 12:39pm UTC](https://discourse.chef.io/t/knife-node-list-output-different-to-opscode-interface/2930/6 "2012-07-22T12:39:07Z")

</div>

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 [knife node list all · GitHub](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 😉
> 
> Andrea  
> That's ideal, I'll definitely take a further look into knife plugins.

Cheers  
Nick
