vinu
vinu

Reputation: 113

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:

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.

Upvotes: 0

Views: 692

Answers (2)

coderanger
coderanger

Reputation: 54251

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.

Upvotes: 2

Laurie
Laurie

Reputation: 162

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

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

Upvotes: 1

Related Questions