4

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!

Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
Ravi kumar
  • 129
  • 3
  • 9

2 Answers2

8

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
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
  • Its really very great explanation and now only I understood it very clear. If you don't mind can you please let me know where I can go through such things in Chef with simple example as you given. – Ravi kumar May 07 '16 at 07:54
  • @Ravikumar Install the ChefDK and you can start by generating a cookbook: "chef generate cookbook demo". After that it's only matter of learning the various tools. See: https://docs.chef.io/ and http://kitchen.ci/ Hope this helps – Mark O'Connor May 07 '16 at 11:46
3

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.

coderanger
  • 52,400
  • 4
  • 52
  • 75