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

Preface

With the evolution of cloud computing, agile development methodologies, and explosion of data in recent years, there is a growing need to manage infrastructure at scale. DevOps tools and practices have become a necessity to automate every stage of such a scalable, dynamic, and complex infrastructure. Configuration management tools are at the core of this devops tool set.

Ansible is a simple, efficient, fast configuration management, orchestration and application deployment tool all combined into one. This book helps you to get familiar with writing Playbooks, Ansible's automation language. This book follows a hands-on approach to show you how to create flexible, dynamic, reusable, and data-driven roles. It then takes you through the advanced features of Ansible, such as node discovery, clustering, securing data with vault, and managing environments, and finally shows you how to use Ansible to orchestrate multitier infrastructure stack.

What this book covers

Chapter 1, Blueprinting Your Infrastructure, will introduced you to Playbooks, YAML, and so on. You will also learn about the components of a playbook.

Chapter 2, Going Modular with Ansible Roles, will demonstrate creating modular reusable automation code using Ansible roles, which are units of automation.

Chapter 3, Separating Code and Data – Variables, Facts, and Templates, covers the creation of flexible, customizable, data-driven roles with templates and variables. You will also learn about auto-discovered variables, that is, facts.

Chapter 4, Bringing In Your Code – Custom Commands and Scripts, covers bringing in your existing scripts and invoking Shell commands with Ansible.

Chapter 5, Controlling Execution Flow – Conditionals, discusses the control structures offered by Ansible to change the direction of execution.

Chapter 6, Iterative Control Structures – Loops, demonstrates how to iterate over arrays, hashes, and so on, using omnipotent with statements.

Chapter 7, Node Discovery and Clustering, discusses the discovery of topology information and creates dynamic configurations using magic variables and facts caching.

Chapter 8, Encrypting Data with Vault, discusses secure variables stored in, and shared with, version control systems using Ansible-vault.

Chapter 9, Managing Environments, covers the creation and management of isolated environments with Ansible and mapping automation code to the software development workflow.

Chapter 10, Orchestrating Infrastructure with Ansible, covers the orchestration features of Ansible, such as rolling updates, pre-tasks and post-tasks, tags, building tests into the playbook, and so on.

What you need for this book

This book assumes that you have a working installation of Ansible and a good knowledge of Linux/Unix environments and systems operations, and that you are familiar working with the command-line interface.

Who this book is for

The target audience for this book are systems or automation engineers with a few years of experience in managing various parts of infrastructure, including operating systems, application configurations, and deployments. This book also targets anyone who intends to manage systems and application configurations effectively and in an automated way, with the shortest learning curve.

It is assumed that readers have a conceptual understanding of Ansible, have already installed it and are familiar with basic operations such as creating inventory file and running ad hoc commands with Ansible.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

---
# site.yml : This is a sitewide playbook
- include: www.yml

Any command-line input or output is written as follows:

$ ansible-playbook simple_playbook.yml -i customhosts

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "The resultant variable hash should contain items from defaults plus the overridden values from vars".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

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.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.