Python for Entrepreneurs Transcripts
Chapter: Deploying to the cloud
Lecture: Setting up notifications

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


Talk Python's Mastodon Michael Kennedy's Mastodon