Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 ?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.