Templates & files
ERB templates from node data.
The template resource renders a file from an ERB (Embedded Ruby) template using node attributes and variables, then places it — the same generate-config-from-data pattern as Ansible’s Jinja2 templates. You keep the template in the cookbook’s templates/ directory, pass it values, and let it produce the per-node config. Files that are static (no substitution) use the cookbook_file resource instead.
worker_processes <%= node['nginx']['worker_processes'] %>;http {server {listen <%= @port %>;server_name <%= node['fqdn'] %>;<% if @enable_tls %>ssl_certificate <%= @cert_path %>;<% end %>}}
template '/etc/nginx/nginx.conf' dosource 'nginx.conf.erb'variables(port: 8080, enable_tls: true, cert_path: '/etc/ssl/site.pem')notifies :reload, 'service[nginx]'end
Variables vs node attributes
Two ways to feed a template: read node attributes directly inside the ERB (node[‘nginx’][‘port’]), or pass explicit variables via the variables property and reference them with @. Passing variables is cleaner for values computed in the recipe and keeps the template’s inputs obvious. As with Jinja2, keep ERB logic light — substitute and lightly branch, but compute in the recipe.