Search code examples
windows-serviceschef-infra

Chef windows_service resource - Configuring existing service


I'm having some issues ensuring that a Windows service resource is configured correctly after it is created. I'm in the situation where the creation of the service is handled by a separate installer (.exe).

I need to configure this service afterwards to use a different user.

Here's my resource definition:

windows_service 'Service' do
  action [:configure_startup, :start]
  service_name 'service'
  startup_type :automatic
  run_as_user agent_credentials['user']
  run_as_password agent_credentials['password']
  only_if { ::Win32::Service.exists?('myservice') }
end

I'm pulling the credentials out of an encrypted data bag.

The issue I'm facing is that the account the service runs as is never being updated. In my client run, it sees no need to apply the resource actions after the .exe is installed:

  * windows_service[Service] action configure_startup (up to date)
  * windows_service[Service] action start (up to date)

I can only get my resource to apply if the service is stopped first, which immediately after install, it is not. Do I have to use Chef to stop it first and then start it again? I thought it would be able to detect that the configuration of the service did not match that of the defined resource and then correct it...

Thanks


Solution

  • just add :stop to the action array, such as

    windows_service 'Service' do
      action [:configure_startup, :stop, :start]
      service_name 'service'
      startup_type :automatic
      run_as_user agent_credentials['user']
      run_as_password agent_credentials['password']
      only_if { ::Win32::Service.exists?('myservice') }
    end
    

    if the service does not fail on stop even when the service is stopped, then this should ease your pain. note that you might need to place the :stop in a different place on the array, depending on the service behavior.