Server Provisioning in AWS CLOUD by using Ansible Playbook

Prince MishraPrince Mishra
2 min read

Hello, everyone, I am showing with you how to install ec2 instance by using Ansible.

Pre-requisite: -

  1. Install any Linux OS

  2. Install Ansible

So, in this demo, I am using Amazon Linux OS for installing Ansible.

when you settle Amazon Linux, try to install ansible-core.

yum install ansible-core

For this demo, I am using localhost. [means no inventory]

after downloading ansible, now we download AWS module because AWS module not installed initially, we have to download it from Ansible Galaxy.

Step to copy module path for installation

Go to chrome and Search Ansible Galaxy

Click on Ansible Galaxy and open collection.

after clicking on collection search in keyword for AWS

  • Open AWS BY clicking. And you get installation page. and copy Installation path. And paste on your server where ansible in installed.

ansible-galaxy collection install amazon.aws

Go to content and search for ec2_instance.

  • click on ec2_instance and Check Requirement

    After checking Requirement, you see 2 module you needed boto3 and botocore for this you have pip3 if not then install pip3.

    then install boto3 and botocore.

    Now you are ready to create an ansible playbook by using parameter shown in below image.

    So first create an YML file by using below command and write playbook by using different parameter. [for secret and access key, you have to login on AWS and go to credentials and click on create. ]

  •   vi instance.yml
    

    And write below code to launch instance in AWS cloud.

 - hosts: localhost
   tasks:
    - amazon.aws.ec2_instance:
        name: "Redhat_OS_testing"
        image_id: "ami-05a5bb48beb785bf1"
        instance_type: "t2.micro"
        count: 1
        region: "ap-south-1"
        access_key: "AKIARPHUIJ7T6YFDPN3D"
        secret_key: "Kk39cBov/ZIMiuPYFCfQvycKm8HOt***"

And write in vi editor by pressing i and after write click Esc and then type :wq! (to save)

and now check syntax error if possible.

ansible-playbook --syntax-check instance1.yml

And finally run playbook by using below command

ansible-playbook instance1.yml

Now you can see the result in AWS Cloud

Thank you so much for reading my article. ๐Ÿ™

0
Subscribe to my newsletter

Read articles from Prince Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prince Mishra
Prince Mishra