2

I was said to use the onlyif-parameter, but there doesn't seem to exist one for the service-type.

What can I do to enable this service only if it's installed? It's installed with a binary installer, so testing if the installation directory exists t

class scom {
    service { 'scx-cimd':
        ensure => running,
        enable => true,
        hasstatus => true,
        hasrestart => true,
        onlyif => "test -f /opt/microsoft/scx"
    }
}

The current error message is "Invalid parameter onlyif", because service doesn't have an onlyif parameter. What can I do to enable this service only if it's installation directory exists?

ujjain
  • 348

1 Answers1

2

The service type (3.x, 2.x) does not have an onlyif parameter, that is part of the exec type (3.x, 2.x.

To do what you want to do, there are two ways. The first way is the normal and preferred way to do stuff. The second way is a hack that will work too, ideally the the chkconfig of the service should be a separate exec but there's no harm in chkconfiging a service on even if it is already configured that way.

Option 1. Write a fact to see if the service is installed and put a conditional around the serivce type:

# service_scx_installed.rb
Facter.add('service_scx_installed') do
  setcode do
    File.exists?('/opt/microsoft/scx')
  end
end

# scom.pp
class scom {
  if $service_scx_installed {
    service { 'scx-cimd':
        ensure => running,
        enable => true,
        hasstatus => true,
        hasrestart => true,
    }
  }
}

Option 2. Put all of the logic in a big exec:

exec {'start and chkconfig on scx service':
  command  => 'chkconfig scx service on; service scx restart',
  onlyif   => ['test -f /opt/microsoft/scx', '! service scx status'],
  provider => 'shell',
}