[chef] Re: Using variables to define attributes?


Chronological Thread 
  • From: Noah Kantrowitz < >
  • To:
  • Subject: [chef] Re: Using variables to define attributes?
  • Date: Mon, 6 Jul 2015 17:52:42 -0700

Yes they are case sensitive, though Chef is just returning the hostname as 
reported by the OS. node['hostname'].downcase might be what you want.

--Noah

On Jul 6, 2015, at 5:49 PM, Dan-Joe Lopez 
< >
 wrote:

> I think I am starting to get it…, and yes, now I see the output as you 
> suggested.  Are the attributes this case sensitive?  node[‘hostname'] 
> returns allcaps.  does ruby have something like var.tolower()?
> 
> thanks,
> Dan-Joe
> Sent from Windows Mail
> 
> From: Noah Kantrowitz
> Sent: ‎Monday‎, ‎July‎ ‎6‎, ‎2015 ‎5‎:‎46‎ ‎PM
> To: 
> 
> 
> Above that error, you should still see the value of var. Confirm that is 
> what you expect it to be. The error likely means one of the levels of keys 
> doesn't exist. Ruby returns nil on accessing a key that doesn't exist, so 
> figure out which key that is and you should be able to correct it.
> 
> --Noah
> 
> On Jul 6, 2015, at 5:42 PM, Dan-Joe Lopez 
> < >
>  wrote:
> 
> > I had a similar idea with the logging. The error I am getting seems to be 
> > a "compile” error though, so it doesn't show my 1st logging data…
> > 
> > 
> > 
> > #DEBUG ATTRIBURE ACCESS
> > var = node['hostname']
> > Chef::Log.info var
> > Chef::Log.info <<-EOH 
> > \n
> > !!!!!!!!!!!!!!!!!!!!!!!!!!!DEBUG ec2-ATTRIBURE 
> > ACCESS!!!!!!!!!!!!!!!!!!!!!!!!!!!
> > --ec2.instances.ec2-8a4b54410.tags.ec2:owner: 
> > #{node[ec2]['instances']['mo-8a4b54410']['tags'][ec2:owner']}
> > --ec2:owner: #{node['ec2:owner']}
> > --hostname: #{node['hostname']}
> > --ec2.instances.[hostname].tags.ec2:owner: 
> > #{node[ec2]['instances'][var]['tags'][ec2:owner']}
> > !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> > \n
> > EOH
> > 
> > and the error that I get is:
> > 
> > 
> > ================================================================================
> > Recipe Compile Error in 
> > C:/ec2/repos/devops_automation/kxen_ga/chef/site-cookbooks/debug/recipes/default.rb
> > ================================================================================
> > 
> > NoMethodError
> > -------------
> > undefined method `[]' for nil:NilClass
> > 
> > Cookbook Trace:
> > ---------------
> > C:/ec2/repos/devops_automation/kxen_ga/chef/site-cookbooks/debug/recipes/default.rb:10:in
> >  `from_file'
> > 
> > Relevant File Content:
> > ----------------------
> > C:/ec2/repos/devops_automation/kxen_ga/chef/site-cookbooks/debug/recipes/default.rb:
> > 
> > 3: Chef::Log.info var
> > 4: Chef::Log.info <<-EOH 
> > 5: \n
> > 6: !!!!!!!!!!!!!!!!!!!!!!!!!!!DEBUG ec2-ATTRIBURE 
> > ACCESS!!!!!!!!!!!!!!!!!!!!!!!!!!!
> > 7: --ec2.instances.ec2-8a4b54410.tags.ec2:owner: 
> > #{node['ec2']['instances']['mo-8a4b54410']['tags']['ec2:owner']}
> > 8: --ec2:owner: #{node['ec2:owner']}
> > 9: --hostname: #{node['hostname']}
> > 10>> --ec2.instances.[hostname].tags.ec2:owner: 
> > #{node['ec2']['instances'][var]['tags']['ec2:owner']}
> > 11: 
> > !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> > 12: \n
> > 13: EOH
> > 
> > Does this help at all?
> > ​
> > 
> > Sent from Windows Mail
> > 
> > From: Noah Kantrowitz
> > Sent: ‎Monday‎, ‎July‎ ‎6‎, ‎2015 ‎5‎:‎30‎ ‎PM
> > To: 
> > 
> > 
> > If var == 'myhappyhost', those two are equivalent. Most likely some 
> > intermediary value is not what you expect. Add some logging to your 
> > recipe code:
> > 
> > var = node['hostname']
> > Chef::Log.info("var = #{var.inspect}")
> > node['ec2']['instances'][var]....
> > 
> > Don't forget to add `-l info` to the client command line too so it will 
> > display the logging output.
> > 
> > --Noah
> > 
> > 
> > On Jul 6, 2015, at 5:19 PM, Dan-Joe Lopez 
> > < >
> >  wrote:
> > 
> > > Thanks for the quick reply Noah.  Unfortunately I don’t have a solid 
> > > answer to your question.  I am working within an infrastructure that I 
> > > did not create.  I am not sure what information that I can share with 
> > > you beyond the below that you might find helpful.
> > > 
> > > As far as access to other instances information; we have collections of 
> > > instances organized into projects.  Each project contains a bunch of 
> > > attributes from all of its instances, so that any instance can see them.
> > > 
> > > As far as the data not being stored correctly, would that still be a 
> > > concern, given that manually calling the attribute works as expected 
> > > when call like this:
> > > node[‘ec2’][‘instances’][‘myhappyhost’][‘ip’]
> > > but fails when using the a var in place of the actual hostname:
> > > node[‘ec2’][‘instances’][var][‘ip’]
> > > ?
> > > 
> > > Thanks,
> > > Dan-Joe Lopez
> > > Sent from Windows Mail
> > > 
> > > From: Noah Kantrowitz
> > > Sent: ‎Monday‎, ‎July‎ ‎6‎, ‎2015 ‎5‎:‎03‎ ‎PM
> > > To: 
> > > 
> > > 
> > > 
> > > On Jul 6, 2015, at 4:53 PM, Dan-Joe Lopez 
> > > < >
> > >  wrote:
> > > 
> > > > OHAI Chefs!
> > > > 
> > > > I don’t know if this is even possible, but I am starting to feel like 
> > > > it is not ☹ Also, I am pretty green, so my advanced apologies if this 
> > > > is a silly question.
> > > > 
> > > > I would like to call a specific attribute by using a variable, or 
> > > > another attribute.  I cannot find any documentation on the subject, 
> > > > and every method that I have tried has resulted in an error.
> > > > 
> > > > So, I can call node[‘hostname’] and get the hostname; not a problem.
> > > > But then let’s say I want to write something that calls an attribute 
> > > > in which the host name is one of the path components… e.g.
> > > > node[‘ec2’][‘instances’][HOSTNAME][‘ip’]…
> > > > 
> > > > if I manually type in the host name, no problem:
> > > > node[‘ec2’][‘instances’][‘myhappyhost’][‘ip’]
> > > > BOOM: 12.34.56.78
> > > > 
> > > > but when I try any variation of:
> > > > node[‘ec2’][‘instances’][node[‘hostname’]][‘ip’]
> > > > or
> > > > var = node[‘hostname’]
> > > > node[‘ec2’][‘instances’][var][‘ip’]
> > > > node[‘ec2’][‘instances’][“var”][‘ip’]
> > > > node[‘ec2’][‘instances’][‘var’][‘ip’]
> > > > node[‘ec2’][‘instances’].var[‘ip’]
> > > > etc…
> > > > 
> > > > nothing but errors ☹
> > > > 
> > > > Am I missing a permutation of syntax here? Is there documentation 
> > > > that would have given me the answer? Is this even possible?
> > > > 
> > > > Your suggestions and help are appreciated!
> > > 
> > > 
> > > node['ec2']['instances'][var] is correct, however chances are you are 
> > > not storing things correctly. How are you populating that 
> > > node['ec2']['instances'] hash? Nodes cannot see the node attributes of 
> > > other nodes by default, you need to use the search API or something 
> > > else to get that data.
> > > 
> > > --Noah

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail




Archive powered by MHonArc 2.6.16.

§