2

I am trying to pass 2 data bags as variables into a template but it end in error message. Do anyone know how do i pass 2 databags to a template?

Recipe

db = data_bag_item('dbconnect', 'connection')
dbkey = data_bag_item('database', 'databasename')
template '/etc/config.cnf' do 
    source 'config.cnf.erb'
    action :create
    variables ( 
        :dbcon => db,
        :dbk => dbkey
    )
    end

Template

connection = mysql://<%= @dbcon['dbuser'] %>:<%= @dbcon['dbpasswd'] %>@<%= @dbcon['dbname'] %>/<%= @dbk['dbname'] %>
gopisaba
  • 41
  • 1
  • 6
  • 1
    Can you edit your question to show the error message? Can you also include the structure of the databags? – samdunne Jun 23 '15 at 17:16

1 Answers1

2

Okay. I got the answer. I missed {} brackets in variables.

db = data_bag_item('dbconnect', 'connection')
dbkey = data_bag_item('database', 'databasename')
template '/etc/config.cnf' do 
  source 'config.cnf.erb'
  action :create
  variables ({ 
    :dbcon => db,
    :dbk => dbkey
  })
end
gopisaba
  • 41
  • 1
  • 6
  • @Tensibai see http://acrmp.github.io/foodcritic/#FC034 (in the example there is a comma missing, though) – Roland Jun 24 '15 at 22:01
  • and another example: https://github.com/opscode-cookbooks/chef-client/blob/master/recipes/config.rb#L81-L88 - Nowadays with Ruby 1.9+ you can also use the "key: value" syntax instead of the "hash-rocket" ("=>") – Roland Jun 24 '15 at 22:07
  • @Tensibai Can you please be less aggressive in your comments? Thanks – Roland Jun 25 '15 at 10:48
  • Thanks Ronald When we pass the value as string then (....) is working fine but when i try to pass data bags (or nested hash) then i need ({.....}) – gopisaba Jun 26 '15 at 14:50