1

I have a ERB(database.yml.erb) file template for database.yml for rendering using chef 11.10

I want to have an output(after rendering in chef) in database.yml, like below:

default: &default
  adapter: mysql2
  username: <%= ENV['diff_db_user'] || <username> %>
  password: <%= ENV['diff_db_password'] || <password> %>
  ...

In the above yaml, I expect the values <username> and <password> are substituted from @node[:database][:username] & @node[:database][:password]

So, the reason I wanted to do this is, to use a different password just by setting the environment variable while running a rake job like db migration

So, I tried the above with using escaping the erb tag as mentioned in How do I escape the ERB tag in ERB but in the output yml I could see the value is not substituted, it just prints the variable itself

for example I had an erb like this:

default: &default
      adapter: mysql2
      username: <%%= ENV['diff_db_user'] || @node[:database][:username] %>
      password: <%%= ENV['diff_db_password'] || @node[:database][:password] %>
      ...

The output I'm getting for it is:

default: &default
  adapter: mysql2
  username: <%= ENV['diff_db_user'] || @node[:database][:username] %>
  password: <%= ENV['diff_db_password'] || @node[:database][:password] %>
  ...

So, wanted to know is there any other way for to get printed like the one I initially mentioned at the top, when using template in chef to render the erb to yml

sarathprasath
  • 589
  • 1
  • 8
  • 20

2 Answers2

0

You could add another item in database.yml

development_with_password:      
  adapter: mysql2
  username: 
  password: 

and call it in your rake task e.g.

RAILS_ENV=development_with_password rails db:migrate
Eyeslandic
  • 14,553
  • 13
  • 41
  • 54
0

Something like this?! You need yo use nested <% %> tags, escaping the outer ones. Also don't forget the quotes.

username: <%%= ENV['diff_db_user'] || "<%= @node[:database][:username] %>" %>
password: <%%= ENV['diff_db_password'] || "<%= @node[:database][:password] %>" %>
Draco Ater
  • 20,820
  • 8
  • 62
  • 86