Search code examples
puppet

In puppet, how to stop a service, perform some action and then start the service?


I need to perform some action (configure something) after stopping the tomcat service. Once the configuration is complete, I need to ensure that the tomcat service is up and running again. I have written following puppet code for the same:

Service {'tomcat': ensure => stopped }
->
class {'config':}
->
Service {'tomcat': ensure => running }

On puppet apply, it is complaining that

'Error: Duplicate declaration: Service[tomcat] is already declared in file'

How to fix this problem. What is the recipe in puppet to stop a service, perform some action and then bring back the service again?


Solution

  • In puppet, you can't declare same service again. that's the error you have.

    With puppet, you needn't care of tomcat stop/start processes. It takes care the final status (called "idemotency"). After you define the relationship between package, config files and services, it will do all jobs for you. For example, you need to understand below processes in puppet and the differences between -> and ~>.

    Package['tomcat'] -> File['server.xml']  ~> Service['tomcat']
    

    In your case, you apply the change in tomcat config file, and puppet will restart the tomcat services automatically.

    For your reference, here is the copy-paste from Introduction to Puppet blog to explain what's the meaning of idempotency:

    One big difference between Puppet and most other tools is that Puppet configurations are idempotent, meaning they can safely be run multiple times. Once you develop your configuration, your machines will apply the configuration often — by default, every 30 minutes — and Puppet will only make any changes to the system if the system state does not match the configured state.

    Update 2016:

    Here another official Puppet blog post on idempotency: https://puppet.com/blog/idempotence-not-just-a-big-and-scary-word