Search code examples
chef-infraknife

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.


Solution

  • 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.