Reduce your wrinkles over the skin With Total Age Repair

Cellulite SK Lab Skincare Reviews It’s terribly important to scrub your face. Before bed and when you first rouse, particularly. Many skin face wash and cleansers contain active ingredients like…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Managing Autoscaled EC2 instances with Ansible and Jenkins

When running EC2 instances through the autoscaling group or by ECS (Elastic Container Service -EC2 type) we must keep our instances updated with the latest packages and security updates.

Using Ansible with a dynamic inventory approach makes it possible to run ansible playbooks on EC2 instances running inside your cluster. This approach not only will work for ECS clusters but also for any of your manual/autoscaled EC2 instances. The only requirement for this to work is to make sure that you add custom tags on your EC2 instances and you have a working SSH connection to these instances.

Ansible is a great tool to manage your EC2 instances. By using EC2 tags and Ansible dynamic inventory to query those tags makes it a lot easier and convenient to manage instances and make configuration changes in them.

Each EC2 tag is a simple label consisting of a customer-defined key and an optional value that can make it easier to manage, search for, and filter resources. They enable you to categorize resources by purpose, owner, environment, or other criteria. For this tutorial, you can see my EC2 instance which I have tagged key “Name” with value “Ansible-test-slave”.

This tutorial is intended for :

I assume the following :

So, let’s begin.

2. Download dynamic inventory files. ec2.py and ec2.ini from the following links

I will be copying these files in the directory /etc/ansible

3. Export their path as

To make this change persistent, add the following lines in /etc/environment

4. Uncomment the following lines in /etc/ec2.ini file to make sure that Ansible does not query for RDS and Elasticache cluster.

5. If you want ec2.py to return you the list of all the EC2 instances based on their private IP address (like in my use case) and not the Public IP addresses which it does by default just modify the following lines in /etc/ansible/ec2.ini

6. To get rid of nasty ssh key file errors. Configure Ansible to disable host key checking in the file /etc/ansible/ansible.cfg.

7. Now, give execution permission to the ec2.py file.

8. Make sure that the EC2 instance from which you are running Ansible (Jenkins Server) has an IAM role attached to it which should permit it to list all the EC2 instances otherwise you will get permission denied error. I used the IAM role instead of defining credentials in ec2.ini as it is best practice as far as security is concerned.

9. Now, try to get the list of instances using the following commands

This will give you a list of all the ec2 instances.

10. In my case, the EC2 instances in ECS had an EC2 tag “Name” with value “Ansible-test-slave”

11. Test ansible can ping the instances or not using

The above command should return a success message. If it doesn’t make sure you have allowed ssh on the target instances security group from the ansible server.

12. Create Ansible Playbooks

a. /etc/ansible/playbooks/ping.yml

b. /etc/ansible/playbooks/update-upgrade.yml

13. Execute the playbook using the following command.

When you execute the above command from the command line of Ansible server (Jenkins) you will be able to run your ansible-playbook /etc/ansible/playbooks/ping.yml which will report success if it’s able to connect with the target servers.

In the second part, we will see how you can automate the execution of this playbook by creating a Freestyle Jenkins job.

In this second part, I will tell you how to automate the ansible-playbook execution using a Jenkins freestyle job.

Go to “Manage Jenkins” -> “Manage Plugins” -> Search for “Ansible plugin”

2. Configure Ansible plugin in Jenkins.

Go to “Manage Jenkins” -> “Global Tool Configuration” -> “Ansible”

As shown in the image below.

3. Add your target EC2 instances SSH keys in the Credentials section of Jenkins as shown in the image.

4. Create a freestyle Jenkins job with the following parameters in the Build section.

Click on the Advanced button and enter the following details.

5. Select the option “This Project is Parameterized” and add the two variables as Choice Parameter shown in the image below.

6. After making all these changes save your Jenkins job and you can execute it manually as shown in the image below or you can also set it to be executed as cronjob by setting the time of execution in the “Build periodically” option.

After following the above steps you will have a working Jenkins freestyle job by which you can execute your ansible-playbooks and define the EC2 instances on which it has to be executed by specifying the EC2 tags.

As in autoscaling, EC2 instances can be initialized and destroyed as the load increases or decreases but by using Ansible dynamic inventory through Jenkins we can easily perform day to day activities like (instances patching, software installation, bootstrapping, etc.) no matter the number of instances that are running as it will automatically query for the instances IP addresses based on the EC2 tag we define.

Add a comment

Related posts:

Unha decorada

Saiba como aumentar sua renda Familiar!. “Unha decorada” is published by ITRETOUCH Tratamento Imagens.

Allowance is the Powerful Antidote to Judgment

Your orientation in life determines how you see and feel your life experiences. Orientation also affects your choices. Look closely at your beliefs to discover where you are judging others and yourself.

How to implement Microservices using Spring boot and java

This article is a continuation of my previous article on how to design the microservices, I will explain how we can build micro services using spring boot, eureka server and java and will fit with…