Monday, 28 September 2020

How to create Ec2 instance using Ansible Tower /AWX


Step i: Install amazon.aws collection, Boto

connect to your awx server using mobaxterm and enter

pip3 install boto3

 ansible-galaxy collection install amazon.aws

Step ii: Specify your collection path in the ansible config file

vi /etc/ansible/ansible.cfg


Insert the below line

collections_paths = /home/ubuntu/.ansible/collections/ansible_collections


Save



Step A: Create a New branch from myfirstrepo Repository in bitbucket


 Name the branch : Ansible

Step B: Open git bash in your project folder and do a git pull to get the latest branch update

cd myfirstrepo

git pull
git checkout Ansible

This will switch to the newly created Ansible branch



Launch Vscode by entering code .

Step C: Playbook to create EC2 instance

Create a new file in Vscode called aws.yml

Paste the below code in the file:

---
- hosts: "{{ host }}"
gather_facts: true
vars:
access_key: YOUR_ACCESSKEY
secret_key: YOUR_SECRETKEY
tasks:
- name: Provision instance
ec2:
aws_access_key: "{{ access_key}}"
aws_secret_key: "{{ secret_key }}"
key_name: "{{ pem_key }}"
instance_type: t2.micro
image: ami-03657b56516ab7912
wait: yes
count: 1
region: us-east-2

Save the file....Add commit message and push....



Step 1: First create an Organization: Devops

Description: All Devops Projects



Save

Step 2: Create Credentials for SCM: 

Click Credentials ....Click the +

Enter a Name for your credential. Any Name will work. I used BITBUCKET
Select Credential Type:Source Control
Enter bitbucket username and password
Save






Step 3: Create a Project: AWS-Instance-Creation . Select Org you just created

Choose SCM Type: Git

Enter SCM http url

Select the SCM credentials you created

Enter Branch: Ansible

Check UPDATE REVISION ON LAUNCH



Save

Step 4 Create Template
Select Templates 
Click +  Select Job Template to create a New template
Enter Template Name: T2-Micro-ubuntu-18-instance
Select Inventory: Demo inventory This specifies the server where we want to run the playbook. Demo inventory is localhost,we will be running the Playbook on the awx server
Select Project: AWS-Instance-Creation
Select Playbook: aws.yml
Verbosity: Select 4
Extra Variables : Copy and paste below code
---
host: localhost
pem_key: YOUR_KEY_name





SAVE
 
GO TO TEMPLATES....CLICK THE ROCKET BUTTON TO LAUNCH

THIS WILL PROVISION YOUR INSTANCE



No comments:

Post a Comment