Puppet - can I selectively notify a Service?
I have a Puppet script that handles things differently in different environments based on an if/else
block. But I have a bunch of common file resource blocks at the bottom that apply to all environments. Currently, those blocks notify => Service['my-service']
, but for production, I want it to not notify. I only want it to update the files, not start or stop any services.
My initial idea is, can I store the Service into a variable and set it in each section?
Example:
if ($env == 'dev') {
$myService = Service['my-service']
} elsif ($env == 'prod') {
$myService = Service['dummy-service']
}
file { "myfile.xml":
ensure => file,
content =>
template("mytemplate.erb"),
require => Package['my-service'],
notify => $myService
}
I'm not sure if that will work or not, but if it does, what could I use for a dummy service?
Solution 1:
Yes, this is possible, and your code is pretty close to the correct solution:
if ($env == 'dev') {
$my_service = 'my-service'
} elsif ($env == 'prod') {
$my_service = 'dummy-service'
}
file { "myfile.xml":
ensure => file,
content => template("mytemplate.erb"),
require => Package['my-service'],
notify => Service[$my_service]
}
However, since your request is to not notify at all on a certain environment, a better way would be to do it this way rather than notify a dummy service:
if ($env == 'dev') {
File['myfile.xml'] ~> Service['my-service']
}
These are called chaining arrows: https://puppet.com/docs/puppet/7/lang_relationships.html#lang_rel_chaining_arrows