Information Technology Reference
In-Depth Information
source. Plus, keep in mind that Chef recipes are intended to be run on different machines and
even different operating systems producing the same configuration. Chef steers you toward
using absolute paths in recipes because it would be difficult to specify consistent file loca-
tions using relative paths.
NOTE
On Windows, Chef will convert absolute paths with forward slashes ( / ) to use the
Windows-style backslash character ( \ ). You can use the backslash character, but Chef
code uses the backslash ( \ ) as an escape character in strings. So, you have to specify a
double backslash ( \\ ) to insert a literal single backslash in a string. In this topic, we'll stick
with using forward slashes ( / ) on Windows.
Let's explain the #{ENV['HOME']} construct and why we changed the file string to use
double quotes (“”) instead of single quotes ('\'). In Chef code, it doesn't matter if you use
double quotes or single quotes for string literals . However, it does matter when you want to
evaluate the value of a variable in a string (also known as string interpolation ).
#{ENV['HOME']} is a variable referring to the current user's home directory. Variable refer-
ences within strings are denoted by #{<variable>} (a hash character followed by the vari-
able enclosed by a set of curly braces). A string must be enclosed in double quotes (“”) when
it contains a variable reference. Otherwise, Chef will not replace the variable reference with
its value when the string is evaluated.
The ENV['HOME'] variable is a reference to a collection of name-value pairs. Chef calls a
collection of name-value pairs a Hash . Other languages refer to this construct as dictionary,
associative array, or a map—it's all the same thing. In a Chef recipe, you can retrieve the
value for a system environment variable by referring to the variable ENV['<name>'] . name
refers to a string with an environment value name. This is the code equivalent of:
▪ echo $HOME on Linux/Mac OS X/Windows PowerShell
▪ echo %USERPROFILE% on Windows Command Prompt
For example, my home directory is /Users/misheska . Chef evaluates the string
"#{ENV['HOME']}/stone.txt" as "/Users/misheska/welcome.txt" .
Search WWH ::




Custom Search