0

I'm starting to learn how develop recipes to chef. I need to install Ganglia Monitor in some servers (or nodes in ganglia literature). So that's why I'm checking if the plataform is ubuntu, centOS and many others to install the correct package.

The issue is that I have two different .config files, actually there is only one or two parameters in this .config file that would differ one from another. I need help how to detect in which datacenter that the server belongs so that I can copy the properly .config file. So far, I was able to develop this script below, but I have some dobut, which are in comments in the code.

#
# Cookbook Name:: ganglia
# Recipe:: default
#
# Copyright 2013, Valter Henrique.com
#
# All rights reserved - Do Not Redistribute
#
# Installing Ganglia Monitor

case node[:platform]
  when "ubuntu", "debian"
    package "ganglia-monitor"
  when "redhat", "centos", "fedora"
    package "ganglia-gmond"
  end
  user "ganglia"
end

# Setting different .config files
case ipaddress
# DataCenter #1
# how put more options in the when condition ? A when for /^200.222./ or /^200.223./ ?
    when /^200.222./ 
        # putting config file
        cookbook_file "/etc/ganglia/gmond.conf" do
            owner "root"
            group "root"
            mode "0644"
            source "dc1/gmond.conf"
            notifies(:restart, "service[gmond]")
        end
    #DataCenter #2
    when /^216.235./
        cookbook_file "/etc/ganglia/gmond.conf" do
            owner "root"
            group "root"
            mode "0644"
            source "dc2/gmond.conf"
            notifies(:restart, "service[gmond]")
        end
  end

Any suggestion in how I develop this code in a better way ?

MadHatter
  • 78,442
  • 20
  • 178
  • 229
Valter Silva
  • 155
  • 1
  • 4
  • 14

1 Answers1

1

You can use variables in source attribute for cookbook_file resource to avoid code duplication.

dc = case ipaddress
     when /^200\.222\./
       'dc1'
     when /^216\.235\./
       'dc2'
     end

cookbook_file "/etc/ganglia/gmond.conf" do
   owner "root"
   group "root"
   mode "0644"
   source "#{dc}/gmond.conf"
   notifies(:restart, "service[gmond]")
end
AlexD
  • 8,179
  • 2
  • 28
  • 38