小编Kin*_*ook的帖子

厨师模板 - 有条件地插入一块文本

使用Chef,只有满足条件,才能从模板插入文本块吗?

假设我们有一个属性:

node["webapp"]["run"] = "true"
Run Code Online (Sandbox Code Playgroud)

如果webapp为true,我们只想在sites-enabled/app.conf中的nginx .conf中输入一个条目,如下所示:

#The nginx-webapp.conf.erb template file

SOME WORKING NGINX CONFIG STUFF

<% if node["webapp"]["run"]=="true" -%>
location /webapp {      
    try_files  $uri @some_other_location;
}

<% end -%>

SOME OTHER WORKING NGINX CONFIG STUFF
Run Code Online (Sandbox Code Playgroud)

就目前而言,条件文本没有错误,它只是永远不会出现.我已经仔细检查了模板是否可以通过以下方式查看节点属性:

<%= node["webapp"]["run"] %>
Run Code Online (Sandbox Code Playgroud)

哪个DID将文本"true"插入配置文件中.

我在Chef和erb模板中看到过.如何使用布尔代码块,我可以插入看似只是带有来自节点的评估变量的文本.

我试过换到

<% if node[:webapp][:run]=="true" -%>
TEXT
<% end -%>
Run Code Online (Sandbox Code Playgroud)

无济于事.

我有什么想法我做错了吗?谢谢!

编辑:

根据Psyreactor的回答,在模板本身我停止尝试评估字符串"true",而是使用它:

SOME WORKING NGINX CONFIG STUFF

<% if node["webapp"]["run"] -%>
location /webapp {      
    try_files  $uri @some_other_location;
}

<% end -%>

SOME …
Run Code Online (Sandbox Code Playgroud)

templates nginx conditional-statements chef-infra

3
推荐指数
1
解决办法
2480
查看次数