How define twice the same service in Puppet?

2019-07-27 10:31发布

问题:

In order to deploy Varnish with a Puppet class, I need stop Varnish for move and deploy files, then at the end, ensure that Varnish is started.

My problem is simple, how I can define twice a service in a Puppet class, in order to stop or start the service at differents steps ?

class varnish::install (

    (...)

    service { "varnish":
        ensure => "stopped",
        require => Package['varnish'],
        before => Exec['mv-lib-varnish'],
    }

    (...)

    service { "varnish":
        ensure => "running",
        require => File["$varnishncsa_file"],
    }
}

I've an Duplicate definition: Service[varnish] (...) error, and it's logical...

What's the best practice to manage services in a Puppet class ? Divide in multiple classes, or there is an option for "rename" a service for declare it several times ?

回答1:

try the following to get rid of duplicate error, but what you are trying to do is wrong. Puppet brings system to certain consistent state - so telling stop service X, do some work , start service X - it out of scope of proper puppet use, puppet is more like restart service if some files on which the service depends were modified.

class varnish::install (

(...)

service { "varnish-stop":
    name => "varnish"
    ensure => "stopped",
    require => Package['varnish'],
    before => Exec['mv-lib-varnish'],
}

(...)

service { "varnish-start":
    name => "varnish"
    ensure => "running",
    require => File["$varnishncsa_file"],
}
}


回答2:

Use exec with service restart as a hook (notify) for "deploy files" action (package/another exec). Define service itself only once as running, because that is what you normally want assuring. Puppet is for describing target state.