Ansible: Automate Apps and IT Infrastructure with GCP and AWS

Bismo Baruno
5 min readMay 9, 2020

--

Picture by pngtree.com

Hi everyone! With me again and again to share some experience, though or opinion about technology related with software engineering field. In this story, I will share about automation tool for our infrastructure. There are several tool for do this, like Ansible, Puppet and Chef. According to the title, we will try Ansible for this experiment.

What’s Ansible?

Ansible is the simplest way to automate apps and IT infrastructure. Application Deployment + Configuration Management + Continuous Delivery.

The detail for Ansible can be found on their official page https://www.ansible.com/

Why we use Ansible? If we want to manage our infrastructure, it is easy if we just have a few server. But how about if we have a dozens or even hundreds server that we should do the repetitive task for each server? It will be complicated and waste a lot of time.

Picture 1 Managing Dozens Infrastructure

So, with Ansible, we can simplify our process just with a configuration file. Let’s jump into technical side!

We will use GCP and AWS as cloud computing service. Make sure we already have a valid GCP and AWS account and ready to make some instances.

Our goal is achieve this deployment on my previous story (https://medium.com/@bismobaruno/f7e01693b645) with Ansible.

1. Setup Ansible

First, install Ansible on our local machine. Because it’s depend on our OS, then check the guide of installation on this link https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html

Because I’m using macOS, I will install Ansible using homebrew.

brew install ansible

And verified the installation of Ansible.

ansible --version
Picture 2 Ansible Check Version

2. Setup GCP Instance

Next, we will try to manage 3 instances for this experiment. Go to GCP console page https://console.cloud.google.com/

Choose VM instances submenu from Compute Engine menu

Picture 3 Go to VM Instances

Click Create button for creating VM instances.

Picture 4 Create VM Instances

We will modify several configuration like Machine type, Boot disk and Firewall. Leave the rest by default. Click Create button for finishing this step.

Picture 5.1 Fill Instance Configuration
Picture 5.2 Fill Instance Configuration

Repeat the creating instance step above until we have 3 instances.

Picture 6 Instance List

Don’t forget to add our public key to metadata.

Picture 7 Go to Metadata

Choose SSH Keys tab and click Add SSH keys button.

Picture 8 Go to SSH Keys
Picture 9 Go to Add SSH Keys

Copy our public key with this command:

pbcopy < ~/.ssh/id_rsa.pub

If we don’t have any ssh key, first we should generate it. Follow this tutorial link https://git-scm.com/book/en/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key

Back to GCP console, input our public key into column.

Picture 10 Add SSH Keys

3. Setup Playbook & Inventory

Create our project directory that contain this file.

Picture 11 Project Directories

4. Running

If we access the Public IP of the instances, it would be not reachable like this.

Picture 12 Instances not Reachable

We need to run our playbook file with this command:

ansible-playbook -i inventory playbook.yml -b

Wait until Ansible done with the task.

Picture 13 Ansible Done with the Task

5. Testing

Re-access the Public IP of the instances, we should be getting this response.

Picture 14 Instances Reachable

Banzai, it’s works! Now you can access all instances!

But, waiittt.. how about AWS ? Okay, let’s try on AWS environment too.

6. Setup AWS Instance

Go to AWS console https://aws.amazon.com/console/ and choose EC2 service.

Picture 15 Go to EC2 Service

Scroll down on sidebar menu, on Network & Security category, choose Key Pairs menu,

Picture 16 Go to Key Pairs

Click on Actions button and choose Import key pair.

Picture 17 Go to Import Key Pair

Copy our public key with this command:

pbcopy < ~/.ssh/id_rsa.pub

And paste into column. Don’t forget add the Name and click Import key pair button for proceed.

Picture 18 Import Key Pair

Next, create instance by clicking Instances menu.

Picture 19 Go to Instances List

Click Launch Instance for creating the instance

Picture 20 Go to Launch Instance

We will use Amazon Linux AMI for this instance. Use the default value for each step until step 6. And add HTTP rule for allow port 80.

Picture 21 Choose AMI
Picture 22 Add HTTP Rule

Choose key pair that we created using own public key. Then click Launch Instances for creating the instance.

Picture 23 Launch Instances

Back to Instances list page and try to access the Public IP or Public DNS. We will get the response not reachable.

Picture 24 Copy Public IP or Public DNS
Picture 25 Instance Not Reachable

We need to modify playbook.yml for supporting Amazon Linux AMI.

Don’t forget to modify the inventory Public IP:

[web]
52.32.74.56

Re-run our playbook file with this command:

ansible-playbook -i inventory playbook.yml -b

Wait until Ansible done with the task.

Picture 26 Ansible Done with the Task

Check the instance again and ….

Picture 26 Instance Reachable

Banzai, it’s works! Now you can access the instance!

If you wan to see the example project, you can visit my github repository

Hope you enjoy it, I’m happy if this article useful for you! Happy Provisioning!

Thank you!

--

--