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