Now hopefully we're really getting familiar with creating our recipes.
We're going to create a recipe now with packages to
install software packages on our system.
So we know to create the subdirectory within cookbooks, we're
calling the recipe install, and it must have a subdirectory called
recipes. Inside of that, we create our default Ruby, and here
we're using our package resource.
Now we've already seen that we've used the Git resource,
we've used the file resource.
The package resources is obviously just installing software packages. The
name of the resource can be inherited into the name of our package to
install, so we're installing tree. We've got our action to install, but
we could equally remove our package.
Once we're done with this,
we can go through and execute it within our chef‑client.
And of course, we then got the tree package install, so we're able to list
the contents of our cookbooks subdirectory to kind of remind ourselves of
some of the things that we've been looking at.
But not only installing a single package,
we can look at installing multiple packages, and that's exactly what
we're going to be doing within the demonstration.
So working with our package resource,
we're going to look at installing a single package, and then installing
package lists and looking at ways that we can manipulate or create our
lists within Ruby. And yes, the glorious command line again. Well,
we're going to learn, well, some of the old tricks we've already done.
So, Ctrl and R, searching for mkdir, and then replacing the name of our
recipe. And we can go through and replace that with the install recipe.
Give that the big Enter key, and we've got our directory
structure now where we can go through and create a new
recipe where we can install software.
We will create this from scratch, so vim and Escape and dot to bring up
our last argument, and then the good old default .rb.
Now we've seen different types of resources that we can use with Chef.
We've looked at the Git resource,
the file resource, but now it's the term of our package resource.
We can then go through and specify that we're installing the
'tree' package, that needs to be quoted.
We can go into our do block, and so long as we're using the
same name as our resource as the package, we only need to
put in the action of install.
So we're going to install the package tree.
If we want to give a more descriptive name, we could then go
through and give our resource name a longer title and use the
name property inside of our resource.
But we'll keep it like this and save our change.
And now we've got to consider what we're doing.
We're installing software, whereas previously we'd been looking
at just working within our own home directory.
So we're going to need to elevate our privileges with sudo and then run our
chef‑client. The chef‑client isn't running elevated by itself.
We can specify then the ‑z as before so we are working locally, and
then the ‑o where we're bypassing our node list and using the recipe
install. The pesky warnings we can send to dev/null, and we can see
now that we're thinking about it, and we're actually using apt_
package so we've detected the underlying packaging system. With tree
installed, of course,
then we can go through and use it, and look at all the hard work
that we've put into our cookbooks subdirectory.
So underneath cookbooks, we have then our three recipes, hello,
install, and repo, and their recipes subdirectory.
Now, of course, we only want to work with a single file, then that's good.
But if we want to install multiple packages, then we can use lists.
Lists are going to be inside of square brackets, so we can create a list,
but it's going to be a little bit of hard work because each list item
needs to be quoted and separated by a comma.
But let's go through and put, let's say, tree in, and then the
package vim, and close off our square brackets.
So if you like this and if you like the traditional longhand list, and we can
do it this way and we will prove that it works, let's go back and execute our
sudo and chef‑client. And we can see that, well,
okay, well both tree and vim were installed, so we're up to date,
there was nothing that we needed to do. But there is a quicker way in Ruby
programming to create a quick list if all of our list items are single words.
Let's go through and just delete all of the existing list. It will be quicker
just to type it all up again. And we can use % and w, and then open up our
parentheses, our round brackets. With w, we're saying that each item in the
list is a single word.
That means then I don't need to comma separate or quote them.
So this becomes a much easier way to create a list and certainly edit the
list later without having to worry about our quotes,
without having to worry about commas between the entries.
So, no quotes and no commas. So % and w is a really cool shortcut.
Once we've done this,
let's go through and save our changes, and let's check that this is working.
We've added in a couple of packages. Certainly the zsh is unlikely to
be installed, and we can see that we're waiting on the apt_package now,
and we can see that we've installed zsh out of that list. But there you
go, we've seen how we can install packages and how we can manage quick lists within Ruby.