How to Manage Virtual Machines With Ansible EC2 AWS Module

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

Virtual Machines are a powerful way to manage and scale infrastructure, but managing them is not always easy. Ansible EC2 is an open source application that uses AWS’s APIs in order to automate the creation of Virtual Machines on Amazon Web Services (AWS).

Ansible is a software tool that allows you to configure and manage virtual machines with ease. The “configure ec2 instance with ansible” is a module that will allow you to use Ansible to manage your EC2 instances.

How to Manage Virtual Machines With Ansible EC2 AWS Module

Clicking through the Management Console to handle existing Amazon Web Service (AWS) EC2 instances works great. However, when your infrastructure increases, managing instances becomes time-consuming and difficult. Is there a more efficient method of managing instances? Yes! The AWS Ansible EC2 module might be of assistance.

In this tutorial, you’ll learn how to use the Ansible AWS EC2 module to manage AWS EC2 instances using an example-driven approach.

Continue reading to get started!

Prerequisites

This tutorial includes detailed instructions. If you want to join in, make sure you have the following items:

Ansible Installation Guide (Ubuntu, RHEL, CentOS, macOS)

Make that the IAM user is set up for programmatic access and is assigned to the AmazonEC2FullAccess policy.

  • Ansible commands and playbooks are executed using an inventory file and one or more hosts. Myserver is the name of the remote Linux machine, and web is the inventory group for this lesson.
  • On both the Ansible controller host and the remote node system, Python v3.6 or later is required. On an Ubuntu system, this course needs Python 3.8.10.

How Do You Install Python 3.6? Related:

  • On the Ansible controller host and the remote node system, Python modules boto3 and botocore higher than 1.15.0 should be installed.

Using Ad Hoc Commands to Start or Stop an EC2 Instance

Ad hoc instructions will suffice if you just need to create or restart a single EC2 instance on an AWS account. Ad hoc commands are a simple and fast method to establish an EC2 instance or change the instance type of an existing AWS EC2 instance.

Log into your Ansible controller and perform the ansible command below to connect to the host (-m amazon.aws.ec2 instance) (web).

The -a parameter instructs Ansible to restart the AWS EC2 instance in the us-east-2 region with instance tags=Name=Tag1). Add the aws access key and aws secret key information to the ad hoc command to authenticate the connection to the AWS account.

All of the tasks in the tutorial are performed in the us-east-2 region, but you may use any AWS region of your choosing.

state=restarted instance tags=Name=Tag1 aws access key=AKIAVWOJMI5I2DPXXXX aws secret key=F9PaprqnPUn/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Following the completion of the operation, you’ll get a CHANGED notification, as seen below, confirming that Ansible successfully restarted the AWS EC2 instance.

Restarting an Amazon EC2 Instance with an Ad Hoc Command Restarting an Amazon EC2 Instance with an Ad Hoc Command

Ansible Playbook for Creating an EC2 Instance

You’ve now learnt how to run an Ansible ad hoc command, which is useful for one-time tasks. However, you may be required to complete many tasks. Create an Ansible playbook to start an EC2 instance and perform numerous tasks if this is the case.

1. In your Ansible controller host’s terminal, use the following commands to create a directory named /ansible aws ec2 module and switch to it.

This directory will hold the playbook as well as all of the necessary configuration files for using the Ansible AWS EC2 module.

/ansible aws ec2 module/mkdir /ansible aws ec2 module cd

2. Create a file named main.yml in the /ansible aws ec2 module directory using your choice text editor. Fill the main.yml file with the YAML playbook contents below.

The job that launches an instance with a public IP address inside a certain VPC in an AWS account is described in the playbook below.

Building an AWS VPC with Terraform: A Step-by-Step Guide

Replace the values aws access key and aws secret key with your own throughout the course.

Ansible EC2 instance Launch module demo —- – name # Specifying the remote server that the Ansible EC2 module will use to administer the objects hosts: ubuntu web remote user # Ubuntu tasks using a remote user: # Start an AWS EC2 instance with a public IP address. – name: create an Amazon.aws.ec2 instance with a public IP address: # Choosing a keyname key name: mykey # Define instance type, image, vpc subnet id, assign public ip, and aws region instance type: t2.micro image: ami-0b9064170e32bde34 vpc subnet id: ami-0b9064170e32bde wait: yes assign public ip: yes aws region: us-east-2 aws access key: AKIAVWOJMI5XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX F9PaprqnPUn/NP8lzQXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

3. Invoke the playbook using the command below (main.yml). The tasks to establish a new instance in the us-east-2 region with the instance type t2.micro are then executed by the playbook.

main.yml for ansible-playbook

Some tasks have a changed status, indicating that Ansible successfully established the instance and adjusted the task’s state to perform the command. Because certain jobs do not need adjustments, you see an alright status.

Playbook Execution Playbook Execution

4. Log in to the AWS Management Console using your preferred web browser.

5. Finally, go to the top of the console’s search box, type in EC2, and choose the EC2 menu option. This will take your browser to the EC2 website.

In the AWS account, look for the EC2 service. In the AWS account, look for the EC2 service.

Your freshly formed instance will appear on the EC2 page, as seen below.

instance freshly builtinstance freshly built

Multiple AWS EC2 Instances are being terminated.

Perhaps certain Amazon EC2 instances are no longer needed. If this is the case, you may use an Ansible playbook to halt or terminate multiple instances. Set the behavior of the Ansible EC2 AWS module in stopping instances by specifying the instance IDs and declaring values in a task.

1. Create a playbook titled stop.yml in Ansible and put the code below into it.

Two instances are stopped in the playbook below (i-0d8c7eb4eb2c643a1 and i-0dbc17a67c0f7577c).

—- – name: Using the Ansible EC2 Module, stop the previously launched EC2 instances # Specifying the remote server that the Ansible EC2 module will use to administer the objects hosts: gather facts on the web: false # Using Ubuntu Remote User: ubuntu remote user instance ids: -‘i-0d8c7eb4eb2c643a1’ -‘i-0dbc17a67c0f7577c’ vars: eastern United States – 2 tasks: – name: Amazon.aws.ec2: instance ids:’instance ids’region:’region’ state: halted wait: True assign public ip: yes vpc subnet id: subnet-0dc9af4c75ad3e2ee aws access key: AKIAVWOJMI5XXXXXXXX F9PaprqnPUn/NP8lzQXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Change the status value to absent if you desire to terminate the instances rather than halt them.

2. Execute the playbook (stop.yml) with the following command, which will stop the instances you specified in the playbook. stop.yml ansible-playbook

stop.yml ansible-playbook

Using the ansible-playbook command to run the Ansible playbook. Using the ansible-playbook command to run the Ansible playbook.

3. Finally, go to your AWS instances in your web browser, and you should find two instances that have been properly halted, as seen below.

Instances That Have Stopped Instances That Have Stopped

Adding a Tag, Volume, and Cloud Watch Monitoring to an Instance

Perhaps you’ll need to add more complex features to your instance, such as tagging, monitoring using cloud watch alerts, and establishing a volume for storage. In such instance, a playbook including the Ansible EC2 AWS module would suffice.

Tags are a great method to categorize AWS resources and calculate resource costs quickly in the AWS Management Console.

1. Create a new Ansible playbook called advanced.yml and fill it with the following content.

The below playbook will launch an AWS EC2 instance with (volumes—> /dev/sdb, monitoring and tag the instance with Instance1).

—- – name: Adding Tags, Volumes, and Cloud Watch Monitoring to an Instance tasks: – name: Amazon.aws.ec2: instance type: t2.micro image: ami-0b9064170e32bde34 vpc subnet id: subnet-0dc9af4c75ad3e2ee vpc subnet id: subnet-0dc9af4c75ad3e2ee vpc subnet id: subnet- AKIAVWOJMI5I2DXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX # Creating and connecting the volumes to an AWS EC2 instance of type io1 volumes: – volume type: io1 device name: /dev/sdb volume size: 100 iops: 1000 # Tagging the AWS EC2 instance that will be launched instance tags: yes # Enabling cloud watch monitoring of the AWS EC2 instance that will be launched monitoring: yes Instance1’s name

2. Now run the following command to start the advanced.yml playbook, which will create an AWS EC2 instance with tag, volume, and cloud watch monitoring.

advanced.yml ansible-playbook

Using Ansible to Create an Instance with Tags, Volumes, and Cloud Watch Monitoring Using Ansible to Create an Instance with Tags, Volumes, and Cloud Watch Monitoring

3. In the AWS EC2 interface, go to the Storage tab of the instance you wish to check. To examine the instance’s comprehensive details, go to Block devices and choose a Volume ID from the list.

Verifying the AWS account volume generated for the AWS EC2 instance. Verifying the AWS account volume generated for the AWS EC2 instance.

4. Finally, on the instance’s summary information page, select the Tags tab. In the playbook (step one), you’ll notice the tag you assigned to the instance, which looks like the one below.

Verifying the tags for the AWS EC2 instance in the AWS account. Verifying the tags for the AWS EC2 instance in the AWS account.

Conclusion

You used the Ansible AWS EC2 module to control AWS EC2 instances with a single command in this lesson. You also learnt how to modify AWS EC2 instances by restarting, terminating, and adding tags, among other things.

Are you ready to make the Ansible EC2 AWS module a part of your instance management routine now that you have a good understanding of it? Perhaps you’d want to create a cron job to automate the process?

Related:Using PHP to Run and List Cron Jobs on a Linux System

Ansible is an open source automation tool that allows users to manage multiple machines from a single machine. The “ansible ec2_instance_info example” shows how Ansible can be used with EC2 AWS Module.

Related Tags

  • ansible ec2_instance
  • ansible ec2 user_data example
  • ansible list all ec2 instances
  • ansible ec2 dynamic inventory
  • create multiple ec2 instance using ansible playbook

Table of Content