When managing infrastructure with Puppet, a common requirement is ensuring services restart when their configuration files change. This is crucial for services like Apache, Nginx, or monitoring tools like Munin where new configurations should take effect immediately.
Puppet provides two primary mechanisms to handle service restarts:
# Example for Apache service
file { '/etc/apache2/apache2.conf':
ensure => file,
source => 'puppet:///modules/apache/apache2.conf',
notify => Service['apache2'], # Triggers restart when file changes
}
service { 'apache2':
ensure => running,
enable => true,
hasstatus => true,
}
You can also use the reverse relationship with subscribe:
service { 'apache2':
ensure => running,
enable => true,
subscribe => File['/etc/apache2/apache2.conf'], # Watches file changes
}
For services with multiple config files, you can chain notifications:
file { '/etc/apache2/conf.d/munin.conf':
ensure => file,
source => 'puppet:///modules/munin/apache.conf',
notify => Service['apache2'],
}
file { '/etc/munin/munin.conf':
ensure => file,
source => 'puppet:///modules/munin/munin.conf',
notify => Service['munin-node'],
}
For more complex restart requirements:
file { '/etc/example/service.conf':
ensure => file,
source => 'puppet:///modules/example/service.conf',
notify => Exec['restart_example_service'],
}
exec { 'restart_example_service':
command => '/usr/bin/systemctl restart example',
refreshonly => true, # Only runs when notified
path => '/usr/bin:/bin',
}
- Always test configuration changes in non-production environments first
- Consider using --noop flag to verify what changes Puppet will make
- For clustered services, implement rolling restarts to avoid downtime
- Document all service dependencies in your Puppet manifests
If services aren't restarting as expected:
- Verify file permissions and paths
- Check Puppet agent logs for errors
- Ensure the service resource matches the actual service name
- Test manual service restart commands work
When managing infrastructure with Puppet, a common requirement is ensuring services restart automatically when their configuration files change. This is particularly crucial for services like Apache, Nginx, or monitoring tools like Munin where new configurations shouldn't take effect until the service reloads.
The most straightforward approach uses Puppet's relationship metaparameters:
file { '/etc/apache2/apache2.conf':
ensure => file,
source => 'puppet:///modules/apache/apache2.conf',
notify => Service['apache2'], # Triggers restart when file changes
}
service { 'apache2':
ensure => running,
enable => true,
hasrestart => true,
}
File[] Pattern /h2>
Another common pattern that works well:
service { 'apache2':
ensure => running,
enable => true,
require => File['/etc/apache2/apache2.conf'],
subscribe => File['/etc/apache2/apache2.conf'],
}
For services with multiple config files, use resource collectors:
File <| tag == 'apache-config' |> {
notify => Service['apache2']
}
Some services need special handling during restarts:
service { 'nginx':
ensure => running,
enable => true,
hasrestart => false,
restart => '/usr/sbin/nginx -t && systemctl reload nginx',
subscribe => File['/etc/nginx/nginx.conf'],
}
If your service isn't restarting as expected:
- Run Puppet with --debug to see relationship triggers
- Verify the service resource's hasrestart parameter
- Check for syntax errors that might prevent restart commands
- Test with manual puppet agent --test runs
Newer Puppet versions offer better control:
file { '/etc/munin/munin.conf':
ensure => file,
source => 'puppet:///modules/munin/munin.conf',
restart_trigger => Service['munin-node'], # Explicit restart control
}