21

how can i ensure that if new version of configuration file is downloaded via puppet from master repository to one of managed servers relevant service is restarted.

typical scenario - let's say there is new munin or apache config. puppet client discovers it, overwrites local files... and... - how to make sure service is restarted / reloaded ?

thanks a lot!

pQd
  • 29,561
  • 5
  • 64
  • 106

5 Answers5

24

An alternative to notify is subscribe:

file { "/etc/sshd_config":
    source => "....",
}

service { sshd:
    ensure => running,
    subscribe => File["/etc/sshd_config"],
}

The difference being that the relationship is described from the other end. For example, you might make apache subscribe to /etc/apache/httpd.conf, but you'd make a vhost file notify apache, as your apache class won't know about every vhost you have.

A similar dual-ended situation applies to require and before. It's just a matter of which makes more sense in the particular situation.

As Chad mentioned, if you find puppet constantly trying to start your service, then you need to add a pattern parameter, which is a regex to apply against the list of processes. By default puppet will do a stop and start to restart a service. If you add "hasrestart => true", then it will use the command specified in the "restart" parameter to restart the service.

David Pashley
  • 23,151
  • 2
  • 41
  • 71
  • what if you wanted to group up the notifications? i.e. avoid a restart for each change and only restart if there haven't been changes in the last X seconds? – djuarez Mar 03 '21 at 16:15
22

it seems i've found something:

file { "/etc/sshd_config":
    source => "....",
    notify => Service[sshd]
}

service { sshd:
    ensure => running
}

we'll see how that will work. anyway your thoughts on the subject are welcome.

pQd
  • 29,561
  • 5
  • 64
  • 106
  • 1
    Yes. You can find the details in the Puppet Type Reference under "Metaparameters" (http://reductivelabs.com/trac/puppet/wiki/TypeReference#metaparameters) – Chad Huneycutt Jul 13 '09 at 21:44
  • 1
    Oh, and depending on your OS, you may have to play with the hasstatus, hasrestart, and/or pattern parameters of the service type. – Chad Huneycutt Jul 13 '09 at 21:47
2

(I know this is a super old question, but just thought I'd put in my two cents with an (in my opinion) much easier way to do it)

Feel free to use arrow notation as well:

file { "/etc/sshd_config":
  source => "....",
} ~>
service { sshd:
  ensure => running
}

or

File['/etc/sshd_config'] ~> Service['sshd']
Ethan Brouwer
  • 225
  • 2
  • 9
1

This works for Solaris 10 :)

class sun_cron_root {
    file { "/var/spool/cron/crontabs/root" :
            source => "puppet:///files/cron/sun/sun_cron_root"
            }

    service {
            "cron":
            provider => "smf",
            ensure => running,
            enable => true,
            hasrestart => true,
            subscribe => File["/var/spool/cron/crontabs/root"]
            }

}
CMag
  • 687
  • 2
  • 11
  • 31
0

There are multiple equivalent notations:

Notify:

file { '/etc/sshd_config':
    notify => Service[sshd],
}

service { sshd:
    ensure => running
}

Subscribe:

file { '/etc/sshd_config':
   ...
}

service { sshd:
    ensure => running,
    subscribe => File['/etc/sshd_config'],
}

Arrow notation:

File['/etc/sshd_config'] ~> Service['sshd']

Chaining declarations

file { '/etc/sshd_config':
   ...
}
~> service { sshd:
    ensure => running,
}

If you want to trigger reload instead of restart, adjust service declaration:

service { sshd:
    ensure => running,
    restart => 'pkill -HUP sshd', # if service support such reload
}
Tombart
  • 2,013
  • 3
  • 27
  • 47