{"id":31856,"date":"2026-12-01T22:04:20","date_gmt":"2026-12-01T22:04:20","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=31856"},"modified":"2026-12-01T22:04:20","modified_gmt":"2026-12-01T22:04:20","slug":"chef-tutorials-create-multiple-resources-in-chef-recipe","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/chef-tutorials-create-multiple-resources-in-chef-recipe\/","title":{"rendered":"Chef Tutorials: Create multiple resources in Chef recipe"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A Chef recipe is Ruby, so you can create many resources of the same type by looping over an array with <code>.each<\/code> and declaring one resource per item, as the example recipe above does for packages, users, files, directories and groups. This page explains how that pattern works, how to run and check the recipe, and how to tighten it for real servers.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/0f7271f46823fee78b4323893f0b85df.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">How the loop pattern works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>%w(tree mysql httpd)<\/code> is Ruby shorthand for the array <code>[&#039;tree&#039;, &#039;mysql&#039;, &#039;httpd&#039;]<\/code>. Calling <code>.each<\/code> on it runs the block once per element. Inside the block, <code>package p do ... end<\/code> declares a separate resource for each value. Chef compiles all of these into the resource collection before it changes anything, then runs them in the order they were declared.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ruby builds the resources at compile time. Chef applies them at converge time. Each loop iteration produces an ordinary resource such as <code>package[tree]<\/code> or <code>user[Ramesh]<\/code>, which appears in the run output and can be notified by name like any other resource.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> Keep resource names unique. If two iterations produce the same type and name, for example <code>file[\/ktfile1]<\/code> twice, you get two resources that act on the same target, and the later one can undo the earlier one.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A Linux node with Chef Infra Client or Chef Workstation installed. Workstation includes <code>chef-apply<\/code> and <code>chef-client<\/code>.<\/li>\n\n\n<li>Root or <code>sudo<\/code> access, because creating users, groups and files under <code>\/<\/code> and installing packages need root.<\/li>\n\n\n<li>A configured package repository. Package names such as <code>httpd<\/code> and <code>mysql<\/code> are specific to the distribution (see the table below).<\/li>\n\n\n<li>Outbound HTTPS access if you use the <code>remote_file<\/code> example.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Run the recipe and verify the result<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Save the example above as <code>chef.rb<\/code>. You can run a single recipe file without a cookbook or a Chef Server:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Run a standalone recipe file<\/span>\nsudo chef-apply chef.rb --chef-license accept\n\n<span class=\"hljs-comment\"># Alternative: local mode, with a dry run first<\/span>\nsudo chef-client --local-mode --why-run chef.rb --chef-license accept\nsudo chef-client --local-mode chef.rb --chef-license accept<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><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\">The output lists each resource the loops generated, for example <code>* yum_package[tree] action install<\/code> followed by <code>- install version ... of package tree<\/code>. When you run it a second time, the same resources report <code>(up to date)<\/code>. That confirms the recipe is idempotent.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">rpm -q tree httpd              <span class=\"hljs-comment\"># or: dpkg -l tree apache2 on Debian\/Ubuntu<\/span>\nid Ramesh; id Ajay; id Vinod\ngetent group ktexperts1        <span class=\"hljs-comment\"># members should list Ramesh,Ajay,Vinod<\/span>\nls -l \/ktfile1 \/ktfile2 \/ktfile3\nls -ld \/ktdir1 \/ktdir2 \/ktdir3\nls -l \/home\/ec2-user<span class=\"hljs-comment\">\/*.rpm<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><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\">Package names differ by platform<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>package<\/code> resource calls the platform&#039;s native provider (<code>yum<\/code>\/<code>dnf<\/code>, <code>apt<\/code>, and others). It does not translate package names, so <code>httpd<\/code> and <code>mysql<\/code> only work where packages with those exact names exist.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Purpose<\/th><th>RHEL \/ CentOS \/ Amazon Linux<\/th><th>Debian \/ Ubuntu<\/th><\/tr><\/thead><tbody><tr><td>Apache web server<\/td><td><code>httpd<\/code><\/td><td><code>apache2<\/code><\/td><\/tr><tr><td>MySQL-compatible client<\/td><td><code>mariadb<\/code> (or <code>mysql<\/code> where a MySQL repo or module is enabled)<\/td><td><code>mariadb-client<\/code> or <code>default-mysql-client<\/code><\/td><\/tr><tr><td>Directory tree viewer<\/td><td><code>tree<\/code><\/td><td><code>tree<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">If a name doesn&#039;t exist, the run stops with an error saying no candidate version is available. To fix it, pick the correct name per platform with <code>node[&#039;platform_family&#039;]<\/code>, as the improved recipe below does.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Improved version of the recipe<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The recipe below follows the same steps as the example above with these changes: packages install in one transaction, usernames are lowercase, home directories are created, the group is created with its members in one resource, files have content and permissions, and the download is pinned with a checksum.<\/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\"># chef.rb - create multiple resources with loops<\/span>\n\n<span class=\"hljs-comment\"># 1. Packages: the package resource accepts an array (one yum\/dnf\/apt call)<\/span>\ndb_client = node&#91;<span class=\"hljs-string\">'platform_family'<\/span>] == <span class=\"hljs-string\">'debian'<\/span> ? <span class=\"hljs-string\">'mariadb-client'<\/span> : <span class=\"hljs-string\">'mariadb'<\/span>\nweb       = node&#91;<span class=\"hljs-string\">'platform_family'<\/span>] == <span class=\"hljs-string\">'debian'<\/span> ? <span class=\"hljs-string\">'apache2'<\/span> : <span class=\"hljs-string\">'httpd'<\/span>\n\npackage &#91;<span class=\"hljs-string\">'tree'<\/span>, web, db_client] <span class=\"hljs-keyword\">do<\/span>\n  action :install\nend\n\n<span class=\"hljs-comment\"># 2. Users: lowercase names, home directories created<\/span>\nteam = %w(ramesh ajay vinod)\n\nteam.each <span class=\"hljs-keyword\">do<\/span> |u|\n  user u <span class=\"hljs-keyword\">do<\/span>\n    shell <span class=\"hljs-string\">'\/bin\/bash'<\/span>\n    manage_home <span class=\"hljs-keyword\">true<\/span>\n    action :create\n  end\nend\n\n<span class=\"hljs-comment\"># 3. Groups: the first group gets the team as members<\/span>\n%w(ktexperts1 ktexperts2 ktexperts3).each <span class=\"hljs-keyword\">do<\/span> |grp|\n  group grp <span class=\"hljs-keyword\">do<\/span>\n    members team <span class=\"hljs-keyword\">if<\/span> grp == <span class=\"hljs-string\">'ktexperts1'<\/span>\n    append <span class=\"hljs-keyword\">true<\/span>\n    action :create\n  end\nend\n\n<span class=\"hljs-comment\"># 4. Directories under a dedicated base path, not \/<\/span>\nbase = <span class=\"hljs-string\">'\/opt\/ktdemo'<\/span>\n\ndirectory base <span class=\"hljs-keyword\">do<\/span>\n  mode <span class=\"hljs-string\">'0755'<\/span>\nend\n\n%w(dir1 dir2 dir3).each <span class=\"hljs-keyword\">do<\/span> |d|\n  directory <span class=\"hljs-string\">\"#{base}\/#{d}\"<\/span> <span class=\"hljs-keyword\">do<\/span>\n    owner <span class=\"hljs-string\">'root'<\/span>\n    group <span class=\"hljs-string\">'ktexperts1'<\/span>\n    mode <span class=\"hljs-string\">'0775'<\/span>\n    action :create\n  end\nend\n\n<span class=\"hljs-comment\"># 5. Files with managed content and permissions<\/span>\n%w(file1 file2 file3).each <span class=\"hljs-keyword\">do<\/span> |f|\n  file <span class=\"hljs-string\">\"#{base}\/#{f}\"<\/span> <span class=\"hljs-keyword\">do<\/span>\n    content <span class=\"hljs-string\">\"Managed by Chef - #{f}\\n\"<\/span>\n    owner <span class=\"hljs-string\">'root'<\/span>\n    group <span class=\"hljs-string\">'ktexperts1'<\/span>\n    mode <span class=\"hljs-string\">'0644'<\/span>\n    action :create\n  end\nend\n\n<span class=\"hljs-comment\"># 6. Download a file, verified by checksum<\/span>\npkg_url    = <span class=\"hljs-string\">'https:\/\/example-mirror.internal\/chef-workstation.rpm'<\/span> <span class=\"hljs-comment\"># replace with a real URL<\/span>\npkg_sha256 = <span class=\"hljs-string\">'REPLACE_WITH_SHA256'<\/span>                                   <span class=\"hljs-comment\"># from the download page<\/span>\n\nremote_file <span class=\"hljs-string\">'\/tmp\/chef-workstation.rpm'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  source pkg_url\n  checksum pkg_sha256\n  mode <span class=\"hljs-string\">'0644'<\/span>\n  action :create\nend<\/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\n<ul class=\"wp-block-list\">\n<li><code>package [...]<\/code> with an array installs all packages in one package-manager call. This is faster and atomic. It replaces the loop where the provider supports multi-package installs (yum, dnf and apt do).<\/li>\n\n\n<li><code>manage_home true<\/code> creates <code>\/home\/&lt;user&gt;<\/code>. Without it, the user can be created without a home directory, depending on the platform defaults.<\/li>\n\n\n<li><code>group<\/code> with <code>members<\/code> and <code>append true<\/code> during <code>:create<\/code> adds the users without removing any existing members. Because the users are declared earlier in the recipe, they exist by the time the group resource runs.<\/li>\n\n\n<li><code>checksum<\/code> on <code>remote_file<\/code> takes a SHA-256. If the local file already matches it, Chef skips the download. If the downloaded content doesn&#039;t match, the run fails, so a corrupted or tampered file never reaches the node.<\/li>\n\n\n<li>To install the downloaded RPM, add <code>package &#039;chef-workstation&#039; do source &#039;\/tmp\/chef-workstation.rpm&#039; end<\/code> after the <code>remote_file<\/code>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Loop over a hash when items need different properties<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An array works when every resource is identical except for its name. When each item needs its own settings, loop over a hash instead:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">app_users = {\n  <span class=\"hljs-string\">'ramesh'<\/span> =&gt; { uid: <span class=\"hljs-number\">2001<\/span>, shell: <span class=\"hljs-string\">'\/bin\/bash'<\/span> },\n  <span class=\"hljs-string\">'ajay'<\/span>   =&gt; { uid: <span class=\"hljs-number\">2002<\/span>, shell: <span class=\"hljs-string\">'\/bin\/bash'<\/span> },\n  <span class=\"hljs-string\">'vinod'<\/span>  =&gt; { uid: <span class=\"hljs-number\">2003<\/span>, shell: <span class=\"hljs-string\">'\/sbin\/nologin'<\/span> },\n}\n\napp_users.each <span class=\"hljs-keyword\">do<\/span> |name, opts|\n  user name <span class=\"hljs-keyword\">do<\/span>\n    uid opts&#91;:uid]\n    shell opts&#91;:shell]\n    manage_home <span class=\"hljs-keyword\">true<\/span>\n  end\nend<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><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\">In a cookbook, move this data into attributes (for example <code>node[&#039;ktdemo&#039;][&#039;users&#039;]<\/code>) or a data bag. That way you can change the list per environment without editing the recipe.<\/p>\n\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>Symptom<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>No candidate version available for <code>mysql<\/code> or <code>httpd<\/code><\/td><td>The package name doesn&#039;t exist in the node&#039;s repositories<\/td><td>Use the platform-specific name (see the table above) or enable the repository first<\/td><\/tr><tr><td><code>group[ktexperts1]<\/code> with <code>:modify<\/code> fails because the group doesn&#039;t exist<\/td><td><code>:modify<\/code> only changes an existing group<\/td><td>Use <code>:create<\/code> with <code>members<\/code> and <code>append true<\/code>, or make sure the create runs first<\/td><\/tr><tr><td>Group membership fails for a user<\/td><td>The user resource is declared after the group, or the name is misspelled<\/td><td>Declare users before the group and reuse the same array variable for both<\/td><\/tr><tr><td><code>remote_file<\/code> fails with a 404 or a checksum mismatch<\/td><td>The URL is outdated or wrong, or the checksum doesn&#039;t match the file<\/td><td>Get the current URL and SHA-256 from the vendor download page<\/td><\/tr><tr><td>Permission denied or useradd errors<\/td><td>The recipe was run without root<\/td><td>Run <code>chef-apply<\/code> or <code>chef-client<\/code> with <code>sudo<\/code><\/td><\/tr><tr><td>The run stops at a license prompt<\/td><td>The Chef license wasn&#039;t accepted<\/td><td>Add <code>--chef-license accept<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Best practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use descriptive block variables (<code>pkg<\/code>, <code>user_name<\/code>, <code>dir<\/code>) instead of <code>p<\/code>. <code>p<\/code> also shadows Ruby&#039;s built-in <code>p<\/code> method.<\/li>\n\n\n<li>Don&#039;t create test files and directories directly under <code>\/<\/code>. Use a dedicated path such as <code>\/opt\/&lt;app&gt;<\/code>.<\/li>\n\n\n<li>Always set <code>owner<\/code>, <code>group<\/code> and <code>mode<\/code> on files and directories so permissions are predictable.<\/li>\n\n\n<li>Use the array form of <code>package<\/code> for bulk installs instead of a loop.<\/li>\n\n\n<li>Pin downloads with <code>checksum<\/code>, and keep URLs and versions in attributes rather than hard-coding them.<\/li>\n\n\n<li>Run with <code>--why-run<\/code> first on shared servers to preview the changes.<\/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\">How do I install multiple packages in a single Chef resource?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Pass an array to the package resource, for example <code>package %w(tree httpd git)<\/code>. On yum, dnf and apt platforms, Chef installs them in one package-manager transaction.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What does %w() mean in a Chef recipe?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It&#039;s Ruby syntax for an array of strings separated by whitespace. <code>%w(a b c)<\/code> is the same as <code>[&#039;a&#039;, &#039;b&#039;, &#039;c&#039;]<\/code>, and you can call <code>.each<\/code> on it to declare one resource per element.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does looping over resources break idempotency?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. Each iteration creates a normal Chef resource that checks the current state before acting. On a second run, every resource reports up to date unless something has changed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I add several users to a group without removing existing members?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Set <code>members<\/code> to an array of usernames and add <code>append true<\/code> on the group resource. Without <code>append true<\/code>, Chef replaces the group&#039;s member list with exactly the users you listed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How can I run a single recipe file without a cookbook?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>sudo chef-apply recipe.rb<\/code> or <code>sudo chef-client --local-mode recipe.rb<\/code>. Both run the file directly on the local node without a Chef Infra Server.<\/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>A Chef recipe is Ruby, so you can create many resources of the same type by looping over an array with .each and declaring one resource per&#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":[4799],"class_list":["post-31856","post","type-post","status-publish","format-standard","hentry","category-chef","tag-chef-recipes"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31856","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=31856"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31856\/revisions"}],"predecessor-version":[{"id":80488,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31856\/revisions\/80488"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=31856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=31856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=31856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}