asdoylejr asdoylejr - 1 year ago 68
Ruby Question

How would I include and configure a ruby library in a Chef recipe?

I want to include the Diplomat gem in my Chef cookbook so that I can perform Consul variable lookups in .erb templates.

I need to configure the Consul URL:

irb(main):015:0> require 'diplomat'
irb(main):016:0> Diplomat.configure do |config|
irb(main):017:1* config.url = "consulurl:80"
irb(main):018:1> end

Set a variable as the URL path:

irb(main):020:0> kv_path = "path/to/variable"
=> "path/to/variable"

And finally, perform the lookup within the templates.

irb(main):022:0> foo = Diplomat::Kv.get(kv_path + '/test_foo_123')
=> "bar"

Where in the cookbook would I need to write the configuration code above such that I can perform variable lookups within .erb templates?

Answer Source

You want to use the chef_gem resource, but make sure to run it during the compile phase:

chef_gem 'diplomat' do
  action :nothing
  compile_time false
require 'diplomat'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download