Book Image

Ansible Playbook Essentials

By : Gourav Shah, GOURAV JAWAHAR SHAH
Book Image

Ansible Playbook Essentials

By: Gourav Shah, GOURAV JAWAHAR SHAH

Overview of this book

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.
Table of Contents (20 chapters)
Ansible Playbook Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Setting Up the Learning Environment
References
Index

Getting introduced to Ansible


Ansible is a simple, flexible, and extremely powerful tool that gives you the ability to automate common infrastructure tasks, run ad hoc commands, and deploy multitier applications spanning multiple machines. Even though you can use Ansible to launch commands on a number of hosts in parallel, the real power lies in managing those using playbooks.

As systems engineer, infrastructure that we typically need to automate contains complex multitier applications. Each of which represents a class of servers, for example, load balancers, web servers, database servers, caching applications, and middleware queues. Since many of these applications have to work in tandem to provide a service, there is topology involved as well. For example, a load balancer would connect to web servers, which in turn read/write to a database and connect to the caching server to fetch in-memory objects. Most of the time, when we launch such application stacks, we need to configure these components in a very specific order.

Here is an example of a very common three-tier web application running a load balancer, a web server, and a database backend:

Ansible lets you translate this diagram into a blueprint, which defines your infrastructure policies. The format used to specify such policies is what playbooks are.

Example policies and the sequence in which those are to be applied is shown in the following steps:

  1. Install, configure, and start the MySQL service on the database servers.

  2. Install and configure the web servers that run Nginx with PHP bindings.

  3. Deploy a Wordpress application on the web servers and add respective configurations to Nginx.

  4. Start the Nginx service on all web servers after deploying Wordpress. Finally, install, configure, and start the haproxy service on the load balancer hosts. Update haproxy configurations with the hostnames of all the web servers created earlier.

The following is a sample playbook that translates the infrastructure blueprint into policies enforceable by Ansible: