user2986175
user2986175

Reputation: 337

Chef Data Bags and dynamic variable passing

I am trying to figure out a way to get the below code work; I have tried various methods but the chef-client run breaks at the 3rd line.

lsf = "#{node[:env]}"+"_ls"
dsf = "#{node[:env]}"+"_ds"

dsTemplateBag = data_bag_item('configTemplates', "#{dsf}") 
lcTemplateBag = data_bag_item('configTemplates', "#{lsf}")

However on another test recipe I was able to successfully get the following working:

env = "test"

dsTemplateBag = data_bag_item('configTemplates', "#{env}")

I am quite new to Chef and please can someone advise me on how to get this working ?

Upvotes: 1

Views: 1064

Answers (1)

user2986175
user2986175

Reputation: 337

After a little bit debugging I realised there was a typo preventing the data bag to be properly used; hence issue.

dsTemplateBag = data_bag_item('configTemplates', "#{node[:env]}_ls")

this worked for me. And as Tensibai suggested in the above comment mixing concatenation and interpolation is not a good practice (I was desperate to make it work! In my defense).

Upvotes: 1

Related Questions