HomeAutomation Tools

Ansible Playbook #3: Spinning up the EC2 instance – AWS

Ansible Playbook #3: Spinning up the EC2 instance – AWS
Like Tweet Pin it Share Share Email
Personal Development Category (English)728x90

In this article, I am going to demonstrate the writing an ansible playbook for spinning up the EC2 instance through an AWS CLI API request from AWS CLI Server. I think you will enjoy working on  AWS Command Line Interface. If you’re already configured with AWS CLI on your server, use that machine to run your ansible playbook.

Spinning up the EC2 instance - AWS

Suggestable Read:

1. Introduction and Installation & Configuration of Ansible 2.3 on CentOS/Redhat Linux 7

2. Configuring Ansible Dynamic Inventory for AWS EC2

3. Creating an Ansible static inventory with variables

4. Part #1: Getting Started with Ansible Ad-Hoc Commands

5. Part #2: Getting Started with Ansible Ad-Hoc Commands

6.  Ansible: Getting Started with Ansible Playbook

7.  Ansible Playbook #2: Adding Multiple Users with Sudo access and private key

Also, Read: 

 1. How to Install and Configure AWS CLI on Linux

I know, here you may get a question that, how I got/Collected all the below information like AMI, VPC,  vpc_subnet_id, key_name and more from AWS.

It’s not a big question. Here is the below example AWSCLI command to get all the details.

Example #1

Check the Ansible-Doc page for more information and related examples.


Examples #2:

If you have already running instance on aws, then use the below command to get all the instance details in JSON format and collect the information from it and create your own playbook.


Example #3:

Using AWS CLI, you can get the details whatever you required to launch the ec2 instance from the API request.


Demo Playbook for spinning up the EC2 instance (AWS EC2 Instance).

Playbook Tasks: 

# 1. Create a Custome Security Group in AWS with allowing ports 22/SSH, 80/HTTP and ICMP.

# 2. Spinning up the EC2 instance – Using EC2 Module

# 3. Wait for the SSH Protocol Come up on Instance


Note:  In this playbook, I am using my custom AMI and ssh_keys which are already available on AWS. You can also create your own AMI and ssh_keys or directly you use the available resources in AWS.

Example commands:

1.The below command will create a new ssh_key and will display you the private key.

2. The below command will create a new Amazon Machine Image( AMI ) from the selected running instance.





spinning up the EC2 instance

Task Verification:

Now you can use the AWS EC2 dynamic inventory to get the hosts details and status.


Now just run the ping module to check the host status. Here I am directly using my EC2 instance username and the private_key file.


After successfully spinning up the EC2 instance, you can log in it using the public ip as shown like below.

spinning up the EC2 instance


Conclusion:  That’s all about spinning up the EC2 instance. I hope this will help you to get more familiar with Automation with Ansible. Next, I will come up with the more useful information like writing the playbooks to manage your infrastructure. If you find useful, please do share the others and command on below if any questions/suggestions.

Follow our Youtube Channel for more about Ansible Tutorials :

Check out the below Video for More about spinning up the EC2 instance.

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.