[Puppet Users] Re: Disable A Windows Service Only If It Exists

Paul Nickerson
Fri, 14 Jun 2013 16:54:15 -0000

I came up with a solution using exec and sc.

In a module I called disable_services:

define disable_services {
  exec { "stop_${title}":
    command => "C:\\Windows\\System32\\cmd.exe /c sc stop
\"${title}\"",
    onlyif  => "C:\\Windows\\System32\\cmd.exe /c sc query
\"${title}\"",
    unless  => "C:\\Windows\\System32\\cmd.exe /c sc query
\"${title}\" |
find \"STOPPED\"",
    before  => Exec["disable_${title}"],
    }
  exec { "disable_${title}":
    command => "C:\\Windows\\System32\\cmd.exe /c sc config
\"${title}\"
start=disabled",
    onlyif  => "C:\\Windows\\System32\\cmd.exe /c sc query
\"${title}\"",
    unless  => "C:\\Windows\\System32\\cmd.exe /c sc qc
\"${title}\" | find
\"DISABLED\"",
    require => Exec["stop_${title}"],
    }
  }

In a manifest:

disable_services { ["CVSNT", "CVSNT Locking Service",
"CVS", "CVSLock"]: }

On Thursday, April 11, 2013 11:45:52 AM UTC-4, Paul Nickerson
wrote:
>
> I am wondering whether it is possible to have Puppet only stop and disable
> a Windows service if it exists. I have Puppet master 3.1.1 on CentOS, and
> Puppet clients 3.1.1 on many Windows machines. Right now, I have the bit
> below for certain nodes:
>
> service { "CVSNT":
> ensure => ''stopped'',
> enable => ''false'',
> }
>
> Some of the nodes have this service, some do not. Occasionally, new nodes
> are added that this applies to (through inherits statements), and these new
> nodes may or may not have this service. It''s not just this
service, either;
> I have an array of services I need to ensure won''t run. I just
simplified
> the above example with one service.
>
> If the Windows node does not have the service, then the Puppet agent still
> runs successfully. But, when I run puppet agent --test in the Command
> Prompt with Puppet on the node, I get this error:
>
> Error: /Stage[main]//Node[winbase]/Service[CVSNT]: Could not evaluate:
> Cannot get status of CVSNT, error was: The specified service does not exist
> as an installed service.
>
> The agent continues to run and completes successfully. But, getting
> several of these lines at different points each time I run the Puppet agent
> makes testing and debugging whatever other module I''m working on a
little
> difficult. I would like to get rid of the clutter.
>
> I''ve been researching this topic for a while, and haven''t
come up with any
> solutions. Is there any way to have Puppet disable a Windows service only
> if it exists? Or, is there a way to suppress these warning lines
> without suppressing other output?

--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to puppet-users+unsubscribe@googlegroups.com.
To post to this group, send email to puppet-users@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users.