ruby on rails - Access chef data bag in recipe


Keywords:ruby  on  rails 


Question: 

I have created below data bag item:

{
  "name": "data_bag_item_nameservers_servers",
  "json_class": "Chef::DataBagItem",
  "chef_type": "data_bag_item",
  "data_bag": "nameservers",
  "raw_data": {
    "id": "servers",
    "serverslist": [
      "xxx.xxx.xxx.xxx",
      "xxx.xxx.xxx.xxx"
    ]
  }
}

And in template erb I added below call,

<% @serverslist.each_with_index do |nmserver| %>
nameserver <%= nmserver %>
<% end %>

But its not working for me and giving error as,

Error executing action create on resource 'template[/etc/resolve.conf]'

Chef::Mixin::Template::TemplateError

undefined method each_with_index' for nil:NilClass

Can someone please help me how can I call those data bag item values in cookbook recipe?

Thanks in advance!


2 Answers: 

Have you considered a simpler option using node attributes?

At run-time it's easy to over-ride the cookbook default settings. I point this out because data bags are rarely needed in my experience.

I have included two examples.


Example 1: Using attributes

"demo" cookbook

├── attributes
│   └── default.rb
├── Berksfile
├── Berksfile.lock
├── chefignore
├── metadata.rb
├── README.md
├── recipes
│   └── default.rb
├── templates
│   └── default
│       └── dummy.erb
└── test
    └── integration
        ├── default
        │   └── serverspec
        │       └── default_spec.rb
        └── helpers
            └── serverspec
                └── spec_helper.rb

attributes/default.rb

default['demo']['nameservers']['one'] = "one"
default['demo']['nameservers']['two'] = "two"
default['demo']['nameservers']['three'] = "three"

recipes/default.rb

template "/etc/dummy" do
  source "dummy.erb"
  owner 'root'
  group 'root'
  mode '0644'
end

templates/default/dummy.erb

<% node['demo']['nameservers'].each do |name,server| %>
nameserver <%= server %>
<% end %>

test/integration/default/serverspec/default_spec.rb

require 'spec_helper'

describe file('/etc/dummy') do
  it { should be_file }
  it { should be_owned_by 'root' }
  it { should contain 'nameserver one' }
  it { should contain 'nameserver two' }
  it { should contain 'nameserver three' }
end

Example 2: Using a data bag

"Demo" cookbook with test data bag under the test/integration directory

├── Berksfile
├── Berksfile.lock
├── chefignore
├── metadata.rb
├── README.md
├── recipes
│   └── default.rb
├── templates
│   └── default
│       └── dummy.erb
└── test
    └── integration
        ├── data_bags
        │   └── stuff
        │       └── nameservers.json
        ├── default
        │   └── serverspec
        │       └── default_spec.rb
        └── helpers
            └── serverspec
                └── spec_helper.rb

test/integration/data_bags/stuff/nameservers.json

Sample data

{
  "id": "nameservers",
  "list": [
    "one",
    "two",
    "three"
  ]
}

recipes/default.rb

The recipe is more complex now. The data in the data bag must be explicitly retrieved and then passed into the template as a variable

nameservers = data_bag_item('stuff', "nameservers")

template "/etc/dummy" do
  source "dummy.erb"
  owner 'root'
  group 'root'
  mode '0644'
  variables ({
    "servers" => nameservers["list"]
  })
end

templates/default/dummy.erb

<% @servers.each do |server| %>
nameserver <%= server %>
<% end %>

test/integration/default/serverspec/default_spec.rb

require 'spec_helper'

describe file('/etc/dummy') do
  it { should be_file }
  it { should be_owned_by 'root' }
  it { should contain 'nameserver one' }
  it { should contain 'nameserver two' }
  it { should contain 'nameserver three' }
end
 

You need to explicitly load the data bag from your recipe code, usually using the data_bag_item(bagname, itemname) function, and then pass it to the template using its variables property.