Ask Your Question

How can I enable a service only when it's installed?

asked 2013-09-20 04:25:31 -0500

ujjain gravatar image

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?

edit retag flag offensive close merge delete


Could you please precise the OS you are working on ?

doc75 gravatar imagedoc75 ( 2013-09-20 14:01:47 -0500 )edit

Red Hat 6.4

ujjain gravatar imageujjain ( 2013-09-21 01:23:13 -0500 )edit

onlyif parameter is valid for the exec resource only, not for the service.

golja gravatar imagegolja ( 2013-09-21 03:06:21 -0500 )edit

4 Answers

Sort by ยป oldest newest most voted

answered 2013-09-21 03:21:22 -0500

golja gravatar image

One way to do it is to write a custom fact, which checks the exsistence of the file on the client side and send the result back to the master.

Then just add something like that in your node definition:

if $::custom_fact_name {
  include scom

For how to write a custom fact check the resources.


edit flag offensive delete link more

answered 2013-09-23 07:18:22 -0500

rahul khengare gravatar image

updated 2013-09-23 07:20:24 -0500

Onlyif meta parameter is not available for service resource.

Here is the work around for that, use dummy command (say sleep ) in exec resource only if /opt/microsoft/scx file is present and subscribe the service resource to the exec resource using [subscribe ] meta parameter.

Whenever the exec resource executes then it calls service resource.

If the /opt/microsoft/scx file is not present then exec is not executed and ultimately the service resource also not get executed.

Refer following code snippet,

class scom {

          path => "/usr/bin:/bin",
          command => "sleep 1",
          onlyif => "test -f /opt/microsoft/scx",      

   service ...
edit flag offensive delete link more


This would not stop puppet from checking if the service is running and enabled. But it will cause the service to restart every time puppet runs when `/opt/microsoft/scx ...(more)

Stefan gravatar imageStefan ( 2013-09-23 14:28:53 -0500 )edit

You are right, the code snippet i have given to you is only check the service file is present or not, if present then service get restarted each time. do ...(more)

rahul khengare gravatar imagerahul khengare ( 2013-09-24 00:57:22 -0500 )edit

answered 2013-09-22 03:21:43 -0500

Stefan gravatar image

You define the desired state of your machine in puppet manifests and puppet will make sure to transform your current state to the desired state.

The situation you are describing is that your desired state (a) Service should run b) Don't care about service) depends on the current state (scom is installed / not installed). While you can write a custom fact to have that piece of information available in your puppet manifest (and if that is your solution you should vote up golja's answer), I think that in most cases it is best to let puppet handle that ... (more)

edit flag offensive delete link more

answered 2013-09-20 10:54:41 -0500

Tomasz Olszewski gravatar image

updated 2013-09-21 08:26:12 -0500

Below solution will not work with puppetmaster

I'm a novice in puppet, so maybe there is much better solution, but you can try something like this:

class scom {
    @service { 'scx-cimd':
        ensure => running,
        enable => true,
        hasstatus => true,
        hasrestart => true,

  if ( inline_template("<%= File.exist?('/opt/microsoft/scx') ? 1 : 0 %>") == '1' ) {
    realize Sevice['scx-cimd']

You can read more here:

Parameter onlyif that you tried to use exists for Exec resource, not for Service.

edit flag offensive delete link more



inline_template is a function which means that it will be executed on the puppet master itself, so it won't really check that the file exists on the client side.

golja gravatar imagegolja ( 2013-09-21 03:10:43 -0500 )edit

Thank! I didn't know that. So this solution will work only for infrastructures without puppetmaster (Where manifests are deployed direcly to nodes, and applied with puppet apply)

Tomasz Olszewski gravatar imageTomasz Olszewski ( 2013-09-21 04:47:24 -0500 )edit

yes so whenever you design your solution you have to have that in mind. Personally I would never rely on a directory existence to determine if I have that service ...(more)

golja gravatar imagegolja ( 2013-09-21 10:29:32 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-09-20 04:25:31 -0500

Seen: 5,183 times

Last updated: Sep 23 '13