how to pass 2 data bag variables to template chef

3.6k Views Asked by At

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'] %>
1

There are 1 best solutions below

4
On

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