1

I'm trying to create a relationship between a service resource and two exec resources. The service should trigger the Exec['before'] when something changes and start the Exec['after'] when the service has been modified.

class foobar (
$debug = true
) {

    service { "sshd":
        ensure => 'stopped',
        notify => Exec['before']
    }

    exec { "before":
        command => "/bin/echo before",
        refreshonly => true,
    }

    exec { "after":
        refreshonly => true,
        command => "/bin/echo after",
    }

}

I've tried literally everything but the Exec['before'] keeps getting triggered after the service:

Notice: Compiled catalog for puppet.puppettest.local in environment production in 0.55 seconds
Notice: /Stage[main]/Foobar/Service[sshd]/ensure: ensure changed 'running' to 'stopped'
Notice: /Stage[main]/Foobar/Exec[before]: Triggered 'refresh' from 1 events
Notice: Applied catalog in 0.04 seconds

How can I make sure that the Exec['before'] runs before the service but only if there have been changes to the service and not with every run?

kr

Nenzo
  • 135
  • 3
  • 13
  • You cannot do this in intrinsic Puppet as far as I know. – Matthew Schuchard Nov 21 '17 at 14:21
  • 4
    The `notify` metaparameter has the semantics of the `before` metaparameter as a subset, which is the opposite of what you want. And it needs to work that way, because Puppet cannot rely on target computers to have a Flux Capacitor installed. – John Bollinger Nov 21 '17 at 23:06

1 Answers1

1

Your metaparameters are not correct. By setting ThingA to notify ThingB you are telling Puppet that ThingA must run before ThingB. Things which are refresh_only => true are going to be tricky to have execute before anything else, because they need something to have run first, in order to refresh them.

If you want your order to be Exec['before'] -> Service['sshd'] -> Exec['after'] you'll need to do something like this.

class foobar (
    $debug = true
) {

    service { "sshd":
        ensure  => 'stopped',
        notify  => Exec['after'],
        require => Exec['before'],
    }

    exec { "before":
        command => "/bin/echo before",
        onlyif  => "some command that helps determine if this should run",
    }

    exec { "after":
        command     => "/bin/echo after",
        refreshonly => true,
    }

}
floodpants
  • 133
  • 7