Updating an Ubuntu EC2 Instance with Ansible

Updating an Ubuntu EC2 Instance with Ansible

A while back, I wrote up a post showing how you can easily and automatically update your servers using Ansible.  More recently, I've started to familiarize myself with AWS and have been tinkering with EC2 instances.

Having become accustomed to easily updating all of my server with a simple command, I decided I would figure out how to get my EC2 instance working with the apt module of Ansible and to no surprise, it's incredibly easy.

Prerequisites

Before starting, you will need to have generated a keypair and have stored your private key on your ansible server. I put mine in /home/tcude/.ssh. Remember this location for later on.

Getting started

To start, you will need edit your hosts file and add your EC2 instance name to it. You can find it shown in the image below

Once you've obtained your IPv4 DNS name, we'll add it to our hosts file, along with a couple of other bits:

#aws
ec2-XX-XX-XX-XX.us-east-2.compute.amazonaws.com ansible_ssh_user=ubuntu ansible_ssh_private_key_file=/home/tcude/.ssh/keypair-main.pem

To break this down, we're adding ec2-XX-XX-XX-XX.us-east-2.compute.amazonaws.com, our host, then ansible_ssh_user=ubuntu, the user we want to SSH into the host as, and finally ansible_ssh_private_key_file=/home/tcude/.ssh/keypair-main.pem, the location of our private key.

Once added, you should now be able to use Ansible to update all of your hosts, as normal.

Conclusion

That's all there is to it! It's simply a matter of adding the host with a couple of extra steps