Martin Konrad
Martin Konrad

Reputation: 1095

Puppet: conditional restart of service

Is it possible to conditionally skip refresh events on a service resource? Or alternatively: Is it possible to prevent a service resource inside a class to be refreshed when the class is notified?


Context: I have a Puppet module containing the following manifest (simplified):

class foo(
  Boolean pre_process_service  = true,
  Boolean auto_restart_service = true
) {
  if $pre_process_service {
    exec { 'process config':
      ... # details including a pretty complex command - should be hidden in my module
      notify => Service['foo'],
    }
  }

  service { 'foo':
    ensure => 'running',
    enable => true,
  }
}

which might be used like so:

file { 'config':
  ... # copies config from somewhere
}

class { 'foo':
  auto_restart_service => false,
  subscribe            => File['config'],
}

How can I avoid restarting the service when the user specifies auto_restart_service => false?

Note that the user of the module decides how to provide the configuration (copying files, checking out a Git repository,...) so I can't do that inside my module. Instead the class subscribes to the resource providing the configuration. As long as the user goes with the default of auto_restart_service = true everything works fine and even disabling the preprocessing of the configuration works correctly. However, when the user specifies auto_restart_service = false the service will still restart since the service resource is refreshed when the class is notified. Wrapping the service resource into an if block like I did with the exec resource doesn't work either since the service resource does multiple things:

  1. It starts the service if it isn't running
  2. It enables the service if it isn't enabled
  3. It restarts the service if notified

I only want to conditionally prevent (3) from happening while always doing (1) and (2). Is there a way to do this?

Upvotes: 0

Views: 2576

Answers (2)

Oleg
Oleg

Reputation: 1

The idea from tectux is really nice. I've enhanced the if conditions. I have decided to use script for custom fact.

modules/autofs/facts.d/autofs.sh

#!/bin/sh
DECISION=`test -f /usr/bin/docker && /usr/bin/docker ps -q |grep -q . && echo no`

if [ "x$DECISION" == "x" ] ; then
    DECISION=yes
fi
echo '{'
echo '  "autofs": {'
echo "     \"do_automatic_restart\": \"$DECISION\""
echo '  }'
echo '}'

So, the output of the script

# modules/autofs/facts.d/autofs.sh 
{
  "autofs": {
     "do_automatic_restart": "yes"
  }
}

Now we can use the custom fact

if $facts['autofs']['do_automatic_restart'] == "no" {                                                                                                                                            
    $_attr = { 'restart' => "logger puppet agent: automounter is NOT going to be RESTARTED due to active containers on a host" }                                                                                                                                                      
} else {                                                                                                                                                                                         
    $_attr = {}                                                                                                                                                                                  
}                                                                                                                                                                                                

Upvotes: 0

tectux
tectux

Reputation: 181

I don't think there's a way to not refresh the service when you notify the class. However, you can try to conditionally override how Puppet should restart the service with the restart attribute of the service resource.

Something like this:

if $auto_restart_service {

  # Let the provider handle the restart
  $_attr = {}

} else {

  # Let Puppet execute `true` instead of actually restarting the service
  $_attr = { 'restart' => '/usr/bin/true' }

}

service { 'foo':
  ensure => 'running',
  enable => true,
  *      => $_attr,
}

Upvotes: 2

Related Questions