{"id":31859,"date":"2026-12-01T22:04:21","date_gmt":"2026-12-01T22:04:21","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=31859"},"modified":"2026-12-01T22:04:21","modified_gmt":"2026-12-01T22:04:21","slug":"chef-tutorials-template-resource-with-example","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/chef-tutorials-template-resource-with-example\/","title":{"rendered":"Chef Tutorials: template Resource with example"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The Chef <code>template<\/code> resource renders an Embedded Ruby (ERB) file from a cookbook&#039;s <code>templates\/<\/code> directory and writes the result to a path on the node. This page explains how rendering, <code>variables<\/code> and node attributes fit together. It also gives a corrected, runnable version of the index.html and sudoers examples above, plus the properties, ERB tags and errors you will run into most often.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What is cookbook template (.erb)<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A cookbook template is an Embedded Ruby (ERB) template that is used to dynamically generate static text files. Templates may contain Ruby expressions and statements, and are a great way to manage configuration files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the template resource to add cookbook templates to recipes; place the corresponding Embedded Ruby (ERB) template file in a cookbook\u2019s \/templates directory. template resource would not work with chef-apply but can be used with chef-solo.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the template resource to manage the contents of a file using an Embedded Ruby (ERB) template by transferring files from a sub-directory of COOKBOOK_NAME\/templates\/ to a specified path located on a host that is running Chef Infra Client.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ERB Specification<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Chef uses Erubis as its template language. It allows embedding pure Ruby code inside special symbols in the templates.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"93\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-23-1024x93.png\" alt=\"ERB tags explained: &lt;%= %&gt; prints a value, &lt;%- %&gt; embeds Ruby logic in Chef templates\" class=\"wp-image-31860\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-23-1024x93.png 1024w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-23-300x27.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-23-768x70.png 768w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-23-1536x140.png 1536w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-23.png 1792w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/56f994e6e6f989bda42fc403edb7472f.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Reference<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>https:\/\/docs.chef.io\/templates\/<\/li>\n\n\n\n<li>https:\/\/github.com\/scmgalaxy\/chef-repo<\/li>\n\n\n\n<li>https:\/\/docs.chef.io\/resources\/template\/<\/li>\n\n\n\n<li>https:\/\/docs.aws.amazon.com\/opsworks\/latest\/userguide\/cookbooks-101-basics-attributes.html<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How the template resource works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">When Chef Infra Client converges a <code>template<\/code> resource, it finds the <code>.erb<\/code> file named in <code>source<\/code> inside the cookbook, renders it, and compares the result with the file already on disk. It only writes the file, and only reports the resource as updated, when the content, owner, group or mode differs. That comparison makes templates idempotent, and it is the reason <code>notifies<\/code> can safely restart a service only when the configuration really changed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A template can read data in two ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Instance variables<\/strong> such as <code>@hi<\/code> or <code>@sudoers_users<\/code>. These come from the <code>variables<\/code> property in the recipe. If a variable is not passed, it is <code>nil<\/code> and no error is raised.<\/li>\n\n\n<li><strong>Node attributes<\/strong> such as <code>node[&#039;platform&#039;]<\/code>. These come from Ohai, from cookbook attribute files, or from roles and environments. They are available in every template without being passed in.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer <code>variables<\/code> for anything the template depends on. Then the recipe shows exactly what the template uses, and the same template can be reused with different data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Older Chef releases rendered templates with the Erubis library. Current Chef Infra Client releases use Erubi. The template syntax is standard ERB either way, so templates written for either engine work unchanged.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Cookbook layout and template lookup<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Chef Workstation can generate the cookbook and empty templates for you:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">mkdir -p ~<span class=\"hljs-regexp\">\/chef-repo\/<\/span>cookbooks &amp;&amp; cd ~<span class=\"hljs-regexp\">\/chef-repo\/<\/span>cookbooks\nchef generate cookbook devopsschool\nchef generate template devopsschool index.html\nchef generate template devopsschool sudoers\nchef generate attribute devopsschool <span class=\"hljs-keyword\">default<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">devopsschool\/\n\u251c\u2500\u2500 attributes\/\n\u2502   \u2514\u2500\u2500 <span class=\"hljs-keyword\">default<\/span>.rb\n\u251c\u2500\u2500 recipes\/\n\u2502   \u2514\u2500\u2500 <span class=\"hljs-keyword\">default<\/span>.rb\n\u251c\u2500\u2500 templates\/\n\u2502   \u251c\u2500\u2500 index.html.erb\n\u2502   \u2514\u2500\u2500 sudoers.erb\n\u2514\u2500\u2500 metadata.rb<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Both <code>templates\/index.html.erb<\/code> and the older <code>templates\/default\/sudoers.erb<\/code> layout used in the example above work. Chef also searches host-specific and platform-specific subfolders such as <code>templates\/host-web01\/<\/code> and <code>templates\/ubuntu-22.04\/<\/code>, and it falls back to <code>templates\/default\/<\/code> and then <code>templates\/<\/code>. New cookbooks normally place files directly in <code>templates\/<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Complete working example (corrected)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The gist above defines <code>elasticsearch<\/code> attributes but the template loops over <code>learn_chef_httpd<\/code>, and it reads <code>node[&#039;devopsschool&#039;][&#039;message&#039;]<\/code> without defining it. The files below are consistent with each other, so the recipe converges without errors.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># attributes\/default.rb<\/span>\n<span class=\"hljs-keyword\">default<\/span>&#91;<span class=\"hljs-string\">'devopsschool'<\/span>]&#91;<span class=\"hljs-string\">'message'<\/span>] = <span class=\"hljs-string\">'Hello from the devopsschool cookbook'<\/span>\n\n<span class=\"hljs-keyword\">default<\/span>&#91;<span class=\"hljs-string\">'elasticsearch'<\/span>]&#91;<span class=\"hljs-string\">'default'<\/span>]&#91;<span class=\"hljs-string\">'ES_USER'<\/span>]           = <span class=\"hljs-string\">''<\/span>\n<span class=\"hljs-keyword\">default<\/span>&#91;<span class=\"hljs-string\">'elasticsearch'<\/span>]&#91;<span class=\"hljs-string\">'default'<\/span>]&#91;<span class=\"hljs-string\">'MAX_LOCKED_MEMORY'<\/span>] = <span class=\"hljs-string\">'unlimited'<\/span>\n<span class=\"hljs-keyword\">default<\/span>&#91;<span class=\"hljs-string\">'elasticsearch'<\/span>]&#91;<span class=\"hljs-string\">'default'<\/span>]&#91;<span class=\"hljs-string\">'LOG_DIR'<\/span>]           = <span class=\"hljs-string\">'\/var\/log\/elasticsearch'<\/span>\n<span class=\"hljs-keyword\">default<\/span>&#91;<span class=\"hljs-string\">'elasticsearch'<\/span>]&#91;<span class=\"hljs-string\">'default'<\/span>]&#91;<span class=\"hljs-string\">'DATA_DIR'<\/span>]          = <span class=\"hljs-string\">'\/var\/lib\/elasticsearch'<\/span>\n<span class=\"hljs-keyword\">default<\/span>&#91;<span class=\"hljs-string\">'elasticsearch'<\/span>]&#91;<span class=\"hljs-string\">'default'<\/span>]&#91;<span class=\"hljs-string\">'CONF_FILE'<\/span>]         = <span class=\"hljs-string\">'\/etc\/elasticsearch\/elasticsearch.yml'<\/span>\n\n<span class=\"hljs-keyword\">default<\/span>&#91;<span class=\"hljs-string\">'authorization'<\/span>]&#91;<span class=\"hljs-string\">'sudo'<\/span>]&#91;<span class=\"hljs-string\">'groups'<\/span>] = %w(sysadmin wheel admin)\n<span class=\"hljs-keyword\">default<\/span>&#91;<span class=\"hljs-string\">'authorization'<\/span>]&#91;<span class=\"hljs-string\">'sudo'<\/span>]&#91;<span class=\"hljs-string\">'users'<\/span>]  = %w(jerry greg)\n<span class=\"hljs-keyword\">default<\/span>&#91;<span class=\"hljs-string\">'authorization'<\/span>]&#91;<span class=\"hljs-string\">'sudo'<\/span>]&#91;<span class=\"hljs-string\">'passwordless'<\/span>] = <span class=\"hljs-keyword\">false<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"><span class=\"hljs-comment\">&lt;!-- templates\/index.html.erb --&gt;<\/span>\n<span class=\"hljs-comment\">&lt;!-- Managed by Chef. Local changes will be overwritten. --&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Platform (Ohai attribute): <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%=<\/span> <span class=\"hljs-attr\">node<\/span>&#91;'<span class=\"hljs-attr\">platform<\/span>'] %&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span>&gt;<\/span>Message (cookbook attribute): <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%=<\/span> <span class=\"hljs-attr\">node<\/span>&#91;'<span class=\"hljs-attr\">devopsschool<\/span>']&#91;'<span class=\"hljs-attr\">message<\/span>'] %&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">pre<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%<\/span> <span class=\"hljs-attr\">node<\/span>&#91;'<span class=\"hljs-attr\">elasticsearch<\/span>']&#91;'<span class=\"hljs-attr\">default<\/span>']<span class=\"hljs-attr\">.each<\/span> <span class=\"hljs-attr\">do<\/span> |<span class=\"hljs-attr\">key<\/span>, <span class=\"hljs-attr\">value<\/span>| <span class=\"hljs-attr\">-<\/span>%&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%<\/span> <span class=\"hljs-attr\">next<\/span> <span class=\"hljs-attr\">if<\/span> <span class=\"hljs-attr\">value.to_s.empty<\/span>? <span class=\"hljs-attr\">-<\/span>%&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%=<\/span> <span class=\"hljs-attr\">key<\/span> %&gt;<\/span>=<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%=<\/span> <span class=\"hljs-attr\">value<\/span> %&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%<\/span> <span class=\"hljs-attr\">end<\/span> <span class=\"hljs-attr\">-<\/span>%&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">pre<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%<\/span> <span class=\"hljs-attr\">4.times<\/span> <span class=\"hljs-attr\">do<\/span> <span class=\"hljs-attr\">-<\/span>%&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%=<\/span> @<span class=\"hljs-attr\">hi<\/span> %&gt;<\/span>, <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%=<\/span> @<span class=\"hljs-attr\">world<\/span> %&gt;<\/span> from <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%=<\/span> @<span class=\"hljs-attr\">from<\/span> %&gt;<\/span>!<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%<\/span> <span class=\"hljs-attr\">end<\/span> <span class=\"hljs-attr\">-<\/span>%&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># recipes\/default.rb<\/span>\ndirectory <span class=\"hljs-string\">'\/var\/www\/html'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  recursive <span class=\"hljs-keyword\">true<\/span>\nend\n\ntemplate <span class=\"hljs-string\">'\/var\/www\/html\/index.html'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  source <span class=\"hljs-string\">'index.html.erb'<\/span>\n  owner  <span class=\"hljs-string\">'root'<\/span>\n  group  <span class=\"hljs-string\">'root'<\/span>\n  mode   <span class=\"hljs-string\">'0644'<\/span>            <span class=\"hljs-comment\"># web content does not need the execute bit<\/span>\n  variables(\n    hi:    <span class=\"hljs-string\">'Testing'<\/span>,\n    world: <span class=\"hljs-string\">'Welt'<\/span>,\n    from:  node&#91;<span class=\"hljs-string\">'fqdn'<\/span>]\n  )\n  action :create\nend<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">Run it in local mode from the repository root. The <code>cookbooks\/<\/code> directory must be under the current directory or one of its parents:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">cd ~\/chef-repo\nsudo chef-client --local-mode --chef-license accept --runlist <span class=\"hljs-string\">'recipe&#91;devopsschool]'<\/span>\ncat \/<span class=\"hljs-keyword\">var<\/span>\/www\/html\/index.html<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">On the first run the client reports the template as created and prints a diff. On a second run with no changes it reports <code>(up to date)<\/code>. The rendered <code>&lt;pre&gt;<\/code> block lists only the non-empty attributes: <code>MAX_LOCKED_MEMORY=unlimited<\/code>, <code>LOG_DIR=\/var\/log\/elasticsearch<\/code>, <code>DATA_DIR=\/var\/lib\/elasticsearch<\/code> and <code>CONF_FILE=\/etc\/elasticsearch\/elasticsearch.yml<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Safer sudoers template<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A broken <code>\/etc\/sudoers<\/code> can lock every user out of <code>sudo<\/code>. The version below fixes three problems in the original. Group lines now have the <code>%<\/code> prefix, which sudo requires to tell a group from a user. <code>@passwordless<\/code> is actually passed to the template. The <code>verify<\/code> property makes Chef check the rendered file with <code>visudo<\/code> before it replaces the live one.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml\"># templates\/sudoers.erb\n# Generated by Chef for <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%=<\/span> <span class=\"hljs-attr\">node<\/span>&#91;'<span class=\"hljs-attr\">fqdn<\/span>'] %&gt;<\/span>. Do not edit by hand.\nDefaults        !lecture,tty_tickets,!fqdn\n\nroot          ALL=(ALL) ALL\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%<\/span> @<span class=\"hljs-attr\">sudoers_users.each<\/span> <span class=\"hljs-attr\">do<\/span> |<span class=\"hljs-attr\">user<\/span>| <span class=\"hljs-attr\">-<\/span>%&gt;<\/span>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%=<\/span> <span class=\"hljs-attr\">user<\/span> %&gt;<\/span>   ALL=(ALL) <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%=<\/span> '<span class=\"hljs-attr\">NOPASSWD:<\/span>' <span class=\"hljs-attr\">if<\/span> @<span class=\"hljs-attr\">passwordless<\/span> %&gt;<\/span>ALL\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%<\/span> <span class=\"hljs-attr\">end<\/span> <span class=\"hljs-attr\">-<\/span>%&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%<\/span> @<span class=\"hljs-attr\">sudoers_groups.each<\/span> <span class=\"hljs-attr\">do<\/span> |<span class=\"hljs-attr\">group<\/span>| <span class=\"hljs-attr\">-<\/span>%&gt;<\/span>\n# Members of the group '<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%=<\/span> <span class=\"hljs-attr\">group<\/span> %&gt;<\/span>' may gain root privileges\n%<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%=<\/span> <span class=\"hljs-attr\">group<\/span> %&gt;<\/span>   ALL=(ALL) <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%=<\/span> '<span class=\"hljs-attr\">NOPASSWD:<\/span>' <span class=\"hljs-attr\">if<\/span> @<span class=\"hljs-attr\">passwordless<\/span> %&gt;<\/span>ALL\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">%<\/span> <span class=\"hljs-attr\">end<\/span> <span class=\"hljs-attr\">-<\/span>%&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">template <span class=\"hljs-string\">'\/etc\/sudoers'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  source <span class=\"hljs-string\">'sudoers.erb'<\/span>\n  owner  <span class=\"hljs-string\">'root'<\/span>\n  group  <span class=\"hljs-string\">'root'<\/span>\n  mode   <span class=\"hljs-string\">'0440'<\/span>\n  verify <span class=\"hljs-string\">'visudo -c -f %{path}'<\/span>   <span class=\"hljs-comment\"># reject the file if the syntax is invalid<\/span>\n  variables(\n    sudoers_users:  node&#91;<span class=\"hljs-string\">'authorization'<\/span>]&#91;<span class=\"hljs-string\">'sudo'<\/span>]&#91;<span class=\"hljs-string\">'users'<\/span>],\n    sudoers_groups: node&#91;<span class=\"hljs-string\">'authorization'<\/span>]&#91;<span class=\"hljs-string\">'sudo'<\/span>]&#91;<span class=\"hljs-string\">'groups'<\/span>],\n    passwordless:   node&#91;<span class=\"hljs-string\">'authorization'<\/span>]&#91;<span class=\"hljs-string\">'sudo'<\/span>]&#91;<span class=\"hljs-string\">'passwordless'<\/span>]\n  )\nend<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> For real systems, the built-in <code>sudo<\/code> resource is usually the better choice. It writes separate files into <code>\/etc\/sudoers.d<\/code> and does not replace the whole sudoers file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key properties and actions<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Property \/ action<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>source<\/code><\/td><td>Template file name in the cookbook. Defaults to the basename of the path plus <code>.erb<\/code>.<\/td><\/tr><tr><td><code>variables<\/code><\/td><td>Hash of values exposed as <code>@name<\/code> inside the template.<\/td><\/tr><tr><td><code>owner<\/code>, <code>group<\/code>, <code>mode<\/code><\/td><td>File ownership and permissions. Quote the mode as a string, for example <code>&#039;0644&#039;<\/code>.<\/td><\/tr><tr><td><code>cookbook<\/code><\/td><td>Load the template from a different cookbook.<\/td><\/tr><tr><td><code>local<\/code><\/td><td>Set to <code>true<\/code> to treat <code>source<\/code> as an absolute path on the node instead of a cookbook file.<\/td><\/tr><tr><td><code>verify<\/code><\/td><td>Command that must succeed on the rendered file before it is put in place. <code>%{path}<\/code> is the temporary file.<\/td><\/tr><tr><td><code>sensitive<\/code><\/td><td>Hides the diff and contents in logs. Use this for passwords and keys.<\/td><\/tr><tr><td><code>helpers<\/code><\/td><td>Adds helper methods (from a block or module) that the template can call.<\/td><\/tr><tr><td><code>action :create<\/code><\/td><td>Default. Creates or updates the file.<\/td><\/tr><tr><td><code>action :create_if_missing<\/code><\/td><td>Writes the file only if it does not exist yet.<\/td><\/tr><tr><td><code>action :delete<\/code>, <code>:touch<\/code>, <code>:nothing<\/code><\/td><td>Remove the file, update its timestamp, or do nothing unless notified.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Templates are often paired with a service restart that runs only when the file changes:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">template <span class=\"hljs-string\">'\/etc\/httpd\/conf.d\/app.conf'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  source <span class=\"hljs-string\">'app.conf.erb'<\/span>\n  <span class=\"hljs-attr\">notifies<\/span> :restart, <span class=\"hljs-string\">'service&#91;httpd]'<\/span>, :delayed\nend\n\nservice <span class=\"hljs-string\">'httpd'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  action &#91;:enable, :start]\nend<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">ERB tag reference<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Tag<\/th><th>Meaning<\/th><\/tr><\/thead><tbody><tr><td><code>&lt;% code %&gt;<\/code><\/td><td>Runs Ruby code without printing anything (loops, conditionals).<\/td><\/tr><tr><td><code>&lt;%= expr %&gt;<\/code><\/td><td>Prints the result of the expression.<\/td><\/tr><tr><td><code>&lt;% code -%&gt;<\/code><\/td><td>Removes the newline after the tag. Without it, loops leave blank lines.<\/td><\/tr><tr><td><code>&lt;%- code %&gt;<\/code><\/td><td>Removes leading whitespace before the tag.<\/td><\/tr><tr><td><code>&lt;%# comment %&gt;<\/code><\/td><td>ERB comment. It does not appear in the output.<\/td><\/tr><tr><td><code>&lt;%% ... %%&gt;<\/code><\/td><td>Prints a literal <code>&lt;%<\/code> and <code>%&gt;<\/code>, which is useful when the target file is itself a template.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Using templates with chef-apply<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The original says templates do not work with <code>chef-apply<\/code>. That is only true for cookbook templates, because <code>chef-apply<\/code> runs a single recipe file with no cookbook to search. You can still render a template that sits on disk by setting <code>local true<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># motd.rb  -- run with: sudo chef-apply motd.rb<\/span>\ntemplate <span class=\"hljs-string\">'\/etc\/motd'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  source <span class=\"hljs-string\">'\/tmp\/motd.erb'<\/span>   <span class=\"hljs-comment\"># absolute path on the node<\/span>\n  local  <span class=\"hljs-keyword\">true<\/span>\n  variables(team: <span class=\"hljs-string\">'DevOps'<\/span>)\nend<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Common errors and fixes<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Error<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td><code>Chef::Exceptions::FileNotFound<\/code> &#8230; does not contain a template<\/td><td>The <code>source<\/code> name or file location is wrong, or a cookbook template was used with <code>chef-apply<\/code>.<\/td><td>Put the file in <code>templates\/<\/code>, check the <code>.erb<\/code> name, or use <code>local true<\/code>.<\/td><\/tr><tr><td><code>undefined method &#039;[]&#039; for nil<\/code><\/td><td>The template reads an attribute key that is not defined, such as <code>node[&#039;learn_chef_httpd&#039;]<\/code>.<\/td><td>Define the attribute in <code>attributes\/default.rb<\/code> or fix the key name.<\/td><\/tr><tr><td><code>undefined method &#039;each&#039; for nil<\/code><\/td><td>An <code>@variable<\/code> was not passed in <code>variables<\/code>.<\/td><td>Add it to <code>variables<\/code>, or guard with <code>Array(@var).each<\/code>.<\/td><\/tr><tr><td>Verification failure on converge<\/td><td>The <code>verify<\/code> command rejected the rendered file.<\/td><td>Run the command manually on the rendered output and fix the template.<\/td><\/tr><tr><td>Unexpected blank lines in output<\/td><td><code>&lt;% end %&gt;<\/code> was used instead of <code>&lt;% end -%&gt;<\/code>.<\/td><td>Add <code>-%&gt;<\/code> to statement tags.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Corrections to the original example<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>index.html.erb<\/code> looped over <code>node[&#039;learn_chef_httpd&#039;][&#039;default&#039;]<\/code>, but the attribute file defines <code>node[&#039;elasticsearch&#039;][&#039;default&#039;]<\/code>. Both now use <code>elasticsearch<\/code>.<\/li>\n\n\n<li><code>node[&#039;devopsschool&#039;][&#039;message&#039;]<\/code> was read but never defined. An attribute default has been added.<\/li>\n\n\n<li><code>mode &#039;0755&#039;<\/code> on an HTML file has been changed to <code>&#039;0644&#039;<\/code>.<\/li>\n\n\n<li>The typo <code>Tesing<\/code> has been changed to <code>Testing<\/code>.<\/li>\n\n\n<li>Sudoers group lines lacked the <code>%<\/code> prefix, so sudo would read <code>wheel<\/code> and <code>admin<\/code> as user names. The prefix has been added, and the duplicated hardcoded <code>%sysadmin<\/code> line has been removed.<\/li>\n\n\n<li><code>@passwordless<\/code> was used in <code>sudoers.erb<\/code> but never passed, so <code>NOPASSWD:<\/code> could never be rendered. It is now passed through <code>variables<\/code>.<\/li>\n\n\n<li>A <code>verify &#039;visudo -c -f %{path}&#039;<\/code> check has been added so an invalid sudoers file is never installed.<\/li>\n\n\n<li>The engine is now described as Erubi, which current Chef Infra Client releases use instead of Erubis.<\/li>\n\n\n<li>The <code>chef-apply<\/code> statement has been clarified: local templates work with <code>local true<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Where should I put .erb files in a Chef cookbook?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Put them in the cookbook&#039;s <code>templates\/<\/code> directory, for example <code>templates\/index.html.erb<\/code>. The older <code>templates\/default\/<\/code> subfolder still works, and Chef also searches host-specific and platform-specific subfolders first.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between variables and node attributes in a template?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>variables<\/code> are passed by the recipe and appear as <code>@name<\/code> in the template. Node attributes are global and can be read as <code>node[&#039;key&#039;]<\/code> from any template. Passing data through <code>variables<\/code> makes templates easier to reuse and test.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use the template resource with chef-apply?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Not with cookbook templates, because <code>chef-apply<\/code> has no cookbook to search. Set <code>local true<\/code> and give an absolute <code>source<\/code> path to render a template file that is already on the node.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I restart a service only when a template changes?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add <code>notifies :restart, &#039;service[name]&#039;, :delayed<\/code> to the template resource. The template is only marked as updated when the rendered content or the file permissions change, so unchanged runs do not restart the service.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I stop secrets in a template from appearing in Chef logs?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Set <code>sensitive true<\/code> on the template resource. Chef then hides the content diff in its output and logs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/infrastructure-as-code\/chef\/\">More Chef tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/what-is-chef-and-use-cases-of-chef\/\">What is Chef and use cases of Chef?<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/chef-tutorials-knife-command-line-reference\/\">Chef Tutorials: knife command line reference<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/top-50-chef-interview-questions-and-answers\/\">Top 50 Chef interview questions and answers<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The Chef template resource renders an Embedded Ruby (ERB) file from a cookbook&#039;s templates\/ directory and writes the result to a path on the node. This page&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[88],"tags":[1659,4799],"class_list":["post-31859","post","type-post","status-publish","format-standard","hentry","category-chef","tag-chef-cookbooks","tag-chef-recipes"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31859","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=31859"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31859\/revisions"}],"predecessor-version":[{"id":80489,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31859\/revisions\/80489"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=31859"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=31859"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=31859"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}