Skip to content

LakshmiDevopsTech/ASG-Rolling-Update

Repository files navigation

ASG Rolling Update(Ansible + Jenkins)

Description

Ansible Role that can be used either for doing Rolling Deployment on the ASG. Here i'm explaining a method that lead me to a easy work. This is an Ansible playbook for rolling update in ASG. This we automating with Jenkins. Usually in a ELB working with ASG while updateing the new site contents through git, we prefer increase the number of instances in ASG and once contents updated will remove old instances. Its practically very difficuly. Using this work we can easily update new changes in ASG manually or automatically.

Features

  • Included Auto Scaling Group, and ELB in this playbook
  • No need to create manuel inventory file. We're fetching instances details created by asg through Dynamic Inventory file.

Pre-Requests

  • Install Ansible on your Master Machine (localhost)
  • Install pip, boto, boto3 and botocore
  • Install jenkins, Git
  • Create an IAM user role under your AWS account and please enter the values once the playbook running time or attach the role to ansible master server.
Ansible Modules used

Architacture

alt text

Behind the Playbook

I just explaining some Important parts of the playbook here. For detaild code, please check .yml file.

# create ASG
- name: "create ASG"
       ec2_asg:
         name: "{{ name }}_asg"
         launch_config_name: "{{ lc_status.name }}" <--- lc creation check .yml file
         load_balancers: "{{ lb_status.elb.name }}" <---- elb creation, check .yml file
         health_check_period: 30
         health_check_type: EC2
         replace_all_instances: yes
         min_size: 3
         max_size: 3
         desired_capacity: 3
         region: "{{ region }}"
         tags:
           - Name: devops-web
             propagate_at_launch: true
       register: asg_status
     - name: "asg_status"
       debug:
         var: "asg_status.auto_scaling_group_name"
 
 # Dynamic Inventory creation
 - name: "collect ec2 instance details created by asg"
       ec2_instance_info:
         region: "{{ region }}"
         filters:
           "tag:aws:autoscaling:groupName": "{{ asg_status.auto_scaling_group_name }}"
           instance-state-name: [ "running"]
       register: instance_status
     - name:
       debug:
         var: instance_status

     - name: "Aws - Creating Dynamic Inventory"
       add_host:   <---------- Module for create Dynamic Inventory file
         name: "{{ item.public_ip_address }}" <--- public ip of instances by asg
         groups: "instances" <-- Group name in inventory file (hosts)
         ansible_host: "{{ item.public_ip_address }}" 
         ansible_port: 22
         ansible_user: "ec2-user"
         ansible_ssh_private_key_file: "/root/{{ key }}.pem"
         ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
       with_items:
         - "{{ instance_status.instances }}" <-- status of instances created by asg
         
# Rolling update
 - name: "Rolling update starting"
   hosts: instances <-- inventory file group (hosts )
   become: true
   serial: 1
   vars_files:
     - update.vars
   tasks:

     - name: "Cloning Git repository"
       git:
         repo: "{{ git_url }}"
         dest: /var/contents/
       register: repo_status

Important Jenkins Configurations

alt_text

alt_text

alt_text

For automate this Process we can include Git webhook also.

Conclusion

The above playbook created for ASG rolling update without recreate instances. Please see the asg.vars and update.vars for check variables and see ansible-asg.yml for playbook details.

About

ASG rolling update with Ansible

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages