Please help with node attributes


#1

I used to have the following code:
ssh_this_node = node[‘keys’][‘ssh’][‘host_rsa_public’]
Now i ran foodcritic and it says:
FC039: Node method cannot be accessed with key: cookbooks/ssh/recipes/default.rb:11

Following the example in http://www.foodcritic.io/#FC039, i changed the code to:
ssh_this_node = node.keys[‘ssh’][‘host_rsa_public’]

However, i get the error:
TypeError

no implicit conversion of String into Integer

Cookbook Trace:

/var/chef/cache/cookbooks/ssh/recipes/default.rb:14:in []' /var/chef/cache/cookbooks/ssh/recipes/default.rb:14:infrom_file’

Relevant File Content:

/var/chef/cache/cookbooks/ssh/recipes/default.rb:

14>> ssh_this_node = node.keys[‘ssh’][‘host_rsa_public’]
15: Chef::Log.debug “keys_ssh_host_rsa_public” + ssh_this_node


#2

That doesn’t seem right, the first pass is what you would want, not node.keys - that warning might be erroneous. You would only want to change that if the thing you wanted to call was a method.


#3

When you get the error no implicit conversion of String into Integer in this kind of code, it means that somewhere you have an array which you are trying to access like a hash. You can see this in irb or pry with code like %w{a b c}[0] (this works) %w{ a b c }["oops"] (type error).

So one of node[‘keys’], or node[‘keys’][‘ssh’] is an Array.