【问题标题】:Chef cookbook template REGEX to match厨师食谱模板正则表达式匹配
【发布时间】:2021-04-20 20:53:37
【问题描述】:

我有一个模板文件

#fileName: test.properties.erb
<% if @env == 'STAGING' && node['hostname'] =~ /^staging/ %>
somekey=node['hostname']
<% else %>
somekey=defaultNode
<% end %>

我的食谱如下

template ("test.properties") do
  source 'test.properties.erb'
  variables(
    env: "STAGING"
  )
end

我想和 REGEX 检查一起执行 && 操作

当我在 erb 文件中使用以下内容时,它适用于 &&

<% if @env = 'STAGING' && node['hostname'].include?('staging-01') %>
...

我想应用一个正则表达式并检查主机名是否以字符串 staging 开头 假设我有staging-01staging-02 并申请两者

【问题讨论】:

  • node['hostname'] 是字符串吗?
  • 是的,是一个字符串,返回主机名
  • 所以这个有逻辑的文件就是erb文件。

标签: ruby chef-infra


【解决方案1】:

答案似乎是简单的 Ruby 字符串匹配表达式

所以我对表达式 node['hostname'].match(/staging*/) 进行了更改,这成功评估了食谱。

#fileName: test.properties.erb
<% if @env == 'STAGING' && node['hostname'].match(/staging*/) %>
somekey=node['hostname']
<% else %>
somekey=defaultNode
<% end %>

【讨论】:

猜你喜欢
  • 2019-03-03
  • 2015-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-29
  • 2021-11-01
  • 2014-12-05
相关资源
最近更新 更多