{"id":31837,"date":"2026-12-01T22:04:18","date_gmt":"2026-12-01T22:04:18","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=31837"},"modified":"2026-12-01T22:04:18","modified_gmt":"2026-12-01T22:04:18","slug":"chef-tutorials-how-to-set-environment-variable-in-chef-recipe","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/chef-tutorials-how-to-set-environment-variable-in-chef-recipe\/","title":{"rendered":"Chef Tutorials: How to set environment variable in Chef recipe"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">How you set an environment variable in a Chef recipe depends on who needs to see it. It might be the running chef-client process, a single <code>execute<\/code> command, every login shell, a systemd service, or Windows system-wide. This guide explains when to use each method shown above and when its timing matters. It also gives tested replacements for the outdated <code>magic_shell<\/code> and <code>Chef::Util::FileEdit<\/code> examples.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Method #1<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\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-keyword\">If<\/span> you need an env <span class=\"hljs-keyword\">var<\/span> set strictly within the Chef process, you can <span class=\"hljs-keyword\">use<\/span> <span class=\"hljs-title\">ENV<\/span>&#91;'<span class=\"hljs-title\">foo<\/span>'] = '<span class=\"hljs-title\">bar<\/span>' <span class=\"hljs-title\">since<\/span> <span class=\"hljs-title\">it<\/span>'<span class=\"hljs-title\">s<\/span> <span class=\"hljs-title\">a<\/span> <span class=\"hljs-title\">ruby<\/span> <span class=\"hljs-title\">process<\/span>.\r\n\r\n<span class=\"hljs-title\">Method<\/span> #2\r\n<span class=\"hljs-title\">If<\/span> <span class=\"hljs-title\">you<\/span> <span class=\"hljs-title\">need<\/span> <span class=\"hljs-title\">to<\/span> <span class=\"hljs-title\">set<\/span> <span class=\"hljs-title\">one<\/span> <span class=\"hljs-title\">for<\/span> <span class=\"hljs-title\">an<\/span> <span class=\"hljs-title\">execute<\/span> <span class=\"hljs-title\">provider<\/span>, <span class=\"hljs-title\">Chef<\/span> <span class=\"hljs-title\">exposes<\/span> <span class=\"hljs-title\">an<\/span> <span class=\"hljs-title\">environment<\/span> <span class=\"hljs-title\">hash<\/span>:\r\n\r\n<span class=\"hljs-title\">execute<\/span> '<span class=\"hljs-title\">Bootstrap<\/span> <span class=\"hljs-title\">the<\/span> <span class=\"hljs-title\">database<\/span>' <span class=\"hljs-title\">do<\/span> \r\n  <span class=\"hljs-title\">cwd<\/span> \"#{<span class=\"hljs-title\">app_dir<\/span>}\/<span class=\"hljs-title\">current<\/span>\"\r\n  <span class=\"hljs-title\">command<\/span> \"#{<span class=\"hljs-title\">env_cmd<\/span>} <span class=\"hljs-title\">rake<\/span> <span class=\"hljs-title\">db<\/span>:<span class=\"hljs-title\">drop<\/span> <span class=\"hljs-title\">db<\/span>:<span class=\"hljs-title\">create<\/span> <span class=\"hljs-title\">db<\/span>:<span class=\"hljs-title\">schema<\/span>:<span class=\"hljs-title\">load<\/span> <span class=\"hljs-title\">RAILS_ENV<\/span>=#{<span class=\"hljs-title\">rails_env<\/span>}\"\r\n  <span class=\"hljs-title\">environment<\/span> '<span class=\"hljs-title\">HOME<\/span>' =&gt; \"\/<span class=\"hljs-title\">home<\/span>\/#{<span class=\"hljs-title\">app_user<\/span>}\"\r\n  <span class=\"hljs-title\">user<\/span> <span class=\"hljs-title\">app_user<\/span>\r\n  <span class=\"hljs-title\">action<\/span> :<span class=\"hljs-title\">run<\/span>\r\n  <span class=\"hljs-title\">not_if<\/span> %&#91;<span class=\"hljs-title\">psql<\/span> -<span class=\"hljs-title\">U<\/span> <span class=\"hljs-title\">postgres<\/span> -<span class=\"hljs-title\">c<\/span> \"\\\\<span class=\"hljs-title\">l<\/span>\" | <span class=\"hljs-title\">grep<\/span> #{<span class=\"hljs-title\">db_name<\/span>}]\r\n<span class=\"hljs-title\">end<\/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<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<script src=\"https:\/\/gist.github.com\/devops-school\/fdb9f62b6a3a955b6c6c668506d01b68.js\"><\/script>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Choose the method by scope and persistence<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Chef recipes are Ruby, so an environment variable can live in several places. Before you write any code, answer two questions. Which processes must see the variable? Must it still be there after the Chef run ends? The table maps the methods above (including the extra ones from the embedded gist) to those answers.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Method<\/th><th>Who sees the variable<\/th><th>Persists after the run?<\/th><\/tr><\/thead><tbody><tr><td><code>ENV[&#039;FOO&#039;] = &#039;bar&#039;<\/code><\/td><td>The chef-client process and every child process it starts later in the run<\/td><td>No<\/td><\/tr><tr><td><code>environment<\/code> property on <code>execute<\/code>, <code>bash<\/code>, <code>script<\/code><\/td><td>Only that one command<\/td><td>No<\/td><\/tr><tr><td>File in <code>\/etc\/profile.d\/<\/code><\/td><td>New login shells (SSH sessions, <code>bash -l<\/code>)<\/td><td>Yes<\/td><\/tr><tr><td><code>\/etc\/environment<\/code><\/td><td>New PAM sessions (logins, <code>su -<\/code>, many desktop sessions)<\/td><td>Yes<\/td><\/tr><tr><td>systemd <code>Environment=<\/code><\/td><td>Only that service<\/td><td>Yes<\/td><\/tr><tr><td><code>windows_env<\/code><\/td><td>New Windows processes (machine or user scope)<\/td><td>Yes<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> No method changes the environment of a shell that is already open. You must log in again, start a new shell, or restart the service before the new value shows up.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting variables for the Chef run with ENV<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Method #1 assigns to Ruby&#039;s <code>ENV<\/code> hash. Every child process that chef-client starts after the assignment inherits the variable. That covers <code>execute<\/code> commands, guards and package managers. The catch is timing. Chef compiles all recipes first and converges resources afterwards. A bare <code>ENV[...] = ...<\/code> at the top level of a recipe runs during compile, before any resource has run. That is usually fine. If the value depends on something a resource creates, set it at converge time inside a <code>ruby_block<\/code> instead.<\/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\"># Compile time: set as soon as the recipe file is loaded<\/span>\nENV&#91;<span class=\"hljs-string\">'APP_ENV'<\/span>] = <span class=\"hljs-string\">'production'<\/span>\n\n<span class=\"hljs-comment\"># Converge time: set when this resource is reached in the run<\/span>\nruby_block <span class=\"hljs-string\">'export JAVA_HOME for the rest of the run'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  block <span class=\"hljs-keyword\">do<\/span>\n    ENV&#91;<span class=\"hljs-string\">'JAVA_HOME'<\/span>] = ::File.realpath(<span class=\"hljs-string\">'\/usr\/lib\/jvm\/default-java'<\/span>)\n  end\nend\n\nexecute <span class=\"hljs-string\">'show inherited variables'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  command <span class=\"hljs-string\">'env | grep -E \"APP_ENV|JAVA_HOME\" &gt; \/tmp\/chef-env-check.txt'<\/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\"><code>ENV<\/code> changes are global to the process and last until chef-client exits. They can leak into unrelated resources, so use them only for values the whole run needs, such as proxy settings.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Passing variables to execute, bash and script resources<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For most cases, the <code>environment<\/code> property (alias <code>env<\/code>) from Method #2 is the cleanest option. It takes a Ruby hash and applies only to that command. The example below is a self-contained version of the database bootstrap above. The variables are defined, and <code>RAILS_ENV<\/code> is passed through the environment instead of as a rake argument.<\/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\">app_user = <span class=\"hljs-string\">'deploy'<\/span>\napp_dir  = <span class=\"hljs-string\">'\/srv\/myapp'<\/span>\ndb_name  = <span class=\"hljs-string\">'myapp_production'<\/span>\n\nexecute <span class=\"hljs-string\">'Bootstrap the database'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  cwd <span class=\"hljs-string\">\"#{app_dir}\/current\"<\/span>\n  command <span class=\"hljs-string\">'bundle exec rake db:create db:schema:load'<\/span>\n  user app_user\n  environment(\n    <span class=\"hljs-string\">'HOME'<\/span>      =&gt; <span class=\"hljs-string\">\"\/home\/#{app_user}\"<\/span>,\n    <span class=\"hljs-string\">'RAILS_ENV'<\/span> =&gt; <span class=\"hljs-string\">'production'<\/span>,\n    <span class=\"hljs-string\">'PATH'<\/span>      =&gt; <span class=\"hljs-string\">\"\/usr\/local\/bin:#{ENV&#91;'PATH']}\"<\/span>\n  )\n  not_if <span class=\"hljs-string\">\"psql -U postgres -Atc \\\"SELECT 1 FROM pg_database WHERE datname='#{db_name}'\\\" | grep -q 1\"<\/span>\n  action :run\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>Setting <code>user<\/code> does not reset <code>HOME<\/code>. Without the <code>environment<\/code> entry, tools such as Bundler or Git can try to write into root&#039;s home directory and fail with permission errors.<\/li>\n\n\n<li>If you set <code>PATH<\/code> in the hash, it replaces the inherited value completely. Append <code>ENV[&#039;PATH&#039;]<\/code> as shown so that system binaries can still be found.<\/li>\n\n\n<li>The <code>db:drop<\/code> from the original was removed. Even with the guard, a destructive drop does not belong in a bootstrap step that runs repeatedly.<\/li>\n\n\n<li>A string guard can take its own options, for example <code>not_if &#039;some-check&#039;, environment: { &#039;PGPASSWORD&#039; =&gt; pw }, user: &#039;postgres&#039;<\/code>. Pass them explicitly when the guard needs the same variables as the command.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>bash<\/code>, <code>script<\/code>, <code>powershell_script<\/code> and <code>batch<\/code> resources accept the same <code>environment<\/code> property.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Persistent variables on Linux: profile.d, \/etc\/environment and systemd<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Method #3 suggests writing to <code>\/etc\/profile.d<\/code>. You can do that with Chef&#039;s built-in <code>file<\/code> resource. The resource is idempotent and reports changes correctly, and it replaces the community <code>magic_shell<\/code> cookbook from Method #4, which is old and no longer necessary.<\/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 <span class=\"hljs-string\">'\/etc\/profile.d\/myapp.sh'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  content &lt;&lt;~EOS\n    <span class=\"hljs-keyword\">export<\/span> APP_ENV=production\n    <span class=\"hljs-keyword\">export<\/span> JAVA_HOME=<span class=\"hljs-regexp\">\/usr\/<\/span>lib\/jvm\/java<span class=\"hljs-number\">-17<\/span>-openjdk-amd64\n  EOS\n  owner <span class=\"hljs-string\">'root'<\/span>\n  group <span class=\"hljs-string\">'root'<\/span>\n  mode  <span class=\"hljs-string\">'0644'<\/span>\nend<\/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\">Only login shells source <code>\/etc\/profile.d<\/code>. Cron jobs, Ruby <code>system()<\/code> calls, the Chef run itself and systemd services do not read it, which is the gotcha the gist mentions. For a long-running service, set the variable in the service&#039;s unit file instead.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">systemd_unit <span class=\"hljs-string\">'myapp.service'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  content(\n    Unit:    { <span class=\"hljs-attr\">Description<\/span>: <span class=\"hljs-string\">'My application'<\/span>, <span class=\"hljs-attr\">After<\/span>: <span class=\"hljs-string\">'network.target'<\/span> },\n    <span class=\"hljs-attr\">Service<\/span>: {\n      <span class=\"hljs-attr\">User<\/span>: <span class=\"hljs-string\">'deploy'<\/span>,\n      <span class=\"hljs-attr\">Environment<\/span>: <span class=\"hljs-string\">'APP_ENV=production RAILS_ENV=production'<\/span>,\n      <span class=\"hljs-attr\">ExecStart<\/span>: <span class=\"hljs-string\">'\/srv\/myapp\/current\/bin\/start'<\/span>\n    },\n    <span class=\"hljs-attr\">Install<\/span>: { <span class=\"hljs-attr\">WantedBy<\/span>: <span class=\"hljs-string\">'multi-user.target'<\/span> }\n  )\n  action &#91;:create, :enable]\nend<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><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\"><strong>Note:<\/strong> <code>\/etc\/environment<\/code> is not a shell script. <code>pam_env<\/code> reads it as plain <code>NAME=value<\/code> lines, so <code>export<\/code>, <code>$VAR<\/code> expansion and command substitution do not work there.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Corrected \/etc\/environment example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Method #5 has several problems. It runs <code>Chef::Util::FileEdit<\/code> directly in the recipe body, so the file is edited at compile time and Chef never reports the change. It calls <code>write_file<\/code> on every pass through the loop. <code>insert_line_if_no_match<\/code> never updates a variable that already exists with an old value. The Java 7 path is also out of date. The version below runs at converge time, updates or inserts each line, writes the file once, and skips the work when the file is already correct.<\/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\">env_vars = {\n  <span class=\"hljs-string\">'JAVA_HOME'<\/span> =&gt; <span class=\"hljs-string\">'\/usr\/lib\/jvm\/java-17-openjdk-amd64'<\/span>\n}\n\nruby_block <span class=\"hljs-string\">'manage variables in \/etc\/environment'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  block <span class=\"hljs-keyword\">do<\/span>\n    fe = Chef::Util::FileEdit.<span class=\"hljs-keyword\">new<\/span>(<span class=\"hljs-string\">'\/etc\/environment'<\/span>)\n    env_vars.each <span class=\"hljs-keyword\">do<\/span> |name, val|\n      line = <span class=\"hljs-string\">\"#{name}=\\\"#{val}\\\"\"<\/span>\n      fe.search_file_replace_line(\/^<span class=\"hljs-comment\">#{name}=\/, line)<\/span>\n      fe.insert_line_if_no_match(\/^<span class=\"hljs-comment\">#{name}=\/, line)<\/span>\n    end\n    fe.write_file\n  end\n  not_if <span class=\"hljs-keyword\">do<\/span>\n    current = ::File.read(<span class=\"hljs-string\">'\/etc\/environment'<\/span>)\n    env_vars.all? { |n, v| current.<span class=\"hljs-keyword\">include<\/span>?(<span class=\"hljs-string\">\"#{n}=\\\"#{v}\\\"\"<\/span>) }\n  end\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<p class=\"wp-block-paragraph\">The original also set <code>LD_LIBRARY_PATH<\/code> to the JDK library directory. The Java launcher finds its own libraries, so this is not needed. Setting it system-wide can also make unrelated programs load the wrong shared libraries, so it has been left out.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting variables on Windows with windows_env<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>windows_env<\/code> is built into Chef Infra Client. By default it writes a machine-level variable. Set the <code>user<\/code> property to target a specific account, and use <code>delim<\/code> to add an entry to a list-style variable such as <code>PATH<\/code> without overwriting it.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">windows_env <span class=\"hljs-string\">'APP_ENV'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  value <span class=\"hljs-string\">'production'<\/span>\n  <span class=\"hljs-attr\">action<\/span> :create\nend\n\nwindows_env <span class=\"hljs-string\">'PATH'<\/span> <span class=\"hljs-keyword\">do<\/span>\n  value <span class=\"hljs-string\">'C:\\\\tools\\\\bin'<\/span>\n  delim <span class=\"hljs-string\">';'<\/span>\n  <span class=\"hljs-attr\">action<\/span> :modify\nend<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><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\"><strong>Note:<\/strong> The gist uses <code>CHEF_LICENSE<\/code> as its example, but a recipe cannot accept the license for the run that is executing it. Accept the license before the first run with <code>chef-client --chef-license accept<\/code> or by setting <code>CHEF_LICENSE=accept<\/code> in the bootstrap environment.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verifying the result and common errors<\/h2>\n\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\"><span class=\"hljs-comment\"># Run a single recipe locally<\/span>\nsudo chef-client -z -o <span class=\"hljs-string\">'recipe&#91;myapp::env]'<\/span>\n\n<span class=\"hljs-comment\"># Check a new login shell picks up profile.d<\/span>\nbash -lc <span class=\"hljs-string\">'echo $APP_ENV'<\/span>\n\n<span class=\"hljs-comment\"># Check a systemd service<\/span>\nsystemctl show myapp.service -p Environment\n\n<span class=\"hljs-comment\"># Windows (PowerShell): machine-level value<\/span>\n&#91;Environment]::GetEnvironmentVariable(<span class=\"hljs-string\">'APP_ENV'<\/span>, <span class=\"hljs-string\">'Machine'<\/span>)<\/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<figure class=\"wp-block-table\"><table><thead><tr><th>Symptom<\/th><th>Cause<\/th><th>Fix<\/th><\/tr><\/thead><tbody><tr><td>Variable is empty in your current SSH session<\/td><td>Existing shells never reload their environment<\/td><td>Log out and back in, or start <code>bash -l<\/code><\/td><\/tr><tr><td>Service does not see the variable from profile.d<\/td><td>systemd does not source shell profiles<\/td><td>Use <code>Environment=<\/code> in the unit and restart the service<\/td><\/tr><tr><td><code>execute<\/code> with <code>user<\/code> fails writing to <code>\/root<\/code><\/td><td><code>HOME<\/code> is still root&#039;s home directory<\/td><td>Set <code>HOME<\/code> in the <code>environment<\/code> hash<\/td><\/tr><tr><td>Command not found after setting <code>environment<\/code><\/td><td><code>PATH<\/code> in the hash replaced the inherited value<\/td><td>Append <code>ENV[&#039;PATH&#039;]<\/code> to your value<\/td><\/tr><tr><td>ENV value is missing or stale in a later resource<\/td><td>The variable was set at compile time from data that only exists at converge time<\/td><td>Move the assignment into a <code>ruby_block<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Does ENV[&#039;VAR&#039;] = &#039;value&#039; in a Chef recipe persist after chef-client finishes?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. It changes only the environment of the running chef-client process and its child processes. To make a variable persistent, write it to \/etc\/profile.d, \/etc\/environment, a systemd unit or, on Windows, use windows_env.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between environment and env on the execute resource?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There is no difference. <code>env<\/code> is an alias for <code>environment<\/code>, and both take a hash of variable names and values that applies only to that command.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does my systemd service not see a variable I added to \/etc\/profile.d?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Only login shells read \/etc\/profile.d. Services started by systemd do not, so set the variable with <code>Environment=<\/code> or <code>EnvironmentFile=<\/code> in the unit file and restart the service.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I add a directory to PATH on Windows with Chef?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use the <code>windows_env<\/code> resource with <code>key_name<\/code> PATH, the directory as <code>value<\/code>, <code>delim &#039;;&#039;<\/code> and <code>action :modify<\/code>. This appends the entry instead of replacing the whole PATH.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Do I still need the magic_shell cookbook to set environment variables?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. A built-in <code>file<\/code> or <code>template<\/code> resource that writes a script to \/etc\/profile.d does the same job without an external cookbook dependency.<\/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>How you set an environment variable in a Chef recipe depends on who needs to see it. It might be the running chef-client process, a single execute&#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,1074],"class_list":["post-31837","post","type-post","status-publish","format-standard","hentry","category-chef","tag-chef-recipes","tag-environment-variables"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31837","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=31837"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31837\/revisions"}],"predecessor-version":[{"id":80486,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/31837\/revisions\/80486"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=31837"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=31837"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=31837"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}