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


Chronological Thread 
  • From: Bryan Berry < >
  • To:
  • Subject: [chef] Re: Re: possible to call data_bag_item w/in erb template?
  • Date: Sun, 9 Oct 2011 09:06:36 +0200

alright I figured out what I was doing wrong

in my ruby code

sudoers[username] = u['sudo_cmds']

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

thanks for your help Adam!

it is taking me some time to figure out all the tricks in ruby


On Sat, Oct 8, 2011 at 10:04 PM, Adam Jacob < "> > wrote:
Hi Bryan - in the data structure you have there, you can just use it directly.

<% @sudoers.each do |user| %>
<%   user["sudo_cmds"].each do |cmd| %>
<%= cmd %>
<%   end %>
<% end %>

Adam

On Sat, Oct 8, 2011 at 7:38 AM, Bryan Berry < "> > wrote:
> 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|  -%>
>
>



--
Opscode, Inc.
Adam Jacob, Chief Product Officer
T: (206) 619-7151 E: ">




Archive powered by MHonArc 2.6.16.

§