Provisioning AWS Using Ansible.

Christopher Quiles
5 min readDec 30, 2020

--

Step by step explanation and resources for provisioning AWS EC2 instances using Ansible Playbooks.

Why Use Ansible for AWS?

Ansible is a simple IT automation engine that automates cloud provisioning, configuration management, application deployment, intra-service orchestration, and many other IT needs.

The benefit of Ansible is that it enables you to automate cloud deployments. You can use it to manage applications and services using automation playbooks. Each playbook defines a set of configurations, which is used consistently across cloud environments.

Project Requirements & Setup:

In this demonstration ill be using a CentOS 7 server:

Server is from a CentOS 7 playground in Linux Academy.

Of course you can also create a CentOS 7 sever from AWS as well. You can follow the video for step by step directions. https://youtu.be/nAN3PSVYEHA

These installs will be addressed at during different parts of the lab.

Installing Ansible:

Follow the steps below by copying and pasting into your terminal and editors

1. sudo su2. yum install ansible

NOTE: Make sure to have the latest version of Ansible. Steps 3 & 4 are from the following link. https://fedoraproject.org/wiki/EPEL

3. yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm4. yum install ansible (yes again)

YUM is the primary tool for getting, installing, deleting, querying, and managing Red Hat Enterprise Linux RPM software packages from official Red Hat software repositories, as well as other third-party repositories.

5. ansible --version
6. cd /etc/ansible/
7. ls -lrt

Creating EC2 instances with Ansible Playbooks.

Github link for playbooks: https://github.com/quiwest/AWS-Provisioning-using-Ansible

Create a directory to work with AWS Cloud.

1. mkdir AWS_CLOUD
2. cd AWS_CLOUD

In order for the EC2 module to run from the playbook you have to install Boto3. It is the Amazon Web Services (AWS) SDK for Python. It enables Python developers to create, configure, and manage AWS services, such as EC2 and S3

3. pip install boto boto3
You don’t need to be in ROOT to install Boto and don’t mind the ( — user) part.

Save your AWS credentials. First by downloading AWS CLI. The AWS CLI is a unified tool to manage your AWS services from a terminal session on your own client.

4. pip install awscli
5. aws configure
6. ~/.aws/credentials

Now that the AWS credentials are stored. Create a playbook to provision the EC2 instance and using VIM to create the Ansible playbook.

7. touch launch_ec2.yml8. vim launch_ec2.yml

Launching the Ansible Playbook:

The key_name section is optional here. Make sure to provide the AMI ID you want here.

Explaining the Playbook:

Hosts = This is the target section. We are not working with server, we are going to work with cloud, regardless we need a target, so we’ll use localhost.

Connection = We are not using ssh so it’ll be local.

Gather_facts = This is going to be FALSE since we are working with cloud, we don’t need to gather any facts.

Tasks = This is where we’ll draw up our playbook, and tell Ansible what exactly we want the playbook to do. In this case launch an instance. For each task you need to implement a module.

Link for Ansible Modules: https://docs.ansible.com/ansible/2.9/modules/list_of_cloud_modules.html

Run the Ansible Playbook:

9. ansible-playbook launch_ec2.yml
You should see an EC2 instance created in your AWS console.
You can start as many instances as you want just be changing the “COUNT” in the playbook.

Launch EC2 instances with multiple security groups & tags:

Tags enable you to categorize your AWS resources in different ways, for example, by purpose, owner, or environment. This is useful when you have many resources of the same type — you can quickly identify a specific resource based on the tags that you’ve assigned to it.

Make sure to customize the key_name and the security groups.
The created instance New Node matches the name in the instance tags section of our playbook.

Single Ansible playbook to start, stop, and terminate instances:

This playbook will allow you to easily start, stop, and terminate instances.

The tag -never will not allow this task to run, unless called upon during the run.
— -syntax-check lets you check to see if you having an errors in your playbook before running it.
-t is for task, in this run we are calling the start task.
This run we are calling the stop task.
This run we are calling the terminate task.

Conclusion:

Learning from this project Ansible can be very simple to set up and use. No special coding skills are necessary to use playbooks. Ansible allows you to orchestrate the entire application environment no matter where it’s deployed.

If you have any questions feel free to contact me at quileswest@gmail.com or on Linked In at https://www.linkedin.com/in/quiwest/ thank you!

--

--

No responses yet