Databags parsing and templates

I think I’ve a quite simple question for experts here, but this one’s
driving me mad as there are only a very few examples on how to use databags
with templates on the Web.

I have a databag on my CHEF server, named “instances” and organized like
this :

owner : nobody

id : instances

type : dbapp {

            Java_webapp

           Tomcat

}

webapps : {

_default {

            port : “8001”

            name : “preprod1.test.com”

}

            testjava {

            port : “8002”

            name : “preprodjava.test.com”

}

            Testtomcat {

            port “8003”

            name : “preprodtomcat.test.com”

}

}

A template :

è /templates/default/test.conf.erb :

Set up application listeners here.

worker.list = jkstatus

Test configuration

<% @server.each do |server, port| %>

<%= server %>, <%= port %>

<% end %>

And my recipe to generate a proper configuration file :

è /recipes/default.rb

Cookbook Name:: testdatabag

Recipe:: default

Copyright 2012, TEST

All rights reserved - Do Not Redistribute

require ‘chef/data_bag’

server=[]

port=[]

data_bag(‘instances’).each do |webapps|

s = data_bag_item(‘instances’,'webapps')

server << s['name']

port << s['port']

end

template “test.conf” do

path “/tmp/test.conf”

source “test.conf.erb”

owner “root”

group “root”

mode “0644”

variables(

            :server => server,

            :port => port

)

My result should be :

Test configuration

preprod1.test.com, 8001

preprodjava.test.com, 8002

preprodtomcat.test.com, 8003

And, well, it doesn’t work at all.

Thanks in advance for any help or hint.

Regards,

Philippe Bérard

Your each loop is weird :

On Fri, Dec 14, 2012 at 3:49 PM, Philippe Bérard <philippe.berard@jalios.com

wrote:

data_bag(‘instances’).each do |webapps|****

s = data_bag_item(‘instances’,'webapps')****

server << s['name']****

port << s['port']****

end

The variable webapps is never used in the block (you are using the string
'webapps' instead); and the way you are defining s, it will contain the
same info every time.
And your each loop in the template looks very funky as well... how is your
block supposed to know about port, if you're iterating on server ?

--
Loïc ANTOINE-GOMBEAUD
IT Engineer I
Plinga GmbH | www.plinga.com
Saarbrücker Straße 20/21 | 10405 Berlin | Germany

E-Mail: loic.gombeaud@plinga.com | Skype:loic.plinga
Telefon : +49(0)160-92286573

Geschäftsführer: Johannes Kreibohm, Florian Schmidt-Amelung
Eingetragen beim Amtsgericht Charlottenburg, HRB 119994