Chef and ruby templates - how to loop though key value pairs?

Viewed 35005

1) I have a data bag as follows:

 "zookeeper":{
        "server1":"111.111.111.111",
        "server2":"222.222.222.222"
        },

2) In my recipe I get the hash as follows.

data_bag("mydb")
db = data_bag_item("mydb", "rtb")
ZOOKEEPER = db['zookeeper']

3) Also in my recipe I have a template as follows:

template "/etc/zookeeper/conf/zoo.cfg" do
  path "/etc/zookeeper/conf/"
  source "zoo.cfg.erb"
  owner "root"
  group "root"
  mode "0644"
 variables :zookeeper => #{ZOOKEEPER}
end

4) I need to have my template look like this

server.1=111.111.111.111:2888:3888
server.2=222.222.222.222:2888:3888

My question is this. How do I pass the hash to the template so I can loop through the hash and create the temlplate? I am not a strong ruby coder.

for example:

count = 1
for server, ipaddress in zookeeper:
      server.count=ipaddress:2888:3888
      count = count + 1
1 Answers
Related