Ansible: Automate Apps and IT Infrastructure with GCP and AWS
--
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.
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
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
Click Create button for creating 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.
Repeat the creating instance step above until we have 3 instances.
Don’t forget to add our public key to metadata.
Choose SSH Keys tab and click Add SSH keys button.
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.
3. Setup Playbook & Inventory
Create our project directory that contain this file.
4. Running
If we access the Public IP of the instances, it would be not reachable like this.
We need to run our playbook file with this command:
ansible-playbook -i inventory playbook.yml -b
Wait until Ansible done with the task.
5. Testing
Re-access the Public IP of the instances, we should be getting this response.
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.
Scroll down on sidebar menu, on Network & Security category, choose Key Pairs menu,
Click on Actions button and choose 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.
Next, create instance by clicking Instances menu.
Click Launch Instance for creating the 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.
Choose key pair that we created using own public key. Then click Launch Instances for creating the instance.
Back to Instances list page and try to access the Public IP or Public DNS. We will get the response 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.
Check the instance again and ….
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!