Search code examples
regexchef-infradslchef-recipe

Regular expressions in chef


How do we use regular expressions/pattern matching in a chef recipe. In my default.rb recipe, for example:

case node["env_name"]
when "admin-dev"
     include_recipe "lxsetup-deploy::lxsetup-dev"
when "admin-dit"
     include_recipe "lxsetup-deploy::lxsetup-dev"
when "admin-perf1"
     include_recipe "lxsetup-deploy::lxsetup-qa"
when "admin-perf2"
     include_recipe "lxsetup-deploy::lxsetup-qa"
else
     include_recipe "lxsetup-deploy::lxsetup-prod" 
end

Is there any way I can do something like "admin-d*" or "admin-d[a-z]*" to match the strings admin-dev and admin-dit. I tried a bunch of stuff including the above ones but nothing helped.


Solution

  • Personnaly I like to use a hash to do this kind of mapping like this (and in attributes):

    attribute.rb

    env_to_suffix = 
    { 
      "admin-dev" => "dev",
      "admin-dit" => "dev",
      "admin-perf1" => "qa",
      "admin-perf2" => "qa"
    }
    default['company']['env_suffix'] = env_to_suffix[node.chef_environment] || "prod"
    

    in recipe.rb

    include_recipe "lxsetup-deploy::lxsetup-#{node['company]['env_suffix']}" 
    

    I find it easier to maintain in long term and it allow a per node override for edge cases.