Q&A-3Design ordering for package, template, and service resources.
Answer
Assumptions. A declarative config-management system with a resource graph: Puppet, Chef, or Ansible. Three resource kinds: package (install a pinned version), template (render a config file), service (run/reload a daemon). Nodes are long-lived and converge repeatedly, so idempotence and minimal restarts matter.
Core answer. Model the three as a directed graph with explicit edges, never source order: package → file → service. The first edge is a plain ordering edge (require/before); the second is a refresh edge (notify/subscribe), so the service restarts only when the rendered file actually changed. Render to a temp path, validate with the target binary, then install.
Why source order fails. Puppet compiles a catalog from every class, defined type and include in the run. The order resources land in that catalog depends on class evaluation order, not the order you read them in the manifest: a file written above its package can still be applied first. Only declared edges are guaranteed.
package { 'nginx':
ensure => '1.24.0-1.el9',
}
file { '/etc/nginx/nginx.conf':
ensure => file,
owner => 'root',
mode => '0644',
content => template('nginx/nginx.conf.erb'),
require => Package['nginx'],
notify => Service['nginx'], # refresh edge, not ordering
}
service { 'nginx':
ensure => running,
enable => true,
require => Package['nginx'],
}notify on the file and subscribe on the service are the same edge spelled from either end; declare one.
Trace. Three runs against the same node:
| run | package | file | service | changed |
|---|---|---|---|---|
| first | installed | created | started | 3 |
| rerun, nothing edited | unchanged | unchanged | unchanged | 0 |
| template edited | unchanged | rewritten → notify fires | restarted | 2 |
The rerun reporting 0 changed is the whole point. A plain require between file and service would restart nginx every run.
Failure modes.
- Missing
require => Package: on a clean node the file resource fails because/etc/nginxdoes not exist yet. Detected by a converge on a fresh node, not on your dev box. - Ordering edge instead of refresh edge between file and service: change count stays above 0 on a no-op rerun; every run restarts the daemon and drops in-flight connections.
- Cycle:
A require BplusB require Afails catalog compilation with a dependency-cycle error before any change is applied. Cheap to catch, so reject cycles at compile time. - Unvalidated config: write the file, restart, and nginx exits on a syntax error, taking the service down. Render to
/tmp/nginx.conf, runnginx -t -c /tmp/nginx.conf, and replace the live file only on exit 0.
Alternatives and when to pick them.
- Declarative graph vs linear script. A one-shot bootstrap where every node is fresh can be a linear shell script: no cycle risk, no graph to reason about. Pick the graph when nodes converge repeatedly and you need no-op reruns.
- Validate-in-place vs side-by-side. Side-by-side (temp render plus
nginx -t) costs an extra file and a validation step but bounds the blast radius to a failed converge. Validate-in-place is only defensible when the service cannot be tested without the live path. - Ansible handlers dedupe by name and run at end of play;
meta: flush_handlersforces them mid-play. Batching is fine until a later task needs the restarted service, then you must flush.
Scale. Illustrative: 200 nodes, hourly converge. Restart-on-every-run is 200 × 24 = 4,800 restarts/day; refresh-only is roughly 200 on the day the template changes. At ~200 ms of dropped connections per restart, that is the difference between a routine edit and a daily blip.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.