Search code examples
rubylinuxchef-infrachef-recipecookbook

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. However, I'm not sure how to do it.

Example: my hostname looks like this:

  • dvabwichf01
  • dvcdwichf01

my recipe in default.rb is :

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. please assist.


Solution

  • 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). That would only fix when the hostname is literally *ab*. A regexp literal in Ruby usually looks like /whatever/. so when /ab/ in this case.