Best practices – Conditionals for Chef resources without guard attributes

chefhadoop

We are setting up a cluster using Apache Ambari.

Our Chef run is interrupted by the need to use Ambari to provision the Hadoop cluster. Current installs are a three part process:

  1. Initial Chef run to prep OS.
  2. Use Ambari to configure (and later manage) Hadoop cluster.
  3. Post-Ambari configs using a second Chef run.

I searched similar questions, but answers all revolved around using resources that included guard attributes for conditionals. The second run uses a series of resources that do not accept guard attributes.

I've been working around this by using execute commands and "not_if"/"only_if" guards. Example:

%w{package1 package2 package3 package4}.each do |package|
  execute "Install #{package}" do
    command "yum install #{package}"
    user 'root'
    only_if { ::File.exists?("/etc/yum.repos.d/ambari.repo")}
  end
end

But this gets tedious to write for each resource. There has got to be a better way.

Is there a standard or "best" practice for executing all remaining code (or select resources) in a recipe if one conditional is met?

I thought about wrapping in a Ruby block and using notifies, but blocks seem declarative. I'm not sure how best to set them up to "do x if y, else nothing", or even if that's the right tool for the job?

ruby_block "Check for Ambari.repo" do
  block do
    File.exists?("/etc/yum.repos.d/ambari.repo")
  end
  notifies :run "execute[package]" :immediately
end

%w{package1 package2 package3 package4}.each do |package|
      execute "Install #{package}" do
        command "yum install #{package}"
        user 'root'
        action :nothing
      end
end

Best Answer

You are asking for an if block so why not just use an if block?

if File.exists?("/etc/yum.repos.d/ambari.repo")
  %w{package1 package2}.each do |pkg|
    package pkg
  end

  ... do some other stuff here if you want...
end

The default action for package is install so you don't need the full block in this case.

Related Topic