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


Talk Python's Mastodon Michael Kennedy's Mastodon