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 Playbook Essentials
Ansible Playbook Essentials

Ansible Playbook Essentials: Design automation blueprints to manage your multitier infrastructure

eBook
$17.99 $25.99
Paperback
$32.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
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Ansible Playbook Essentials

Setting Up the Learning Environment

To use this book most effectively and to examine, run, and write code that is part of exercises supplied in this book, it's essential to set up a learning environment. Even though Ansible can work with any type of nodes, virtual machines, cloud servers, or bare metal hosts with an operating system installed and SSH service running, the preferred mode is to use virtual machines.

In this session, we will cover the following topics:

  • Understanding the learning environment
  • Understanding the pre requisites
  • Installing and configuring virtualbox and vagrant
  • Creating virtual machines
  • Installing Ansible
  • Using the sample code

Understanding the learning environment

We assume that most of our learners would like to set up the environment locally, and thus recommend using the open source and freely available software VirtualBox and Vagrant, which have support for most of the desktop operating systems, including Windows, OSX, and Linux.

The ideal setup includes five virtual machines, the purpose of which is explained as follows. You can also consolidate some of the services, for example, the load balancer and web server can be the same host:

  • Controller: This is the only host that needs to have Ansible installed, and works as a controller. This is used to launch the ansible-playbook commands from the controller.
  • Database (Ubuntu): This host is configured with Ansible to run the MySQL database service and runs the Ubuntu distribution of Linux.
  • Database (CentOS): This host is configured with Ansible to run the MySQL database service, however, it runs the CentOS distribution of Linux. This is added to test multiplatform support while writing the MySQL role for Ansible.
  • Web Server: This host is configured with Ansible to run the Apache web server application.
  • Load balancer: This host gets configured with the haproxy application, which is an open source HTTP proxy service. This host acts as a load balancer, which accepts HTTP requests and spreads the load across available web servers.

Prerequisites

For most up-to-date instructions on prerequisites, software and hardware requirements, as well as setup instructions, refer to the following GitHub repository:

https://github.com/schoolofdevops/ansible-playbook-essentials.

Systems prerequisites

A moderately configured desktop or a laptop system should be sufficient to set up the learning environment. The following are the recommended prerequisites in the context of of software and hardware:

Processor

2 cores

Memory

2.5 GB RAM available

Disk Space

20 GB of free space

Operating System

Windows, OS X (Mac), Linux

The base software

For the purpose of setting up the learning environment, we recommend using the following software:

  • VirtualBox: Oracle's virtualbox is a desktop virtualization software, which is freely available. It works on a variety of operating systems, including Windows, OS X, Linux, FreeBSD, Solaris, and so on. It provides a layer of hypervisor and allows one to create and run virtual machines on top of an existing base OS. The code provided along with this book has been tested on 4.3x versions of virtualbox. However, any version of virtualbox, which is compatible with the vagrant version can be used.
  • Vagrant: This is a tool that allows one to easily create and share virtual environments on most hypervisors and cloud platforms, including but not limited to virtualbox. It can automate tasks such as importing an image, specifying resources, such as memory and CPUs assigned to VMs, and setting up network interfaces, hostnames, user credentials, and so on. Since it provides a text configuration in the form of a Vagrant file, virtual machines can be provisioned programmatically, making them easy to use it with other tools such as Jenkins to automate build and test pipelines.
  • Git for Windows: Even though we do not intend to use Git, which is a version control software, we use this software to install the SSH utility on the Windows system. Vagrant needs an SSH binary available in the path. Windows is not packaged with the SSH utility, and Git for Windows is the easiest way to install it on Windows. Alternative options such as Cygwin exist.

The following table lists the version OS the software used to develop the code provided with the book, with download links:

Software

Version

Download URI

VirtualBox

4.3.30

https://www.virtualbox.org/wiki/Downloads

Vagrant

1.7.3

https://www.vagrantup.com/downloads.html

Git for Windows

1.9.5

https://git-scm.com/download/win

Learners are advised to download, install, and refer to the respective documentation pages to get familiar with these tools before proceeding.

Creating virtual machines

Once you have installed the base software, you can use vagrant to bring up the virtual machines required. Vagrant uses a specification file by the name Vagrantfile, a sample of which is as follows:

# -*- mode: ruby -*-
# vi: set ft=ruby :
# Sample Vagranfile to setup Learning Environment
# for Ansible Playbook Essentials

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ansible-ubuntu-1204-i386"
  config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/precise/current/precise-server-cloudimg-i386-vagrant-disk1.box"
  config.vm.define "control" do |control|
    control.vm.network :private_network, ip: "192.168.61.10"
  end
  config.vm.define "db" do |db|
    db.vm.network :private_network, ip: "192.168.61.11"
  end
  config.vm.define "dbel" do |db|
    db.vm.network :private_network, ip: "192.168.61.14"
    db.vm.box = "opscode_centos-6.5-i386"
    db.vm.box = "http://opscode-vm-bento.s3.amazonaws.com/vagrant/virtualbox/opscode_centos-6.5_chef-provisionerless.box"
  end
  config.vm.define "www" do |www|
    www.vm.network :private_network, ip: "192.168.61.12"
  end
  config.vm.define "lb" do |lb|
    lb.vm.network :private_network, ip: "192.168.61.13"
  end
end

The preceding Vagrant file contains specifications to set up five virtual machines, as described in the beginning of this chapter, which are, control, db, dbel, www, and lb.

Learners are advised to use following instructions to create and start the virtual machines required to set up the learning environment:

  1. Create a directory structure for the learning environment setup, for example, learn/ansible, anywhere on the system.
  2. Copy the Vagrantfile file provided previously to the learn/ansible directory. The tree should now look as follows:
                         learn
                            \_ ansible
                                  \_ Vagrantfile

    Note

    The Vagrantfile file contains specifications for the virtual machines described in the earlier section.

  3. Open a terminal and go to learn/ansible.
  4. Bring up the control node and log in to it, as follows:
    $ vagrant up control 
    $ vagrant ssh control
    
  5. From a separate terminal window, from the learn/ansible directory, bring up the remaining virtual machine, one at a time, as follows:
    $ vagrant up db
    $ vagrant up www
    $ vagrant up lb
    optionally (for centos based mysql configurations)
    $ vagrant up dbel 
    Optionally, to login to to the virtual machines as
    $ vagrant ssh db
    $ vagrant ssh www
    $ vagrant ssh lb
    optionally (for centos based mysql configurations)
    $ vagrant ssh dbel 
    

Installing Ansible on the controller

Once the virtual machines are created and brought up, Ansible needs to be installed on the controller. Since Ansible is agentless and manages nodes using SSH transport, no additional setup is needed on the nodes except for ensuring that the SSH service is running. To install Ansible on the controller, refer to the following steps. These instructions are specific to the Ubuntu distribution of Linux, as that's what we use on our controller. For generic installation instructions, please refer to the following page:

http://docs.ansible.com/intro_installation.html.

The steps are as follows:

  1. Log in to the controller using the following command:
    # from inside learn/ansible directory   
    $ vagrant ssh control 
    
  2. Update the repository cache using the following command:
    $ sudo apt-get update
    
  3. Install the prerequisite software and repositories:
    # On Ubuntu 14.04 and above 
    $ sudo apt-get install -y software-properties-common
    $ sudo apt-get install -y python-software-properties
    $ sudo apt-add-repository ppa:ansible/ansible
    
  4. Update the repository cache after adding a new repository, such as follows:
    $ sudo apt-get update

    
  5. Install Ansible using the following command:
    $ sudo apt-get install -y ansible 
    
  6. Validate Ansible using the following command:
    $ ansible --version
    [sample output]
    vagrant@vagrant:~$ ansible --version
    ansible 1.9.2
      configured module search path = None
    

Using sample code

The sample code provided with this book is divided as per the chapter numbers. A directory named after the chapter number contains the snapshot of the state of the code at the end of the respective chapter. Learners are advised to independently create their own code and use the sample code as a reference. Moreover, if the readers skip one or more chapters, they can use the sample code from the previous chapter as a base.

For example, while using Chapter 6, Iterative Control Structures – Loops, you can use the sample code from Chapter 5, Controlling Execution Flow – Conditionals, as a base.

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Left arrow icon Right arrow icon

Description

Ansible Playbook Essentials will show you how to write a blueprint of your infrastructure, encompassing multitier applications using Ansible's playbooks. Beginning with basic concepts such as plays, tasks, handlers, inventory, and YAML Ain't Markup Language (YAML) syntax that Ansible uses, you'll understand how to organize your code into a modular structure. Building on this, you will study techniques to create data-driven playbooks with variables, templates, logical constructs, and encrypted data, which will further strengthen your application skills in Ansible. Adding to this, the book will also take you through advanced clustering concepts, such as discovering topology information about other nodes in the cluster and managing multiple environments with isolated configurations. As you approach the concluding chapters, you can expect to learn about orchestrating infrastructure and deploying applications in a coordinated manner. By the end of this book, you will be able to design solutions to your automation and orchestration problems using playbooks quickly and efficiently.

Who is this book for?

If you are a systems or an automation engineer who wants to automate common infrastructure tasks, deploy applications, and use orchestration to configure systems in a synchronized manner, this book is ideal for you. Some familiarity with the Linux/Unix command-line interface is necessary.

What you will learn

  • Write simple tasks and plays
  • Organize code into a reusable, modular structure
  • Separate code from data using variables and Jinja2 templates
  • Run custom commands and scripts using Ansible s command modules
  • Control execution flow based on conditionals
  • Integrate nodes and discover topology information about other nodes in the cluster
  • Encrypt data with Ansible Vault
  • Create environments with isolated configurations to match the application development workflow
  • Orchestrate infrastructure and deploy applications in a coordinated manner

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Aug 05, 2015
Length: 168 pages
Edition : 1st
Language : English
ISBN-13 : 9781784395612
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
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Aug 05, 2015
Length: 168 pages
Edition : 1st
Language : English
ISBN-13 : 9781784395612
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 $ 130.97
Learning Ansible 2
$48.99
Mastering Ansible
$48.99
Ansible Playbook Essentials
$32.99
Total $ 130.97 Stars icon

Table of Contents

13 Chapters
Setting Up the Learning Environment Chevron down icon Chevron up icon
1. Blueprinting Your Infrastructure Chevron down icon Chevron up icon
2. Going Modular with Ansible Roles Chevron down icon Chevron up icon
3. Separating Code and Data – Variables, Facts, and Templates Chevron down icon Chevron up icon
4. Bringing In Your Code – Custom Commands and Scripts Chevron down icon Chevron up icon
5. Controlling Execution Flow – Conditionals Chevron down icon Chevron up icon
6. Iterative Control Structures – Loops Chevron down icon Chevron up icon
7. Node Discovery and Clustering Chevron down icon Chevron up icon
8. Encrypting Data with Vault Chevron down icon Chevron up icon
9. Managing Environments Chevron down icon Chevron up icon
10. Orchestrating Infrastructure with Ansible Chevron down icon Chevron up icon
A. References Chevron down icon Chevron up icon
Index Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.4
(14 Ratings)
5 star 50%
4 star 42.9%
3 star 7.1%
2 star 0%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




Thomas Jul 28, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Straight to the point
Subscriber review Packt
CHRIS Sep 22, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
The book flows well and was really interesting it would have been nice to see a chapter on networking but it was really good read.
Amazon Verified review Amazon
C. C Chin Dec 09, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Work uses Ansible
Amazon Verified review Amazon
Martin Oct 13, 2017
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Great book to push ansible further then just installing some packages.
Amazon Verified review Amazon
digitalshadow Mar 26, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you're new to Ansible and are looking for a quick guide to get up to speed quickly, then this is the book for you. Clear, concise explanations and examples of the topics you need to know to get started with Ansible. Once you finish reading this book, you'll be ready for other books that go into more complex topics and applications of Ansible.
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.