Search icon CANCEL
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

eBook
$20.98 $29.99
Paperback
$38.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
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 : 9781788298773
Vendor :
Red Hat
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want

Product Details

Publication date : Feb 28, 2018
Length: 200 pages
Edition : 1st
Language : English
ISBN-13 : 9781788298773
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 $5 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 $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 87.98
Security Automation with Ansible 2
$48.99
Ansible 2 Cloud Automation Cookbook
$38.99
Total $ 87.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

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.