7

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.

3 Answers 3

5

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.

5

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
0

This is what worked for my needs:

knife exec -E 'nodes.find("*:*") {|n| printf "%s - %s \n",  n["hostname"] , n["ipaddress"]  }'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.