Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knife: obtaining two (or more) attributes in one go

I can currently fetch one attribute of a node at a time via knife search node like this:

knife search node "chef_environment:production AND name:i-7a421114" -a cloud.public_hostname
# RESULT:
i-7a421114:
cloud.public_hostname: ec2-104-214-107-198.compute-1.amazonaws.com

knife search node "chef_environment:production AND name:i-7a421114" -a cloud.local_hostname
# RESULT:
i-7a421114:
cloud.local_hostname: ip-10-60-146-201.ec2.internal

I want to retrieve two attributes simultaneously via a single invocation, something like this:

knife search node "chef_environment:production AND name:i-7a421114" -a cloud.public_hostname -a cloud.local_hostname

Of course, this doesn't work, only one attribute is obtained. Since I have almost no chef/knife knowledge, could someone let me know how to do this, or some other way to achieve this.

like image 674
nonbeing Avatar asked May 30 '13 18:05

nonbeing


2 Answers

I started looking at how to write my own knife plugin, but that was overkill. knife exec solved this problem rather elegantly and simply:

knife exec -E 'nodes.find(:name => "i-7a421114") { |n| puts "#{n.cloud.public_hostname} - #{n.cloud.local_hostname}" }'

And it's easy to extend this to as many attributes as required - simply keep adding n.[ATTRIB] to the closure.

like image 130
nonbeing Avatar answered Sep 29 '22 16:09

nonbeing


As of today (Chef: 12.19.36) your second query actually works (and you can add as many attribs as you wish):

knife search node "chef_environment:production AND name:i-7a421114" -a cloud.public_hostname -a cloud.local_hostname
like image 36
AxA Avatar answered Sep 29 '22 14:09

AxA