简体   繁体   English

使用CHEF将配方应用于主机名与regexp匹配的所有服务器

[英]apply recipe to all the servers whose hostnames matches regexp using CHEF

I'm creating a chef recipe to apply a configuration change on all servers whose hostname matches a specific pattern using regexp. 我正在创建一个厨师食谱,以使用regexp在主机名与特定模式匹配的所有服务器上应用配置更改。 However, I'm not sure how to do it. 但是,我不确定该怎么做。

Example: my hostname looks like this: 示例:我的主机名看起来像这样:

  • dvabwichf01 dvabwichf01
  • dvcdwichf01 dvcdwichf01

my recipe in default.rb is : 我在default.rb中的食谱是:

case node['hostname']
when '*ab*'
  template "/tmp/regextest" do
    source "test_ab.erb"
    mode "0644"
  end
else
  template "/tmp/regextest" do
    source "test_cd.erb"
    mode "0644"
  end
end

But this is not working as expected, only the "else" template is updating on all servers. 但这不能按预期工作,只有“ else”模板在所有服务器上都在更新。 please assist. 请协助。

You would need to use an actual regex, not a string like you have there (also you're using fnmatch glob matching, not a regex). 您将需要使用实际的正则表达式,而不是像那里那样的字符串(同样,您正在使用fnmatch glob匹配,而不是正则表达式)。 That would only fix when the hostname is literally *ab* . 仅当主机名实际上是*ab*时,该问题才能解决。 A regexp literal in Ruby usually looks like /whatever/ . Ruby中的regexp文字通常看起来像/whatever/ so when /ab/ in this case. 所以在这种情况下是when /ab/

I used switch for choosing values by adding a method in my helper file (in my case I put it into / app / helpers / application_helper.rb Example below: def name_of_your_method(hostname) case hostname when "Host1" "template_1" when "Host2" "template_2" when "Host2" "template_3" when "Host3" "template_4" else "template_default" end end 我通过在助手文件中添加方法来使用开关来选择值(在我的情况下,将其放入/app/helpers/application_helper.rb中。下面的示例:def name_of_your_method(hostname)case主机名当“ Host1”时为“ template_1”,当“ Host2”时为“” template_2“当” Host2“” template_3“当” Host3“” template_4“否则” template_default“结束

Then in your code you would use the name in your method: 然后在代码中,您将在方法中使用名称:

<%= user.hostname %>

And in your table(data) you would have a column for hostname(in this example) 在表(数据)中,您将有一列用于主机名(在此示例中)

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM