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!
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.
"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
default['demo']['nameservers']['one'] = "one"
default['demo']['nameservers']['two'] = "two"
default['demo']['nameservers']['three'] = "three"
template "/etc/dummy" do
source "dummy.erb"
owner 'root'
group 'root'
mode '0644'
end
<% node['demo']['nameservers'].each do |name,server| %>
nameserver <%= server %>
<% end %>
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
"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
Sample data
{
"id": "nameservers",
"list": [
"one",
"two",
"three"
]
}
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
<% @servers.each do |server| %>
nameserver <%= server %>
<% end %>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With