Chef and erb templates. How to use boolean code blocks

社会主义新天地 提交于 2019-12-31 12:58:06

问题


I am new to chef, ruby, ruby DSL, and erb. I come from python. In a ruby erb template I want to do something like this.

<% if node[:monit][:server]=='nginx' -%>

ALL OF MY NGINX TEXT 

<% end -%>

<% if node[:monit][:server]=='redis' -%>

ALL OF MY REDIS TEXT 

<% end -%>

Clearly I am missing something about proper syntax.

Thanks


回答1:


Try this:

<% if node[:monit][:server]=='nginx' -%>

  nginx_text=<%= node[:nginx][:text] %> 

<% end -%>

<% if node[:monit][:server]=='redis' -%>

  redis_text=<%= node[:redis][:text] %> 

<% end -%>

Code wrapped in <% %> or <% -%> is a statement that is evaluated. Code wrapped in <%= %> is code that is evaluated and the result is placed into the file. Harcoded strings dont have to be wrapped in erb tags if they are constant, but Ruby code must be wrapped in erb tags if you want the result of that code to go into your file



来源:https://stackoverflow.com/questions/9935905/chef-and-erb-templates-how-to-use-boolean-code-blocks

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!