I just try to a new explanation method for easy to convey my work and details. Basically, this is an ansible-playbook with ASG Rolling update and Create an AWS Infrastructure with (ELB ASG Security Group). Its automated with Jenkins.
Client Query: I have an ELB (Elastic LoadBalancer) in amazon and that ELB under instances is registered from an ASG. Also, the developers are uploaded the site contents to the git, and the developers make updates on git (ELB git changes through user-data with git). So, that's very complicated each update time has to change the count of ASG but it's very annoying and expensive creates and removes instances unwanted is there any solution?
Answer: We have created a ASG oriented ansible playbook with dynamic inventory and its help to update git contents which if the current available instances and you can use this manually or automate via jenkins like (continues deployment) and who use the playbook it never needs to create instances unwanted.
- ASG Rolling updates through ansible-playbook (Primary)
- Includs ELB ASG Security group infrastructure on this ansible-playbook
- No need for hosts (Inventory file) for ASG under client servers. Because its work with Dynamic Inventory
- Furthermore, I have included a test website with user-data for more clarification
- Easy to handle and everyone can change the ASG (Count, Project_Name.. etc values)
- No need to install any dependencies like boto and boto3 (Please note that if you have using Ansible 2.2 and python 2.7)
- Install Ansible on your Master Machine
- Create an IAM user role under your AWS account and please enter the values once the playbook running time
Ansible2 (For your reference visit How to install Ansible)
- yum
- pip
- ec2-key
- copy
- ec2-group
- debug
- ec2_lc
- ec2_elb_lb
- ec2_asg
- ec2_instance_info
- add_host
- git
- file
- pause
Ansible Installation article is in pre-request section so please check out the pre-request section.
amzon-linux-extras install -y ansible2
yum install git -y
git clone https://github.com/yousafkhamza/ASG-rolling-update.git
cd ASG-rolling-update
---Please-Change Your-Credentials---
ansible-playbook main.yml
- Architacture
I just explained the primary thing ASG Rolling update and Which variables I used so if you have any further doubts please look at the YAML file complete.
# Dynmic Inventory Creation
- name: "Task 07-Fetch ASG Created EC2 Details"
ec2_instance_info:
aws_access_key: "{{ access_key }}"
aws_secret_key: "{{ secret_key }}"
region: "{{ region }}"
filters:
"tag:aws:autoscaling:groupName": "{{ asg_status.auto_scaling_group_name }}" <------- this is your ASG Name
instance-state-name: [ "running"]
register: asg_instances
- name: "Task 08-Autoscale - Creating Dynamic Inventory Of Autoscaling EC2"
add_host: <---------- Creating Dynmic Inventory
name: "{{ item.public_ip_address }}"
groups: "asg"
ansible_host: "{{ item.public_ip_address }}"
ansible_port: 22
ansible_user: "ec2-user"
ansible_ssh_private_key_file: ansible.pem
ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
with_items:
- "{{ asg_instances.instances }}"
# Play 02-Dyanamic Inventory Rolling Update. (Dynamic Inventory Play starts)
- name: "Task Dynamic Inventory Play 09-ASG Rolling Update Start"
hosts: asg <------------ host works with dynmic inventory (hosts)
become: true
serial: 1
gather_facts: False
vars_files:
- instance.vars
tasks:
- asg.vars
env: "ansible_project" <---------- ProjectName
ami: "ami-0d5eff06f840b45e9" <---------- I used us-east-1 region so the AMI under us-east-1
key: "ansible" <-------------- Creating A keypair for entering ASG under instances
region: "us-east-1" <-------------- Region
sg_name: sgroup <--------------- Security group were I used.
type: "t2.micro" <--------------- Instance_type
count: "2" <----------------- ASG Count
- cred.vars
access_key: "<your-access-key>" <------------------ Enter your IAM Access Key
secret_key: "<your-secret-key>" <------------------ Enter your IAM Secret Key
This playbook is used for ASG rolling update with a website contents from git without recreate instances. Furthermore, I tried to create a additional AWS infrastructure with a (ELB ASG Lauch Configuration Security Group) through Ansible. So, you guys please reffer the playbook for creating infrastructure and dynamic inventory works with localhost inventory