0

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?

bdetweiler
  • 137
  • 5

1 Answers1

2

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

Peter Souter
  • 651
  • 1
  • 4
  • 13
  • 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. – bdetweiler Aug 05 '21 at 11:55