Within our next recipe, we're going to take a look out working with files.
So we can create the recipe; this time we're calling it hello.
Don't forget the subdirectory recipes.
We can create the default.rb or if you prefer,
you could even copy it across from the previous recipe.
We start very similar, but this time we're working with our file resource,
but we're working with that same home directory and then this time,
the hello.txt file.
Here we've got something very simple where we've only got the
content property and a single line content.
But this is good to begin with because we're then able to go
through and test it with our chef‑client and make sure that we
can see the text that we were expecting.
However, there's a lot more that we can do.
We can take a look at having multiple lines of content,
and we can look at setting things like the ownership,
setting things like the permissions on the file as well.
So as we start with something more simple,
we can develop it within the same recipe.
And that's exactly what we're going to see when we move out to the command line.
And I do encourage you to follow along at home as
we work with our file resources, so creating the file,
setting the file mode, and updating the content.
If I use Ctrl+R, I can get back to my mkdir command.
I can hit Esc,
and I just need to change the directory name here for the name of the recipe.
I'm going to change this then to hello.
So it's just making the command line work for you rather
than you working for the command line.
So that's working nicely for me.
With that done, we can go through and copy,
I'll go through and copy, Esc, and dot.
I'm just going to have to change this back to repo/
and then put Esc and dot again.
And inside of our recipes directory from the repo,
we want to then go through and copy across the default.rb file.
If I then go through and vim Esc and dot and then default.
So this is now looking at my hello recipe.
I could start with the original recipe,
the default Ruby file that came from my repo.
And when we look at it, there isn't too much I have to change to begin with.
Obviously, I need to go through and change the resource.
We're going to be working with a file this time.
So I'm going to put then the file resource name.
Within our home directory, we can create then a file,
which we can call anything you want really,
but I'm going to maybe specify it as hello.txt.
Now we're going to get a little bit different,
so I can go through and sort of delete these lines
and then add in a new line above.
With this then, we've just got our content,
so this is the property, and we can go through and put,
let's say, Hello world or whatever we'd like to put in.
We kind of have to have this, don't we?
So let's add this in.
It's all looking good to me,
so I'm going to go through and save our first recursion of this.
Let's see how it works.
So if I up arrow key again, I'm just going to go back to my client,
but make sure the chef‑client, of course,
now we're responding to the hello recipe.
We can hit Enter, and we can see then that we've created that new file.
If we look at it from a permissions point of view,
it will have used the default permissions.
So when I go through and take a look at the hello.text and then,
of course, when I go through and look inside of this with,
let's say, the cat command, we're going to have then are very basic content.
Part of it we don't have a new line at the end,
so it's kind of messing up my prompt.
If we go back into then editing the Ruby file,
one of the things that we can do straight away is to tidy up the output
of the content by adding in a new line at the end.
But if you take a look at it inside of the single quotes,
we don't differentiate between the new line and the rest of the string.
So if we put it into double quotes, then we can still expand the new line.
So the color coding really does help us here.
But we can take this just a little bit further.
If I want multi‑line content,
then just go through and add another line and keep a new line at the end of
that if we want a clear new line at the end of our file.
If we wanted more complex content, we could consider using,
instead of the file resource using a template
resource and copying across a template.
Here we're going to go through and specify the permissions of our file,
so rather than the default permissions of 644,
we can have read, write, and execute for the user,
but no permissions for anyone else, so setting the mode string.
Saving our file, we can go back and reexecute it with our chef‑client.
We won't need to create the file, but we can see that we've updated the content.
We can see the difference in between the old file and the new file,
and we can also see the change of the file mode.
So this is really good that we are only updating the file,
not creating the new file.
And of course, if we go through and list it,
we've got then the full content over two lines and the
tidy end of line before our prompt.
And of course, checking our permissions,
we're going to see then that we've got read,
write, and execute for our vagrant user,
but no permissions for the group or others.
So we've been able to dictate exactly how we want the desired state of our
file to be and modify it so it matches the new desired state.
Now, of course, then this is just something else that you can use.
I'm not saying that you're going to be waiting
patiently for the hello.text file,
but you can then go through and look at distributing small
files such as sudoers files and having the right
permissions and content on those files.
We're going to move ahead now and look at how we can create recipes for distributing packages.