In the puppet, how to stop the service, perform some actions, 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 make sure that the tomcat service is up and running again. I wrote the following code for the dolls for this:

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

On the puppet complains that

'Error: Duplicate ad: Service [tomcat] is already declared in File'

How to solve this problem. What is the puppet’s recipe to stop the service, perform some action, and then return the service again?

+5
source share
2 answers

In the puppet, you cannot announce the same service again. that you have a mistake.

With a puppet, you don’t have to worry about tomcat stop / start processes. It assumes the final status (called "idemotency"). Once you determine the relationship between packages, configuration files, and services, it will complete all the tasks for you. For example, you should understand below the processes in the puppet and the differences between -> and ~> .

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

In your case, you make changes to the tomcat configuration file, and the puppet automatically restarts the tomcat services.

For your reference, here is a paste copy from Introduction to Puppet to explain what idempotency means:

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

Update 2016:

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

+5
source

This is not possible directly with Puppet, as @BMW makes the conclusion correctly. However, there are a few more comments.

There is promising work that will add limited support for the transition state declaration. However, this will not (in its current alpha state, at least) allow you to enter that state in preparation and during the application of the whole class.

A common workaround for this kind of problem is to manage the entity in question with two or more resources. The exec type is a good solution for everyone, because it can manage almost everything. The obvious drawback is that exec must be adapted to your agents (what you know - after all, there is a point for a system like Puppet ;-). Assuming the manifest will be for only one platform, it is simple:

 exec { 'stop-tomcat': command => 'service tomcat stop', onlyif => 'service tomcat status', before => [ Class['config'], Service['config'], ], } 

Ordering exec before Service['config'] is redundant (because service requires a class ), but it would be nice to say that the service resource must have the last word.

+1
source

Source: https://habr.com/ru/post/1209831/


All Articles