Python for Entrepreneurs Transcripts
Chapter: Deploying to the cloud
Lecture: Setting up notifications
0:01 We've got our site up and running, but I'm sure you noticed so far
0:03 that every time you do a deployment, it can often take
0:05 5, 10, 15 minutes to get all the steps done.
0:07 As your application continues to grow in scope and complexity,
0:10 deployments can take even longer.
0:12 That's why it's really nice if you want to get a notification
0:14 when the whole thing is done or when certain steps are completed.
0:17 So in this video we'll take a look at Ansible notifications
0:20 and we'll use one type of notification as an example
0:23 and it will also teach you about other notifications,
0:27 and once you get comfortable with this notification module,
0:30 you can use any of the other ones, that come bundled as a part of core Ansible.
0:33 Let's take a look at our deployment map one more time;
0:36 we've walked through all the steps, and the final one here is an optional one
0:40 to use the Twilio Ansible module to send a text message notification
0:44 that your deployment is completed to your phone.
0:47 The way this works, when your Ansible playbook is running,
0:50 and it hits the Twilio notification task, an API call is done
0:54 from your local development environment to the Twilio service.
0:57 Twilio then sends a text message notification
1:00 from a Twilio phone number to your phone number
1:03 and you can send a notification to as many numbers as you want,
1:06 the great part is if you're working with a bunch of other people
1:08 and you are distributed, you'll all get a notification at the same time,
1:11 that the deployment has been completed.
1:13 Let's first take a look at the documentation for the Twilio module,
1:15 I wrote the Twilio module about three years ago,
1:18 when I was getting really into using Ansible, and this was the first module that I contributed,
1:21 it's pretty simple, you really just have a few arguments that you need to pass in
1:25 and let's take a look at an example,
1:28 you just tell it the message that you want to send,
1:31 your Twilio account_sid and auth_token, you can think of that
1:33 as like the hash that represents a user name password for the API
1:37 the from number, which will be a Twilio phone number
1:39 and the to number which will be your phone number
1:42 and then there's a new part of the task that we haven't seen before
1:45 which is delegate to, and this simply says
1:47 don't execute this task on the remote machine, execute it on my local machine
1:51 that way it's your local development environment calling the Twilio API
1:55 rather than all of your remote machines calling the Twilio API,
1:57 you only want to send a single text message notification.
2:00 Now, this isn't the only notification module,
2:02 there's many other ones out there bundled with Ansible,
2:05 and you can see the list here under notification modules,
2:07 some other useful ones, Slack notifications, Sendgrid,
2:11 I also happen to write the Sendgrid notification module
2:14 which just calls the Sendgrid API to send email notifications,
2:17 if you already have a mail server setup you can just use the mail notification module,
2:21 I love using Flowdoc, so Flowdoc notifications are helpful as well;
2:25 you can see there's a whole laundry list of different notifications
2:27 regardless of whatever channel that you want to get notified on
2:30 there should be something there that is bundled with Ansible.
2:32 Alright, let's send our first notification,
2:35 now we're going to need one more yaml file, we'll update our variables file
2:38 with just a few more variables.
2:41 Let's start out by creating roles/common/tasks/notify.yml.
2:45 We're only going to have a single task here, and we can technically use this task
2:50 many times throughout our deployment
2:54 if we want to send a text message notification any time in the deployment process.
2:58 So the module is Twilio, we're going to send a message
3:01 app deployment complete, we're going to specify account_sid
3:09 and we'll just have these as variables in our Ansible file, our auth_token,
3:15 and if you're wondering where these come from,
3:18 we're going to get them from the Twilio dashboard, in just a moment.
3:21 The from number will be a Twilio phone number that we have
3:25 so this will be Twilio phone number, and the to number
3:28 is the number that we want to alert, one or more numbers that we want to alert,
3:33 so we could technically use a list here, but we'll just give it
3:36 a single phone number as a part of our variable.
3:38 Now, we could also send an mms which is a picture message
3:41 and the way that we would do that is with the media url argument
3:44 but that's optional, we don't have to do that,
3:46 we'll just send a plain old text message when our deployment is complete.
3:49 We'll use delegate to local host, and so our local development environment
3:54 will execute this task and call a Twilio API, we just need to populate these variables
4:00 I am going to open up a new window here,
4:03 that way we can remember which variables that we need to populate.
4:10 Alright, let's modify our variables file here, we need a Twilio account sid,
4:16 we need a Twilio auth token, Twilio phone number,
4:19 and we don't have any of those right now, and we need an alert number
4:24 so this is where you populate your own phone number
4:27 so where do we get the account sid, auth token and the Twilio number itself?
4:30 We need to sign up for the Twilio service, we can grab a free account
4:33 and let's walk through it right now, click the sign up button on twilio.com
4:40 punch in your information to sign up for free, and then click get started.
4:46 Once you walk through the verification process,
4:49 you'll get put into the Twilio dashboard,
4:51 I'm going to log into my existing Twilio account.
4:54 Once you've signed up, you're going to get to the console dashboard,
4:57 like what you see here, this is also a trial account,
4:59 so this is the same one that you have,
5:02 I've got two things here, we've got an account sid, and we've got a hidden auth token
5:06 so I'm going to copy this account sid, and this will be what we place in here
5:12 now we need to populate the auth token,
5:15 I'll make this one visible, but I'm going to reset it after this video
5:18 so no, you cannot use my account sid and auth token.
5:24 Save that, and then we need a phone number
5:27 Twilio accounts come with trial phone numbers,
5:30 but if you don't have an active number, just click the get started button,
5:34 click get your first Twilio phone number,
5:37 choose a phone number and now we can configure this number,
5:45 click done and we're going to paste this in under our Twilio number.
5:51 We use quotes around it as well, great.
5:56 Now we can click manage numbers, click into this phone number
6:00 and we can click the messages log to see any outgoing messages
6:04 real quick before we send the text message notification,
6:07 just click configure and this brings up the number configuration screen
6:11 it just has some basic properties about our phone number,
6:14 what its capability is, so not only can we do text messaging
6:16 but we could also do voice calling, although that's not built into the Twilio Ansible module.
6:21 Then we can configure what happens when someone calls this number
6:25 and when someone text messages this number
6:27 if we go back to our messages log, we should be able to see an outgoing message
6:30 as soon as we finish kicking off our Ansible playbook.
6:33 So let's save this and quit out, and we'll go back over,
6:36 we need to modify the main.yml again, to include notify.yml.
6:41 Go under roles/commo/tasks/main.yml, and include notify.yml
6:50 save that, and now let's kick off our Ansible playbook one more time.
6:55 And of course, I had a typo of in there, so let's just modify that real quick.
7:00 It's not delete to, its delegate to, save that, let's kick this off again.
7:08 Now we should be able to walk away from our deployment
7:12 go grab a cup of coffee, and get a text message when our deployment is done.
7:18 So it looks like we had a failure in the module
7:22 and there was some sort of coding error behind it,
7:25 unfortunately this is a case where Ansible not yet completely compatible
7:29 with Python 3, the support is still in experimental mode,
7:33 because Ansible was built in Python 2, since the beginning
7:37 you may face this with some modules, so one way to get around this
7:40 is just to run a separate Python virtual env that has Python 2,
7:44 I've already set one of those up, and we're going to just activate that
7:47 so we can take a look at how will we finish this,
7:51 I just called it entre2, and I've activated it,
7:54 it has the exact same thing as our Python 3 installation
7:57 but it is running Python 2.7, instead.
8:00 Now over time, in the next few months, chances are
8:02 you won't have to worry about any of this with any of the Ansible modules
8:05 but for right now, it's good to know that sometimes
8:07 you do have to fall back to Python 2, for certain modules,
8:10 if they fail, when you're using them with Python 3.
8:13 Ansible is getting way better at Python 3 support,
8:16 but it's still one of those issues they can crop up,
8:18 let's kick this off one more time, and we've taken away the verbose output
8:21 so we can just see each task running in sequential order;
8:26 one thing to note is that just because we're using Python 2
8:30 to run Ansible on our local development environment,
8:33 doesn't mean that we're using Python 2 on the remote servers,
8:36 the remote servers are still working with Python 3,
8:38 so there is compatibility between using Ansible and Python 2 on your local system
8:43 and using Python 3 on all the deployment servers that you're working with.
8:47 So now it looks like everything was fine with the sms alert
8:49 and if you're following along at home, you should have received the text message
8:53 which will say sent from your Twilio trial account, app deployment complete.
8:57 Do you want to get rid of sent from your Twilio trial account,
8:59 or you want to send to other phone numbers, other than your own-
9:02 that's where you would upgrade your account
9:04 and then just send text messages with a standard Twilio account
9:07 or you can switch up the notification module, and use a different one to get emails
9:10 or send something into your Slack channel.
9:13 So that's how we send notifications in Ansible,
9:15 and we can get around some of the trickier issues that can occur
9:18 if certain modules error out on Python 3 compatibility during your deployment.