Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Ansible 2 Cloud Automation Cookbook
Ansible 2 Cloud Automation Cookbook

Ansible 2 Cloud Automation Cookbook: Write Ansible playbooks for AWS, Google Cloud, Microsoft Azure, and OpenStack

Arrow left icon
Profile Icon Patawari Profile Icon Aggarwal
Arrow right icon
Free Trial
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.5 (2 Ratings)
Paperback Feb 2018 200 pages 1st Edition
eBook
NZ$31.99 NZ$45.99
Paperback
NZ$56.99
Subscription
Free Trial
Arrow left icon
Profile Icon Patawari Profile Icon Aggarwal
Arrow right icon
Free Trial
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.5 (2 Ratings)
Paperback Feb 2018 200 pages 1st Edition
eBook
NZ$31.99 NZ$45.99
Paperback
NZ$56.99
Subscription
Free Trial
eBook
NZ$31.99 NZ$45.99
Paperback
NZ$56.99
Subscription
Free Trial

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

Ansible 2 Cloud Automation Cookbook

Using Ansible to Manage AWS EC2

In this chapter, we will cover the following recipes:

  • Preparing Ansible to work with AWS
  • Creating and managing a VPC
  • Creating and managing security groups
  • Creating EC2 instances
  • Creating and assigning Elastic IPs
  • Attaching volumes to instances
  • Creating an Amazon Machine Image
  • Creating an Elastic Load Balancer and attaching to EC2 instances
  • Creating auto scaling groups
  • Deploying the phonebook application

Introduction

Amazon Web Services (AWS) is one of the most popular cloud providers out there. It consists of over 15 regions geographically located across 4 continents, and provides over 70 different services. It serves customers in over 190 countries, including several governments. Elastic Compute Cloud, better known as EC2, launched in 2006, and is one of the most popular services of AWS. Some of the most popular components of EC2 are:

  • Instances (virtual machines)
  • Volumes (disks)
  • Amazon Machine Image or AMI (disk snapshots)
  • Security groups (firewalls)
  • Elastic Load Balancers (application and network load balancers)
  • Auto scaling groups (to scale instances)

Most of the basic building blocks for running a simple application are provided by AWS EC2 itself.

Preparing Ansible to work with AWS

Ansible interacts with AWS APIs to manage various infrastructure components. Using APIs requires credentials and an IAM user with the right permissions would help us do this. We also need to set up some libraries that would be required to execute certain Ansible modules. So let us get started.

How to do it...

Ansible ships with scores of AWS modules. These Ansible modules use AWS Python SDK, called Boto, as dependency and interact with AWS.

  1. Let us install Boto using Python pip to get started:
$ pip install boto
  1. Along with Boto, we also need to have a user who has enough privileges to create and delete AWS resources. AWS has a predefined policy called AmazonEC2FullAccess which can be...

Creating and managing a VPC

Virtual Private Cloud, or VPC, is technically not a part of EC2. However, this is usually the first step when getting started with EC2. VPC creates a virtual network which logically isolates our resources. This improves security and management since, logically, subnet and gateway are dedicated for our resources only. A common usage of VPC is to isolate public-facing services (like load balancers or instances running public services) and servers storing data (like databases) which do not require direct access from the wider internet.

Technically, a VPC has several moving parts, as depicted in the preceding image. Even a simple architecture would consist of the following components:

  • The VPC itself, where we will allocate a high-level Classless InterDomain Routing (CIDR) block and choose a region.
  • A public subnet, which will use a chunk of CIDR from...

Creating and managing security groups

EC2 security groups are virtual firewalls, which control inbound and outbound traffic to and from our EC2 Instance. We will create security groups before an EC2 Instance because this resource is required for creating an EC2 instance. Security groups and EC2 instances have many-to-many relationships. We can have a single instance with multiple security groups and a single security group can be applied to multiple instances, even multiple AWS instances present in the same subnet can have different security groups.

How to do it...

We can create a security group, using an ec2_group module, this will take the VPC ID, the region, and rules as input.
Let's create a task for a security group...

Creating EC2 instances

Elastic Cloud Compute or EC2, forms a central part of Amazon Web Services. It is one of the most popular services by AWS, which provides rented virtual computers (called instances) under various capacities in terms of CPU, memory, disk, network, and so on where users can run and host their applications.

Getting ready

Before going ahead and launching an EC2 Instance, we will require resources we created in preceding tasks; that is, security groups and VPC (subnets).

Creating an EC2 instance requires the following basic parameters:

  • Instance type, which determines hardware of the host computer used for our instance. An instance type offers a wide range of compute, memory, network, and storage capacity...

Creating and assigning Elastic IPs

An Elastic IP address, also known as EIP is a static IPv4 address, which can be assigned to an EC2 instance. The Elastic IP address is generally used for covering the failure of an EC2 Instance by quickly remapping it to another EC2 instance. An Elastic IP address is allocated to an AWS account and can only be used in a specific region. That is, we cannot associate an Elastic IP address allocated in one region to an EC2 instance in a different region.

How to do it...

We can allocate and associate an Elastic IP address with an EC2 instance using an ec2_ip module. This will require the instance ID of the EC2 instance we want to associate this Elastic IP address with and the region of that instance...

Attaching volumes to instances

Amazon Elastic Block Storage, also known as EBS, is block-level storage that can be attached to an EC2 Instance. Once we attach an EBS volume, we can create a file system on top of these volumes, mount it, and use it like an attached disk, technically as block storage. Amazon EBS volumes are placed in a specific availability zone and they can only be attached to an EC2 instance present in the same availability zone. EBS volumes are automatically replicated to protect our data from the failure of a single component.
In this section, we will be writing a task to create an EBS volume and attaching it to an existing EC2 instance (which was created in previous tasks).

Getting ready

Before we write...

Creating an Amazon Machine Image

We have already discussed Amazon Machine Image (AMI) while creating EC2 instances. AMI is the most common way to create backup images of an EC2 Instance. For the sake of consistency, while creating an AMI, AWS reboots the instance by default. However, this can be overridden and AMIs can be created without rebooting the EC2 Instance. AMIs store references to device mapping with corresponding volume snapshots. AMIs can be used to define a standard template which can be used while launching EC2 instances. For example, we may want to use a standard flavor of Linux with predefined system configurations across all EC2 instances. We can create an AMI for this and use it while creating EC2 instances.

How to do it...

...

Creating an Elastic Load Balancer and attaching to EC2 instances

Often, for the purpose of load balancing, as well as to achieve high availability, we may choose to serve the traffic using a load balancer. AWS provides a virtual load balancer, called Elastic Load Balancer (ELB), which can be used to receive traffic from clients and route the traffic to a set of instances which are attached to the ELB. Apart from stability, the ELB has quite a few features which can help in improving the overall uptime. One of the most important of these features is health check. This lets ELB determine that an attached instance has gone bad and it should stop routing traffic to it. ELB can also insert cookies which can be used for making routing decisions and it can be used to offload SSL from an application.

...

Creating auto scaling groups

So far, we have seen various services provided by AWS EC2. We have also seen the dynamic nature of the cloud that lets us spin any number of instances, volumes, load balancers, and so on.

When we deploy an application in production, we are likely to see non-uniform traffic patterns.

We might see a pattern where peak time starts at mid-afternoon and ends at midnight. For such cases, we might need to add more resources at certain times to keep our application latency uniform. Using auto scaling groups, we can achieve this goal. AWS EC2 provides three major components for auto scaling EC2 instances:

  • Launch Configurations act a template for auto scaling groups to launch EC2 instances. It contains information like the AMI ID, the security group, and so on required to launch an EC2 instance.
  • Auto Scaling Groups is a collection of EC2 instances which...

Deploying the phonebook application

Our phonebook application can be deployed to the instances that we have created. When deploying an application to an instance, we either need to know the IP address of the instance and prepare the inventory, or we can figure out the IP address at runtime. Preparing the inventory is often simple, however, it requires manual intervention. We have to run tasks to boot an EC2 instance with the required parameters and copy the IP address of the instance to the inventory file. After this, we can run the playbook for deploying the application.

Manually adding IPs to the inventory is not possible for unattended setups. In certain cases, the infrastructure is dynamic to the extent that managing IPs might not even be possible. For such cases, there are two possibilities: we can use Ansible's add_host module to deploy an application when we boot up...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Recipe-based approach to install and configure cloud resources using Ansible
  • Covers various cloud-related modules and their functionalities
  • Includes deployment of a sample application to the cloud resources that we create
  • Learn the best possible way to manage and automate your cloud infrastructure

Description

Ansible has a large collection of inbuilt modules to manage various cloud resources. The book begins with the concepts needed to safeguard your credentials and explain how you interact with cloud providers to manage resources. Each chapter begins with an introduction and prerequisites to use the right modules to manage a given cloud provider. Learn about Amazon Web Services, Google Cloud, Microsoft Azure, and other providers. Each chapter shows you how to create basic computing resources, which you can then use to deploy an application. Finally, you will be able to deploy a sample application to demonstrate various usage patterns and utilities of resources.

Who is this book for?

If you are a system administrator, infrastructure engineer, or a DevOps engineer who wants to obtain practical knowledge about Ansible and its cloud deliverables, then this book is for you. Recipes in this book are designed for people who would like to manage their cloud infrastructures efficiently using Ansible, which is regarded as one of the best tools for cloud management and automation.

What you will learn

  • Use Ansible Vault to protect secrets
  • Understand how Ansible modules interact with cloud providers to manage resources
  • Build cloud-based resources for your application
  • Create resources beyond simple virtual machines
  • Write tasks that can be reused to create resources multiple times
  • Work with self-hosted clouds such as OpenStack and Docker
  • Deploy a multi-tier application on various cloud providers

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Feb 28, 2018
Length: 200 pages
Edition : 1st
Language : English
ISBN-13 : 9781788295826
Vendor :
Red Hat
Languages :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Feb 28, 2018
Length: 200 pages
Edition : 1st
Language : English
ISBN-13 : 9781788295826
Vendor :
Red Hat
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just NZ$7 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just NZ$7 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total NZ$ 128.98
Security Automation with Ansible 2
NZ$71.99
Ansible 2 Cloud Automation Cookbook
NZ$56.99
Total NZ$ 128.98 Stars icon

Table of Contents

10 Chapters
Getting Started with Ansible and Cloud Management Chevron down icon Chevron up icon
Using Ansible to Manage AWS EC2 Chevron down icon Chevron up icon
Managing Amazon Web Services with Ansible Chevron down icon Chevron up icon
Exploring Google Cloud Platform with Ansible Chevron down icon Chevron up icon
Building Infrastructure with Microsoft Azure and Ansible Chevron down icon Chevron up icon
Working with DigitalOcean and Ansible Chevron down icon Chevron up icon
Running Containers with Docker and Ansible Chevron down icon Chevron up icon
Diving into OpenStack with Ansible Chevron down icon Chevron up icon
Ansible Tower Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Half star icon Empty star icon Empty star icon 2.5
(2 Ratings)
5 star 0%
4 star 0%
3 star 50%
2 star 50%
1 star 0%
jhitz Sep 15, 2020
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Most of what's in this book is cloud related, so if your not working w/ AWS, Azure, Google limited value.
Amazon Verified review Amazon
Manjunath N Vijayakumar May 31, 2019
Full star icon Full star icon Empty star icon Empty star icon Empty star icon 2
got a indian edition. which i got at cost price . i could have got in local store for 15% Discount. giving content is 5 stars but giving 2 stars for false advertising
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.