Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
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
€15.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
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
Estimated delivery fee Deliver to Lithuania

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

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 Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Lithuania

Premium delivery 7 - 10 business days

€25.95
(Includes tracking information)

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
€18.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
€189.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
€264.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 66.98
Security Automation with Ansible 2
€36.99
Ansible 2 Cloud Automation Cookbook
€29.99
Total 66.98 Stars icon
Banner background image

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 the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact [email protected] with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at [email protected] using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on [email protected] with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on [email protected] within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on [email protected] who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on [email protected] within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela