Score:0

Puppet - can I selectively notify a Service?

jp flag

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?

Score:2
us flag

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

bdetweiler avatar
jp flag
My Puppet file was a little too complicated to accomplish this, so I ended up creating a separate class for the config changes, then using the chain operator to call `-> Class['my_configs']` after the RPM install. Now I can choose to only update the configs using `puppet agent -t --tags my_configs`, or I can do the whole RPM + configs install.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.