How do I print out Chef default.rb values to stdout?

795 Views Asked by At

I want to read out some urls and versions numbers from this Chef default attribute file:

https://github.com/aws/aws-parallelcluster-cookbook/blob/develop/attributes/default.rb

I know I can use a grep, sed, awk solution but those are usually more brittle and I don't have control over this file. I just want to be able to download it and parse out the values I need.

An example solution could be print out default.rb as JSON to stdout and parse it with jq or create a ruby script that prints it out.

Thanks in advance!

UPDATE:

I want a way to read data out of the default.rb file, IE I want to grab version data/urls out of default.rb and use it programmatically for something else.

It would be nice to be able to do

require('default.rb')

puts default['cfncluster']['nvidia']['driver_version']

This would print out the value from line 68 to stdout and I could then do anything I wanted with it. This above is my failed attempt to us ruby. This method fails, it says that default is not defined. I am not a ruby person maybe there is a way to load it in without getting that error message.

Any suggestions are appreciated. If you can print everything out as JSON using some chef cli that is fine too. I can parse what I need out of it.

1

There are 1 best solutions below

5
Mr. On

you did not specify whether you'd like to do it during chef-client.

although it is not recommended, you can achieve it by usuin eval(). for instance:

# foo.rb
default['foo']['bar'] = "baz"
# read.rb
default = Hash.new {|hash,key| hash[key]={}}
eval(File.read("./foo.rb"))
puts default['foo']['bar']
$ ruby test.rb
baz