Monday, 28 September 2020

How to create Ec2 instance using Ansible Tower /AWX


. The Collection (requirements.yml)

AWX uses Execution Environments, but it still looks for a collections/requirements.yml file in your Git repository to ensure the AWS modules are present.

In your Git Repo, create this file structure:

Plaintext
my-awx-repo/
├── collections/
│   └── requirements.yml
└── deploy_ec2.yml

Content of collections/requirements.yml:

YAML
---
collections:
  - name: amazon.aws
    version: 7.1.0



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 ii. Go to your AWS account...

Click on  IAM


Go to Users


Click Add Users

Create a New User , u can call it Tower


Attach Existing  Policy




Give it AdministratorAccess and create user


Download ur keys



Step A: Create a New branch 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 ACCESS KEY secret_key: YOUR SECRET KEY tasks: - name: Provision instance amazon.aws.ec2_instance: aws_access_key: "{{ access_key}}" aws_secret_key: "{{ secret_key }}" key_name: "{{ pem_key }}" instance_type: t2.micro image_id: ami-02f3416038bdb17fb wait: yes count: 1 region: us-east-2 tags: Name: test-ec2

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

Bash Script To Install Ansible Automation Platform ( AWX)

#!/bin/bash # --- Configuration --- AWX_OPERATOR_VERSION="2.19.1" NAMESPACE="awx" KUBECONFIG_PATH="/etc/rancher/k3s...