Knife search help when an attr is not defined


#1

hi. i was trying to list out my nodes and their AMI IDs, but i have a few
nodes that have no ec2 attribute. they’re probably nodes that failed their
first chef run and need to be cleaned out anyway. when the search runs into
a node that has no ec2 attr, the search bails out before it completes listing
everything:

$ knife exec -E 'nodes.transform(":") {|n| puts “#{n.name}: #{n.ec2.ami_id}” }'
web1.dev.dsnine.com: ami-xxxxxxxx
admin2.stg.dsnine.com: ami-xxxxxxxx
db2.stg.dsnine.com: ami-xxxxxxxx
web4.dev.dsnine.com: ami-xxxxxxxx
app1.stg.dsnine.com: ami-xxxxxxxx
ERROR: ArgumentError: Attribute ec2 is not defined!

is there a way to craft the search so the search completes? i tried checking
if n.ec2.nil?, but that didn’t help:

$ knife exec -E 'nodes.transform(":") {|n| puts “#{n.name}: #{n.ec2.ami_id}” if not n.ec2.nil? }'
web1.dev.dsnine.com: ami-xxxxxxxx
admin2.stg.dsnine.com: ami-xxxxxxxx
db2.stg.dsnine.com: ami-xxxxxxxx
web4.dev.dsnine.com: ami-xxxxxxxx
app1.stg.dsnine.com: ami-xxxxxxxx
ERROR: ArgumentError: Attribute ec2 is not defined!

$ knife exec -E 'nodes.transform(":") {|n| puts “#{n.name}” if n.ec2.nil? }'
ERROR: ArgumentError: Attribute ec2 is not defined!

thanks,
kallen


#2

your search is completing but your code is trying to access a key that
doesn’t exist while it runs over the results. node.transform is meant to
iterate over a set of nodes and change some attributes. I don’t think thats
what you want to be doing here.

you can get the list of nodes with ami_id’s from knife search:

knife search node "ec2:ami_id" -a ec2.ami_id

if you want ones that dont have this attrib you can try doing

 knife search node  "*:*" -a ec2.ami_id

You can see that im using knife search’s -a (attribute) to scope the
results and the default output formatter will display the fqdn.

If you must use knife exec for this you can do it without using transform.

 knife exec -E 'search( :node, "*:*" ).each { |n| puts "#{n.name}:

#{n.ec2.ami_id}" if n.has_key? “ec2” }’

Jesse Nelson

On Wed, Feb 27, 2013 at 11:04 PM, kallen@groknaut.net wrote:

hi. i was trying to list out my nodes and their AMI IDs, but i have a few
nodes that have no ec2 attribute. they’re probably nodes that failed their
first chef run and need to be cleaned out anyway. when the search runs into
a node that has no ec2 attr, the search bails out before it completes
listing
everything:

$ knife exec -E 'nodes.transform(":") {|n| puts “#{n.name}:
#{n.ec2.ami_id}” }'
web1.dev.dsnine.com: ami-xxxxxxxx
admin2.stg.dsnine.com: ami-xxxxxxxx
db2.stg.dsnine.com: ami-xxxxxxxx
web4.dev.dsnine.com: ami-xxxxxxxx
app1.stg.dsnine.com: ami-xxxxxxxx
ERROR: ArgumentError: Attribute ec2 is not defined!

is there a way to craft the search so the search completes? i tried
checking
if n.ec2.nil?, but that didn’t help:

$ knife exec -E 'nodes.transform(":") {|n| puts “#{n.name}:
#{n.ec2.ami_id}” if not n.ec2.nil? }'
web1.dev.dsnine.com: ami-xxxxxxxx
admin2.stg.dsnine.com: ami-xxxxxxxx
db2.stg.dsnine.com: ami-xxxxxxxx
web4.dev.dsnine.com: ami-xxxxxxxx
app1.stg.dsnine.com: ami-xxxxxxxx
ERROR: ArgumentError: Attribute ec2 is not defined!

$ knife exec -E 'nodes.transform(":") {|n| puts “#{n.name}” if
n.ec2.nil? }'
ERROR: ArgumentError: Attribute ec2 is not defined!

thanks,
kallen


#3

and I suppose if you’re trying to get the nodes that have no ec2 data to
also print you can search for those specifically or put the ec2 conditional
in puts:

knife exec -E 'search( :node, “:” ).each { |n| puts " #{n.name}:
#{n.ec2.ami_id if n.has_key? “ec2” } " } ’

tho that looks pretty ugly :slight_smile:

Jesse Nelson

On Wed, Feb 27, 2013 at 11:30 PM, Jesse Nelson spheromak@gmail.com wrote:

your search is completing but your code is trying to access a key that
doesn’t exist while it runs over the results. node.transform is meant to
iterate over a set of nodes and change some attributes. I don’t think thats
what you want to be doing here.

you can get the list of nodes with ami_id’s from knife search:

knife search node "ec2:ami_id" -a ec2.ami_id

if you want ones that dont have this attrib you can try doing

 knife search node  "*:*" -a ec2.ami_id

You can see that im using knife search’s -a (attribute) to scope the
results and the default output formatter will display the fqdn.

If you must use knife exec for this you can do it without using transform.

 knife exec -E 'search( :node, "*:*" ).each { |n| puts "#{n.name}:

#{n.ec2.ami_id}" if n.has_key? “ec2” }’

Jesse Nelson

On Wed, Feb 27, 2013 at 11:04 PM, kallen@groknaut.net wrote:

hi. i was trying to list out my nodes and their AMI IDs, but i have a few
nodes that have no ec2 attribute. they’re probably nodes that failed their
first chef run and need to be cleaned out anyway. when the search runs
into
a node that has no ec2 attr, the search bails out before it completes
listing
everything:

$ knife exec -E 'nodes.transform(":") {|n| puts “#{n.name}:
#{n.ec2.ami_id}” }'
web1.dev.dsnine.com: ami-xxxxxxxx
admin2.stg.dsnine.com: ami-xxxxxxxx
db2.stg.dsnine.com: ami-xxxxxxxx
web4.dev.dsnine.com: ami-xxxxxxxx
app1.stg.dsnine.com: ami-xxxxxxxx
ERROR: ArgumentError: Attribute ec2 is not defined!

is there a way to craft the search so the search completes? i tried
checking
if n.ec2.nil?, but that didn’t help:

$ knife exec -E 'nodes.transform(":") {|n| puts “#{n.name}:
#{n.ec2.ami_id}” if not n.ec2.nil? }'
web1.dev.dsnine.com: ami-xxxxxxxx
admin2.stg.dsnine.com: ami-xxxxxxxx
db2.stg.dsnine.com: ami-xxxxxxxx
web4.dev.dsnine.com: ami-xxxxxxxx
app1.stg.dsnine.com: ami-xxxxxxxx
ERROR: ArgumentError: Attribute ec2 is not defined!

$ knife exec -E 'nodes.transform(":") {|n| puts “#{n.name}” if
n.ec2.nil? }'
ERROR: ArgumentError: Attribute ec2 is not defined!

thanks,
kallen


#4

awesome, thanks. these examples’ll help me grok how to craft more searches.

On Wed, 27 Feb 2013, Jesse Nelson wrote:

and I suppose if you’re trying to get the nodes that have no ec2 data to
also print you can search for those specifically or put the ec2 conditional
in puts:

knife exec -E 'search( :node, “:” ).each { |n| puts " #{n.name}:
#{n.ec2.ami_id if n.has_key? “ec2” } " } ’

tho that looks pretty ugly :slight_smile:

Jesse Nelson

On Wed, Feb 27, 2013 at 11:30 PM, Jesse Nelson spheromak@gmail.com wrote:

your search is completing but your code is trying to access a key that
doesn’t exist while it runs over the results. node.transform is meant to
iterate over a set of nodes and change some attributes. I don’t think thats
what you want to be doing here.

you can get the list of nodes with ami_id’s from knife search:

knife search node "ec2:ami_id" -a ec2.ami_id

if you want ones that dont have this attrib you can try doing

 knife search node  "*:*" -a ec2.ami_id

You can see that im using knife search’s -a (attribute) to scope the
results and the default output formatter will display the fqdn.

If you must use knife exec for this you can do it without using transform.

 knife exec -E 'search( :node, "*:*" ).each { |n| puts "#{n.name}:

#{n.ec2.ami_id}" if n.has_key? “ec2” }’

Jesse Nelson

On Wed, Feb 27, 2013 at 11:04 PM, kallen@groknaut.net wrote:

hi. i was trying to list out my nodes and their AMI IDs, but i have a few
nodes that have no ec2 attribute. they’re probably nodes that failed their
first chef run and need to be cleaned out anyway. when the search runs
into
a node that has no ec2 attr, the search bails out before it completes
listing
everything:

$ knife exec -E 'nodes.transform(":") {|n| puts “#{n.name}:
#{n.ec2.ami_id}” }'
web1.dev.dsnine.com: ami-xxxxxxxx
admin2.stg.dsnine.com: ami-xxxxxxxx
db2.stg.dsnine.com: ami-xxxxxxxx
web4.dev.dsnine.com: ami-xxxxxxxx
app1.stg.dsnine.com: ami-xxxxxxxx
ERROR: ArgumentError: Attribute ec2 is not defined!

is there a way to craft the search so the search completes? i tried
checking
if n.ec2.nil?, but that didn’t help:

$ knife exec -E 'nodes.transform(":") {|n| puts “#{n.name}:
#{n.ec2.ami_id}” if not n.ec2.nil? }'
web1.dev.dsnine.com: ami-xxxxxxxx
admin2.stg.dsnine.com: ami-xxxxxxxx
db2.stg.dsnine.com: ami-xxxxxxxx
web4.dev.dsnine.com: ami-xxxxxxxx
app1.stg.dsnine.com: ami-xxxxxxxx
ERROR: ArgumentError: Attribute ec2 is not defined!

$ knife exec -E 'nodes.transform(":") {|n| puts “#{n.name}” if
n.ec2.nil? }'
ERROR: ArgumentError: Attribute ec2 is not defined!

thanks,
kallen