[chef] possible to call data_bag_item w/in erb template?


Chronological Thread 
  • From: Bryan Berry < >
  • To:
  • Subject: [chef] possible to call data_bag_item w/in erb template?
  • Date: Sat, 8 Oct 2011 16:38:38 +0200

I am trying to call the data_bag_item method within an erb template.  My sudo users may have multiple sudo cmds so i have embedded them directly into the user's databag like so

file: users/oper.json
{
  "id": "oper",
  "groups": "oper",
  "uid": 977,
  "shell": "\/bin\/bash",
  "password": "$1$WV1cZLkt$FmFEuRFCGKWaabud6.Gld1",
  "sudo_cmds": [ "ALL=(ALL) NOPASSWD:/sbin/reboot",
                "ALL=(ALL) NOPASSWD:/sbin/poweroff" ]
}

file: recipes/default.rb
sudoers = []
data_bag('users').each do |user_name|
        u = data_bag_item('users', user_name)

        if u['sudo_cmds']
                sudoers << u['id']
        end
end

template "/etc/sudoers" do
  source "sudoers.erb"
  mode 0440
  owner "root"
  group "root"
  variables(
    :sudoers => sudoers
  )
end


file: sudoers.erb
# User privilege specification
root          ALL=(ALL) ALL

<% @sudoers.each do |user|
        cmds = data_bag_item('users/#{user}', 'sudo_cmds')

        cmds.each do |cmd|  -%>
<%= user %> <%= cmd %>
<%      end
   end -%>


Here is the error i get when I try to do this 

Generated at Sat Oct 08 16:38:16 +0200 2011
Chef::Mixin::Template::TemplateError: 

Chef::Mixin::Template::TemplateError (undefined method `data_bag_item' for #<Erubis::Context:0xb6ffcb64>) on line #12:

 10: 
 11: <% @sudoers.each do |user| 
 12:    cmds = data_bag_item('users/#{user}', 'sudo_cmds')      
 13:    
 14:    cmds.each do |cmd|  -%>





Archive powered by MHonArc 2.6.16.

§