{"id":31864,"date":"2026-12-01T22:04:22","date_gmt":"2026-12-01T22:04:22","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=31864"},"modified":"2026-12-01T22:04:22","modified_gmt":"2026-12-01T22:04:22","slug":"chef-tutorials-how-to-set-conditions-in-chef-cookbooks-recipe-using-guard","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/chef-tutorials-how-to-set-conditions-in-chef-cookbooks-recipe-using-guard\/","title":{"rendered":"Chef Tutorials: How to set Conditions in Chef Cookbooks recipe using Guard?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Chef guards are the <code>not_if<\/code> and <code>only_if<\/code> properties, which decide during the converge phase whether a resource runs. A guard can be a shell command string or a Ruby block. This page explains how guards differ from plain Ruby <code>if<\/code> statements and gives a recipe you can run. It also covers <code>guard_interpreter<\/code> and fixes several mistakes in the gist examples above.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Guards<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A guard property can be used to evaluate the state of a node during the execution phase of a Chef Infra Client run. Based on the results of this evaluation, a guard property is then used to tell Chef Infra Client if it should continue executing a resource. A guard property accepts either a string value or a Ruby block value:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A string is executed as a shell command. If the command returns 0, the guard is applied. If the command returns any other value, then the guard property is not applied.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">String guards in a powershell_script run Windows PowerShell commands and may return true in addition to 0.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A block is executed as Ruby code that must return either true or false. If the block returns true, the guard property is applied. If the block returns false, the guard property is not applied.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"904\" height=\"652\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-24.png\" alt=\"Chef docs on guard properties: not_if prevents and only_if allows a resource when condition is true\" class=\"wp-image-31865\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-24.png 904w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-24-300x216.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-24-768x554.png 768w\" sizes=\"auto, (max-width: 904px) 100vw, 904px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"709\" height=\"799\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-25.png\" alt=\"Chef docs listing guard arguments :user, :group, :environment, :cwd and :timeout with not_if examples\" class=\"wp-image-31866\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-25.png 709w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/11\/image-25-266x300.png 266w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/50a1b90ba5e423a21479b25cee141a93.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Guards versus Ruby if statements<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A Chef Infra Client run has two phases. In the <strong>compile phase<\/strong>, Chef reads every recipe and builds a list of resources called the resource collection. In the <strong>converge phase<\/strong>, it works through that list and applies each resource. The examples above mix two kinds of conditions, and each kind runs in a different phase.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Technique<\/th><th>Evaluated when<\/th><th>Effect<\/th><\/tr><\/thead><tbody><tr><td>Ruby <code>if<\/code> \/ <code>unless<\/code> \/ <code>case<\/code> around a resource<\/td><td>Compile phase<\/td><td>The resource is either added to the resource collection or never exists<\/td><\/tr><tr><td><code>include_recipe ... if condition<\/code><\/td><td>Compile phase<\/td><td>The whole recipe is loaded or not<\/td><\/tr><tr><td><code>not_if<\/code> \/ <code>only_if<\/code> guard<\/td><td>Converge phase, just before the resource runs<\/td><td>The resource exists but is skipped, and the log shows <code>(skipped due to not_if)<\/code> or <code>(skipped due to only_if)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Use a Ruby <code>if<\/code> for facts that are known before anything changes, such as the platform, the platform version or the value of an attribute. Use a guard when the condition depends on system state that an earlier resource in the same run may change, such as a file, a user or a running service. A Ruby <code>if<\/code> that checks for a file created earlier in the same run is evaluated before that file exists. That is a common cause of resources that only work on the second run.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">not_if and only_if syntax at a glance<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>not_if<\/code>: the resource is <strong>skipped<\/strong> if the condition is true (block) or if the command exits with <code>0<\/code> (string).<\/li>\n\n\n<li><code>only_if<\/code>: the resource <strong>runs only<\/strong> if the condition is true (block) or if the command exits with <code>0<\/code> (string).<\/li>\n\n\n<li>Block guards are Ruby. Any truthy value counts as true, and <code>nil<\/code> or <code>false<\/code> counts as false.<\/li>\n\n\n<li>String guards run as shell commands. They cost a process fork, so prefer a Ruby block when the check is simple, such as testing whether a file exists.<\/li>\n\n\n<li>One resource can have several guards. It runs only when every <code>only_if<\/code> passes and no <code>not_if<\/code> matches.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">String guards accept options. The examples above use the older hash-rocket syntax (<code>:user =&gt; &#039;adam&#039;<\/code>), which still works. Modern cookbooks normally use Ruby keyword-style hashes:<\/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\">not_if <span class=\"hljs-string\">'grep adam \/etc\/passwd'<\/span>, user: <span class=\"hljs-string\">'adam'<\/span>\nnot_if <span class=\"hljs-string\">'grep adam \/etc\/passwd'<\/span>, group: <span class=\"hljs-string\">'adam'<\/span>\nnot_if <span class=\"hljs-string\">'grep adam \/etc\/passwd'<\/span>, environment: { <span class=\"hljs-string\">'HOME'<\/span> =&gt; <span class=\"hljs-string\">'\/home\/adam'<\/span> }\nnot_if <span class=\"hljs-string\">'grep adam passwd'<\/span>, cwd: <span class=\"hljs-string\">'\/etc'<\/span>\nnot_if <span class=\"hljs-string\">'sleep 10000'<\/span>, timeout: <span class=\"hljs-number\">10<\/span><\/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<h2 class=\"wp-block-heading\">A complete recipe you can run<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The cookbook below uses both kinds of condition. Create it with Chef Workstation by running <code>chef generate cookbook cookbooks\/guards_demo<\/code>, then replace <code>recipes\/default.rb<\/code> with the following:<\/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\"><span class=\"hljs-comment\"># cookbooks\/guards_demo\/recipes\/default.rb<\/span>\n\n<span class=\"hljs-comment\"># Compile-time decision: the resource only exists on Debian-family nodes<\/span>\n<span class=\"hljs-keyword\">if<\/span> platform_family?(<span class=\"hljs-string\">'debian'<\/span>)\n  apt_update <span class=\"hljs-string\">'refresh apt cache'<\/span> <span class=\"hljs-keyword\">do<\/span>\n    frequency <span class=\"hljs-number\">86<\/span>_400\n    action :periodic\n  end\nend\n\npackage <span class=\"hljs-string\">'tar'<\/span>\n\ndirectory <span class=\"hljs-string\">'\/opt\/app'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  mode <span class=\"hljs-string\">'0755'<\/span>\nend\n\n<span class=\"hljs-comment\"># Ruby block guard, evaluated at converge time<\/span>\nfile <span class=\"hljs-string\">'\/tmp\/guards-demo.txt'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  content <span class=\"hljs-string\">\"managed by Chef\\n\"<\/span>\n  mode <span class=\"hljs-string\">'0644'<\/span>\n  not_if { ::File.exist?(<span class=\"hljs-string\">'\/tmp\/guards-demo.lock'<\/span>) }\nend\n\n<span class=\"hljs-comment\"># Two guards: the archive must exist AND the app must not already be extracted<\/span>\nexecute <span class=\"hljs-string\">'extract release'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  command <span class=\"hljs-string\">'tar -xzf \/tmp\/app.tar.gz -C \/opt\/app'<\/span>\n  only_if { ::File.exist?(<span class=\"hljs-string\">'\/tmp\/app.tar.gz'<\/span>) }\n  not_if  { ::File.exist?(<span class=\"hljs-string\">'\/opt\/app\/VERSION'<\/span>) }\nend\n\n<span class=\"hljs-comment\"># String guard: getent exits 0 if the user exists, so the command is skipped<\/span>\nexecute <span class=\"hljs-string\">'create demo user'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  command <span class=\"hljs-string\">'useradd --system --no-create-home demoapp'<\/span>\n  not_if <span class=\"hljs-string\">'getent passwd demoapp'<\/span>\nend<\/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<p class=\"wp-block-paragraph\">Run it in local mode from the directory that contains <code>cookbooks\/<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">sudo chef-client -z --chef-license accept -o <span class=\"hljs-string\">'recipe&#91;guards_demo]'<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><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\">If <code>\/tmp\/app.tar.gz<\/code> does not exist, the output includes lines like these:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">  * file&#91;<span class=\"hljs-regexp\">\/tmp\/gu<\/span>ards-demo.txt] action create\n    - create <span class=\"hljs-keyword\">new<\/span> file \/tmp\/guards-demo.txt\n  * execute&#91;extract release] action run (skipped due to only_if)\n  * execute&#91;create demo user] action run\n    - execute useradd --system --no-create-home demoapp<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><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\">Run the same command again. This time <code>create demo user<\/code> reports <code>(skipped due to not_if)<\/code>. That shows the guard is making an <code>execute<\/code> resource idempotent, meaning it only makes a change when one is needed. In a real cookbook, use the built-in <code>user<\/code> resource for this task. The <code>execute<\/code> example is only here to show how a string guard works.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">guard_interpreter and inherited properties<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By default, a string guard runs as a plain shell command. It does not see the <code>environment<\/code>, <code>user<\/code> or other properties you set on the resource. That is why the first <code>bash &quot;some_app&quot;<\/code> example above fails: <code>JAVA_HOME<\/code> is set for <code>code<\/code> but not for the <code>not_if<\/code> command.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Setting <code>guard_interpreter<\/code> to the resource&#039;s own type (for example <code>:bash<\/code>) runs the guard through that interpreter. The guard then inherits properties such as <code>cwd<\/code>, <code>environment<\/code>, <code>user<\/code>, <code>group<\/code> and <code>umask<\/code> from the parent resource. <code>powershell_script<\/code> already defaults to <code>:powershell_script<\/code>, so its guards inherit these properties and can return a PowerShell boolean as well as an exit code.<\/p>\n\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\">bash <span class=\"hljs-string\">'some_app'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  guard_interpreter :bash\n  environment(<span class=\"hljs-string\">'JAVA_HOME'<\/span> =&gt; <span class=\"hljs-string\">'\/usr\/java\/default'<\/span>)\n  code <span class=\"hljs-string\">'java \/apps\/some_app\/app start'<\/span>\n  not_if <span class=\"hljs-string\">'java \/apps\/some_app\/app status'<\/span>   <span class=\"hljs-comment\"># also sees JAVA_HOME<\/span>\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\"><strong>Note:<\/strong> <code>guard_interpreter<\/code> only affects <strong>string<\/strong> guards. Block guards are always Ruby and run inside the Chef Infra Client process.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Corrected versions of the gist examples<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The gist above mixes code with notes that are not commented out, so it cannot run as one file. The examples below fix the specific errors in it.<\/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\"><span class=\"hljs-comment\"># 1. environment { ... } is parsed as a Ruby block, not a Hash -&gt; syntax error.<\/span>\n<span class=\"hljs-comment\">#    Use parentheses (or drop the braces).<\/span>\nbash <span class=\"hljs-string\">'some_app'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  environment(<span class=\"hljs-string\">'JAVA_HOME'<\/span> =&gt; <span class=\"hljs-string\">'\/usr\/java\/default'<\/span>)\n  code <span class=\"hljs-string\">'java \/apps\/some_app\/app start'<\/span>\n  not_if <span class=\"hljs-string\">'java \/apps\/some_app\/app status'<\/span>, environment: { <span class=\"hljs-string\">'JAVA_HOME'<\/span> =&gt; <span class=\"hljs-string\">'\/usr\/java\/default'<\/span> }\nend\n\n<span class=\"hljs-comment\"># 2. The comment said \"only on RHEL 6.x\", but not_if { rhel &amp;&amp; &lt; 6.0 } also<\/span>\n<span class=\"hljs-comment\">#    installs on every non-RHEL node, and &gt;= 6.0 matches RHEL 7, 8, 9...<\/span>\npackage <span class=\"hljs-string\">'package_name'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  only_if { platform_family?(<span class=\"hljs-string\">'rhel'<\/span>) &amp;&amp; node&#91;<span class=\"hljs-string\">'platform_version'<\/span>].to_i == <span class=\"hljs-number\">6<\/span> }\nend\n\n<span class=\"hljs-comment\"># 3. include_recipe is a method, not a resource: a do...end block with<\/span>\n<span class=\"hljs-comment\">#    only_if is ignored, and '=' assigns instead of comparing.<\/span>\ninclude_recipe <span class=\"hljs-string\">'postfix::server'<\/span> <span class=\"hljs-keyword\">if<\/span> node&#91;<span class=\"hljs-string\">'defaults'<\/span>]&#91;<span class=\"hljs-string\">'postfix_server'<\/span>]\n\n<span class=\"hljs-comment\"># 4. Prefer built-in resources over execute for package work.<\/span>\napt_update <span class=\"hljs-string\">'update'<\/span> <span class=\"hljs-keyword\">if<\/span> platform_family?(<span class=\"hljs-string\">'debian'<\/span>)\npackage <span class=\"hljs-string\">'git'<\/span> <span class=\"hljs-keyword\">if<\/span> platform_family?(<span class=\"hljs-string\">'rhel'<\/span>)\n\n<span class=\"hljs-comment\"># 5. Guard an apt_package with a positive platform check.<\/span>\npackage <span class=\"hljs-string\">'apache2'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  only_if { platform_family?(<span class=\"hljs-string\">'debian'<\/span>) }\nend\n\n<span class=\"hljs-comment\"># 6. String#include? does substring matching; use the helper instead.<\/span>\n<span class=\"hljs-keyword\">if<\/span> platform_family?(<span class=\"hljs-string\">'rhel'<\/span>)\n  <span class=\"hljs-comment\"># ...<\/span>\nend<\/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<h2 class=\"wp-block-heading\">Common mistakes and how to fix them<\/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>Resource runs every time despite <code>not_if<\/code><\/td><td>The string guard depends on variables or a working directory that it does not receive<\/td><td>Pass <code>environment:<\/code> or <code>cwd:<\/code> to the guard, or set <code>guard_interpreter<\/code><\/td><\/tr><tr><td>Resource only works on the second run<\/td><td>A Ruby <code>if<\/code> checked state that an earlier resource creates in the same run<\/td><td>Move the check into an <code>only_if<\/code> or <code>not_if<\/code> block<\/td><\/tr><tr><td><code>only_if<\/code> on <code>include_recipe<\/code> has no effect<\/td><td><code>include_recipe<\/code> does not accept guards<\/td><td>Wrap the call in a Ruby <code>if<\/code><\/td><\/tr><tr><td><code>NameError<\/code> or odd behaviour with <code>File.exist?<\/code> in custom resources<\/td><td><code>File<\/code> can resolve to a Chef class inside resource code<\/td><td>Use <code>::File.exist?<\/code> to reference Ruby&#039;s top-level <code>File<\/code> class<\/td><\/tr><tr><td>Guard is always true<\/td><td>Assignment (<code>=<\/code>) used instead of comparison (<code>==<\/code>)<\/td><td>Use <code>==<\/code> or test the attribute value directly<\/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>Prefer idempotent built-in resources (<code>package<\/code>, <code>user<\/code>, <code>service<\/code>, <code>apt_update<\/code>) to <code>execute<\/code>. They already check state themselves, so you need fewer guards.<\/li>\n\n\n<li>For <code>execute<\/code>, use the <code>creates<\/code> property when the only check is whether a file exists: <code>creates &#039;\/opt\/app\/VERSION&#039;<\/code>.<\/li>\n\n\n<li>Prefer Ruby block guards to shell strings for simple checks. They are faster and do not depend on the shell or the <code>PATH<\/code>.<\/li>\n\n\n<li>Use the platform helpers (<code>platform?<\/code>, <code>platform_family?<\/code>) and string attribute keys such as <code>node[&#039;platform&#039;]<\/code> rather than comparing raw strings or using symbol keys.<\/li>\n\n\n<li>Keep guards read-only. A guard command that changes the system makes the run unpredictable.<\/li>\n\n\n<li>Retire conditions for end-of-life platforms such as RHEL 6, and outdated package names such as <code>php5<\/code>, when you maintain older cookbooks.<\/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\">What is the difference between not_if and only_if in Chef?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>not_if<\/code> skips the resource when its condition is true or its command exits with 0. <code>only_if<\/code> runs the resource only when its condition is true or its command exits with 0. Each is the logical opposite of the other.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can a Chef resource have both not_if and only_if?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. A resource can have several guards of either type. It runs only when every <code>only_if<\/code> passes and none of the <code>not_if<\/code> conditions match.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should I use a Ruby if statement or a guard in a Chef recipe?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use a Ruby <code>if<\/code> for facts known at compile time, such as the platform or attribute values. Use a guard when the condition depends on system state that may change during the run, because guards are evaluated at converge time.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does my string guard not see the environment variables set on the resource?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By default, string guards run as separate shell commands and do not inherit resource properties. Pass <code>environment:<\/code> to the guard, or set <code>guard_interpreter<\/code> to the resource type, such as <code>:bash<\/code>, so the guard inherits <code>environment<\/code>, <code>cwd<\/code>, <code>user<\/code> and similar properties.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use only_if with include_recipe?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. <code>include_recipe<\/code> is a method, not a resource, so guard properties have no effect on it. Use a Ruby condition such as <code>include_recipe &#039;postfix::server&#039; if node[&#039;defaults&#039;][&#039;postfix_server&#039;]<\/code>.<\/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>Chef guards are the not_if and only_if properties, which decide during the converge phase whether a resource runs. A guard can be a shell command string or&#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-31864","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\/31864","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=31864"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31864\/revisions"}],"predecessor-version":[{"id":80490,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31864\/revisions\/80490"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=31864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=31864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=31864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}