Python for Entrepreneurs Transcripts
Chapter: Deploying to the cloud
Lecture: Using Ansible
Login or
purchase this course
to watch this video and the rest of the course contents.
0:01
Now, that we provision our server on Digital Ocean,
0:03
we can start the process of configuring our server
0:05
to get it secured and ready to serve up our web application.
0:10
Throughout the major of this chapter, we're going to use
0:13
a configuration management tool known as Ansible,
0:16
in order to automate our deployment.
0:18
Before configuration management tools became popular
0:21
a lot of deployments were done manually, anything in programming
0:23
that is done manually is a subject to a lot of errors
0:26
and takes more time than it should, compared to when its automated.
0:30
That's why I love using Ansible for deployments,
0:32
and hopefully by the end of the chapter, you will as well.
0:35
So Ansible is an implementation of the concept
0:39
known as configuration management.
0:41
Configuration management tools are used to programmatically set up
0:44
and configure infrastructure, in our case that will be
0:47
a single server running on Ubuntu Linux.
0:50
Ansible was originally created by Michael DeHaan,
0:53
and there was a company he founded around it
0:55
that company is now owned by Red Hat,
0:57
so Red Hat is actively maintaining and evolving Ansible as a tool
1:00
but what is especially great for us, is that everything in Ansible
1:04
is written or Python, in fact, if you go to github
1:07
and look under the Ansible organization and the Ansible project
1:10
you're going to see a Python project and you can go in
1:13
and read all the code behind how Ansible is created.
1:16
Ansible is made up of many, many modules that are written in Python
1:19
and as a shameless plug one of my favorite modules, is the twilio module
1:23
which I wrote a few years ago, and still maintain;
1:26
this module is actually very simple, it's just a bit of Python code
1:29
that calls the Twilio API and allows you to send text message notifications
1:33
or multimedia message notifications when something happens in your deployment.
1:38
As long as you have a trial or upgrade a Twilio account
1:41
you can send text message notifications to yourself,
1:44
or anyone that's involved in deployment,
1:46
but as you can see, this is all just Python code,
1:48
so as you get more comfortable with Ansible,
1:51
it's worth taking a look at the source code so you can understand
1:53
what's happening under the covers.
1:55
But for now, we're not going to worry about way the Ansible is written,
1:57
just know that it is in Python and we're going to use it
1:59
to automate our own deployment.
2:01
Now because it's Python, we can just use pip install Ansible,
2:04
and we'll get the tool a part of our project
2:08
and we're going to use the absolute latest up to date version of Ansible,
2:10
because Python 3 compatibility came fairly late to this project,
2:14
it's really only in version 2 that Python 3 became experimental
2:19
and it's still under active development, so in our case throughout this chapter,
2:22
we're going to use version 2.3 which is still under active development,
2:26
and I'll show you how to install that shortly.
2:29
Ansible uses a module based system to accomplish
2:31
all the tasks we want to perform throughout deployment
2:34
so for example, if you want to install a package on Ubuntu,
2:37
we're going to use the apt module
2:39
and we'll specify all the packages that we want.
2:42
Ansible organizes everything, in what is known as a playbook,
2:45
you'll execute the playbook using the Ansible tool
2:47
so the playbook is the high level concept that makes up the entire deployment process
2:52
we're going to write two playbooks in this chapter
2:55
the first one will be an initial configuration that will run a single time
2:59
to lock down the system, make sure everything is secure.
3:02
And then we'll have a second playbook, we'll run every single time
3:05
we want to deploy new code; each of these playbooks
3:07
is going to have many tasks within it, a task is a single step
3:10
that we want to accomplish and you write tasks in yaml
3:13
which stands for yet another markup language.
3:16
At first I was skeptical of yaml, I hadn't used it before Ansible,
3:19
but it really is a very natural way to write maintainable playbooks
3:24
that you can come back to 6, 12, 18 months later
3:27
and still understand what they're doing.
3:30
Which is definitely not the case if you're just going to automate things with bash scripts,
3:33
those are some of the vocabulary words
3:36
you're going to hear me use throughout these videos
3:38
What I also recommend is to pull up the Ansible documentation
3:42
that's at doc.ansible.com/ansible/index.html
3:46
their documentation is fantastic, it has coverage over every single module
3:51
that is written as part of core Ansible.
3:53
It will also provide more detail around what each of these concepts means
3:56
so you can become more comfortable with it, as we do the deployment.
3:59
I always keep the documentation open as I'm writing my playbooks,
4:02
because it's so handy to just go over to the module index
4:05
and take a look at the documentation for every single module that's out there.
4:10
So for example, when I wrote the Twilio module,
4:13
I had to write some fairly extensive documentation along with examples
4:16
that show exactly how to use this module,
4:19
so the format for every single Ansible module when you're reading the docs
4:23
gives you a high level overview, and then the options;
4:27
now the options are references that I am always looking at
4:31
because they will tell you what you need to specify in your playbook
4:34
in order to get a single task running.
4:36
After the options list are one or more examples for how to use a specific module
4:41
and this case for the Twilio module, you can see that there's a bunch of yaml,
4:45
and there are five parameters we need to specify in order to get this working,
4:48
and if we take a look at other modules, so for example the utilities modules,
4:52
will see that every other module follows the same pattern,
4:55
brief description, the options that are necessary, some examples,
4:59
and then other information you may need to know,
5:02
for example if a module is still in preview mode, and it can change,
5:06
or whether it's stable and therefore
5:09
there will be backwards compatibility going forward.
5:12
Spend some time taking a look at the Ansible documentation
5:14
and in the next video, we're going to write our first Anibal playbook
5:17
to configure our server.