Mixing plain Ruby with Chef DSL
To create simple recipes, you only need to use resources provided by Chef such as template
, remote_file
, or service
. However, as your recipes become more elaborate, you'll discover the need to do more advanced things such as conditionally execute parts of your recipe, looping, or even complex calculations.
Instead of declaring the gem_package
resource ten times, simply use different name attributes; it is so much easier to loop through an array of gem names creating the gem_package
resources on the fly.
This is the power of mixing plain Ruby with Chef Domain Specific Language (DSL). We'll see a few tricks in the following sections.
Getting ready
Start a Chef-shell on any of your nodes in Client mode to be able to access your Chef server, as shown in the following code:
user@server:~$ sudo chef-shell --client
loading configuration: /etc/chef/client.rb Session type: client ...TRUNCATED OUTPUT... run `help' for help, `exit' or ^D to quit. Ohai2u user@server! chef...