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

Josh Cooper
Sat, 15 Jun 2013 05:25:17 -0000

On Friday, June 14, 2013, Paul Nickerson wrote:

> 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 <javascript:_e({},
> ''cvml'',
''puppet-users%2Bunsubscribe@googlegroups.com'');>.
> To post to this group, send email to
puppet-users@googlegroups.com<javascript:_e({}, ''cvml'',
''puppet-users@googlegroups.com'');>
> .
> Visit this group at http://groups.google.com/group/puppet-users.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

Another approach would be to create a custom fact whose boolean value
depends on the service being installed or not. Then in your manifest, only
manage the service if and only if the fact is true.

Josh


--
Josh Cooper
Developer, Puppet Labs

*Join us at PuppetConf 2013, August 22-23 in San Francisco - *
http://bit.ly/pupconf13*
**Register now and take advantage of the Early Bird discount - save 25%!*

--
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.