Now when we're working with Chef, our configuration state files,
our desired states, are written as Ruby files.
So these files are called recipes.
The recipes of course being written in Ruby does add a little complexity
compared to what we've seen previously with YAML; however,
we can also see what we can do.
And we're going to create our first recipe to clone the course Git repo.
In our home directories we'll create a subdirectory called cookbooks,
and below that we can have our recipe name,
and we're going to call it repo,
below that we will have our recipes subdirectory.
Inside of the recipe subdirectory we create our recipe file,
this is the default.rb.
This can contain everything or can include other Ruby files.
We're just putting something relatively simple,
we're using the git resource, and in the name of our resource,
we're actually pointing through to the directory that we are going to clone to.
So we're pulling out our environment variable HOME,
and we are creating a subdirectory called comptia‑automation.
We enter our do block,
and we have a couple of properties that we are setting for the git resource,
the repository that we are cloning, the revision,
or the branch, that we want to clone, and then our action,
which is then the synchronization, and we close the do block with an end.
Once we've done that, we can use our chef‑client.
‑Z is then just specifying that we are working locally only.
The ‑o is overwriting our standard node work,
we haven't assigned any, but we are looking then for the repo recipe.
We're going to dive out then to our command line and see this in action,
our very first recipe, and I hope we're going to enjoy it.
We're going to use it then to clone our course repository
that we've been previously working with.
So here we are then back on our Ubuntu 2004 system,
and when we look at my home directory, it's pretty empty,
we haven't done a lot to it so far.
So we're going to create ourselves a directory to house our cookbooks.
So mkdir ‑p is going to create the parent structure.
I don't need to use the tilde, I'm in my home directory,
but this way we know the command is going to work no
matter the directory that we're in.
And we're creating then our top‑level cookbooks directory,
the client is going to look for this.
Underneath that we can name our recipe, and we're calling it repo,
and we have a recipes subdirectory.
So once we've created this, we need to create our default recipe file.
So we can go through and use vim, Esc + dot to bring up our last argument,
and then default.rb, a Ruby file.
Now this Ruby file might contain just inclusions of other Ruby files,
or, in our case as we're only going to be using a single resource,
it's very easy just to add it into the one file.
So we're going to be making use of the git Chef resource.
We can use this to clone a repository.
We can specify then the path that we're going to clone to,
and we're going to make use of an environment variable within our Ruby code.
So we can go through then in using double quotes and hash,
open brace brackets, ENV, to bring up the environment class,
and say that we want to look then for our HOME environment variable.
We can close off our square brackets and close off our brace brackets,
the brace brackets delimiting the variable name,
and we can then go through and put in comptia‑automation,
the directory in our home directory that we are going to
synchronize our course git repository to.
We can make sure that that's closed off with our double quotes,
and we enter our do block.
In here we can start setting our properties.
So we're setting the property of the path to our repository,
and this is going to be https.
We know this by now, don't we?
And we're going to find this at github.com.
And then our course repository is theurbanpenguin/ and then comptia‑automation.
These are just strings so I'm just using single quotes to mark them as strings.
And we need to then say, well, which branch do we want?
And that's called our revision.
In this repository it's main.
Other ones might be master, but this is just named main.
And then we can go through and specify the action,
what do we want to do to this repository?
Well, we want to synchronize it through to the comptia‑automation directory.
And that is the end of our block, so we end the block with the end keyword.
If it all looks okay, we can save it.
And we are ready to go.
We've created this now, so we can fire it up with our chef‑client,
‑z is telling the client to work locally,
‑o is overwriting the node list, the list of work for the node,
and we're specifying that we want the recipe repo.
We redirect any errors, it will be chatty a little bit,
but we can see then that we've cloned the repository.
Now of course we can check that It's done, very easily.
We saw with ls before, we had nothing, but take a look now,
we've got the comptia‑automation directory and the files that we've
been providing to help support you and your learning.
If we go through and run it again, the chef‑client,
we can see we are up to date, so there's no need to sync any further.
So like most of our tools,
we should be able to see the idempotency working with only pull
information if the repository had been updated.
But look at this, this is a great start,
and looking at something that we haven't seen within our other
configuration management tools that we've seen, cloning a git repository.