Now we have an absolutely awesome demonstration,
and again, a demonstration I would really ask you to follow through.
This whole course is about automation.
So what we're going to be taking a look at now is how we can delete our vagrant
virtual machine, so the Ubuntu 20.04 machine that we've been working with.
We're going to recreate it, so vagrant up, it's very easy to recreate. We'll
install Ansible. Ansible is the first configuration management product that we
looked at, and it's very easy to get up and running without any configuration
if we only want to work with our local host. We'll create a playbook, and that
playbook is going to recreate everything that we've looked at within this
module.
So it's going to install Docker, it's going to add in the repository for
Terraform and the keys that are needed for that, all of the work that
was necessary to get this up and running.
So we're going to see how we do that, and we're going to dive out to
our command line and say I really encourage you to follow through,
and I think you're going to find this quite impressive. Now as this
course is all about automation,
let's go through and destroy our current vagrant virtual machine. So I
can exit to log me out. Back then on my host system,
I can run the vagrant and destroy.
That's very tempting, isn't it?
I only need to worry about destroying the Ubuntu system,
and then ‑‑force so it doesn't prompt me.
So this will both shut my virtual machine down and delete
the instance of that virtual machine.
So my Ubuntu 20.04 system now has been destroyed.
It's very easy to bring it back up again, though,
and that's the vagrant up command.
We have a golden image or a vagrant box, and we clone that box to
create the instance of the virtual machine.
So I'm going to have a clean and fresh virtual machine. Now, allowing that a
couple of minutes to run through, now that it is up, I can then ssh back into
this, and this is now a brand‑new and clean Ubuntu 20.04 system. It doesn't
have anything that we've been working with.
Look at this, listing the home directory, and it
does look pretty empty, doesn't it?
We don't have the Terraform directory, we don't have our CompTIA automation
repository. But it is very easy to get everything back the way we need it,
and this is the importance of automation. We're going to install Ansible.
Ansible is a good candidate for the automation here because it very easily
manages its own system with no configuration. Now, I don't know about you,
but I'm very into no configuration, so let's go through then and update our
apt cache, so our metadata. So this is going to make sure we've got the
up‑to‑date information about what's in our repositories.
But remember, it's just our standard repositories that we are looking
at. Now with that done, we can go ahead and install Ansible. So sudo and
apt install and ansible. I'm not going to worry about the ssh pass
because I'm not going to be working with any remote machines so I don't
need to cache ssh passwords.
So Ansible will install very quickly. We can run this in real time. And
then once it's been installed, we just need to create a playbook. Now, of
course, the playbook is where the complexity is.
We have provided it through our repository, but, of course, as yet, we haven't
got our repository. But let's take a look at this. If I go through and vim my
.play or .yaml, whatever you want to call it,
I'm just calling it .play. Normally we'd have .yaml. We'd get color
coding then in our file. But it doesn't really matter the name of our
file. I can go through and save this. Now, of course, you want to know
what's in that playbook, and it's probably easier to bring across where
I create my playbooks.
So this is just from my host machine, and we can go through
and see all of the tasks that we've got.
So we're only playing against our local host.
The first thing we do is update the apt cache. We add in the Terraform
repository key, so using apt key. Using apt_repository, we can add in then our
repository. We can install the software, so that's including then Docker and
Terraform. There are other products there, but they're probably installed
anyway, but they were prerequisites. We need to add our user into the Docker
group, and we can make use of environment variables so the user variable is
being added to our Docker group.
We can then move ahead into adding our course repository.
The course repository coming from Git is going to be really
important because that's going to have the information that we
need for our Terraform configuration file,
our main.tf file.
We need to create the Terraform directory.
We've seen we don't have it at the moment, so we need to create
it. And, of course, we have the web directory below that. We
can add then the Terraform file.
Now we can copy that across from the Git repository.
Again, we're making use of our variables here, and then we're adding
in our index page. And that's all that we've got.
So this we can just shut down.
We don't need it anymore.
I was only displaying you the playbook through here because it's
probably easier to display it than it is with vim.
But now we are ready to go.
We just need to run then our playbooks, our ansible‑playbook, and then Esc and
dot will bring up that last argument. Hit the big Enter key and watch the
glorious activities. So we didn't need to update our cache. We can see then
that we've added the Terraform key, the Terraform repository, and now we're on
to installing the software. So this will be Docker that's being installed, as
well as Terraform. Everything we need to be done is defined nicely within the
playbook.
If it doesn't need to be done, then it won't happen.
So we can see the apt cache wasn't updated because it was fresh enough.
Now we're adding our user through to the group,
adding the course repo, creating the two subdirectories.
The main Terraform file gets copied across, and we
add content to our index page.
So everything we've done during this module is now automated.
I do need to log out and log back in again to pick up that new group membership,
and of course, we can easily check that using the id command, and we can see
this is a good sign, we belong to the Docker group.
If I list now, we can see that we've got the CompTIA
automation, the Terraform directory, as well as the
playbook that we had created earlier.
But if I move into my Terraform directory,
if I cat out the main.tf file, this was copied across from my Git repository,
and this is then defining what we want Terraform to do. So I can go through and
use terraform and init. Remember it is this that then goes through and installs
the backend drivers, so our provider plugins. We can then go through and run
terraform and apply. This then shows me the work that it is going to do. I can
agree to the schedule, type yes, and then away we go. We're downloading our
docker image,
we're cloning it to become the container, but we can see then how easy it is
to get it up and running by just now browsing through to my fully operational
website, so curl localhost on port 8000, and our website is up and running.
And it really is as simple as that.
We just need to define all of our steps within our playbook or
whatever automation setting that we want to use, and it's very
easy then to reproduce our environment.
So it doesn't matter whether you're using this at home, in the
office, or for DevOps, trying to understand the importance and how
quickly automation can get you up and running can be terribly important and time‑saving. Come on, we need to summarize some of this.