You are previewing Ansible Playbook Essentials.
O'Reilly logo
Ansible Playbook Essentials

Book Description

Design automation blueprints using Ansible's playbooks to orchestrate and manage your multi-tier infrastructure

About This Book

  • Get to grips with Ansible’s features such as orchestration, automatic node discovery, and data encryption
  • Create data-driven, modular and reusable automation code with Ansible roles, facts, variables, and templates
  • A step-by-step approach to automating and managing system and application configurations effectively using Ansible's playbooks
  • Who This Book Is For

    If you are a systems or automation engineer who intends to automate common infrastructure tasks, deploy applications, and use orchestration to configure systems in a co-ordinated manner, then this book is for you. Some understanding of the Linux/UNIX command line interface is expected.

    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 application development workflow
  • Orchestrate infrastructure and deploy applications in a coordinated manner
  • In Detail

    Ansible combines configuration management, orchestration, and parallel command execution into a single tool. Its batteries-included approach and built-in module library makes it easy to integrate it with cloud platforms, databases, and notification services without requiring additional plugins. Playbooks in Ansible define the policies your systems under management enforce. They facilitate effective configuration management rather than running ad hoc scripts to deploy complex applications.

    This book will show you how to write a blueprint of your infrastructure encompassing multi-tier applications using Ansible's playbooks. Beginning with the basic concepts such as plays, tasks, handlers, inventory, and the YAML syntax that Ansible uses, you will see how to organize your code into a modular structure. Building on this, you will master techniques to create data-driven playbooks with variables, templates, logical constructs, and encrypted data. This book will also take you through advanced clustering concepts such as discovering topology information, managing multiple environments, and orchestration.

    By the end of this book, you will be able to design solutions to your automation and orchestration problems using playbooks quickly and efficiently.

    Style and approach

    This book follows a step-by-step approach, with the concepts explained in a conversational and easy-to-follow style. Each topic is explained sequentially in the process of creating a course. A comprehensive explanation of the basic and advanced features of Ansible playbooks is also included.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Ansible Playbook Essentials
      1. Table of Contents
      2. Ansible Playbook Essentials
      3. Credits
      4. About the Author
      5. Acknowledgments
      6. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      9. Setting Up the Learning Environment
        1. Understanding the learning environment
          1. Prerequisites
            1. Systems prerequisites
          2. The base software
          3. Creating virtual machines
          4. Installing Ansible on the controller
          5. Using sample code
      10. 1. Blueprinting Your Infrastructure
        1. Getting introduced to Ansible
        2. Plays
          1. YAML – the playbook language
        3. Our first playbook
          1. Creating a host inventory
          2. Patterns
          3. Tasks
            1. Modules
              1. The modules and idempotence
          4. Running the playbook
        4. Review questions
        5. Summary
      11. 2. Going Modular with Ansible Roles
        1. Understanding roles
        2. Naming roles
        3. The directory layout for roles
        4. Creating a site-wide playbook, nesting, and using include statements
        5. Creating the www playbook
          1. The default and custom role paths
          2. Parameterizing the roles
        6. Creating a base role
          1. Refactoring our code – creating a base role
        7. Creating an Nginx role
          1. Adding role dependencies
          2. Managing files for Nginx
        8. Automating events and actions with handlers
        9. Adding pre-tasks and post-tasks to playbooks
        10. Running playbooks with roles
        11. Review questions
        12. Summary
      12. 3. Separating Code and Data – Variables, Facts, and Templates
        1. Static content explosion
        2. Separating code and data
        3. Jinja2 templates
          1. The template formation
        4. Facts and variables
          1. Automatic variables – facts
          2. User-defined variables
            1. Where to define a variable
            2. How to define a variable
        5. Templating the Nginx configurations
        6. Adding another layer – the MySQL role
          1. Creating the scaffolding for the roles with Ansible-Galaxy
          2. Adding metadata to the role
          3. Using variables in tasks and handlers
            1. Creating variables
            2. Creating tasks
          4. Using variables in playbooks
          5. Applying a MySQL role to the DB servers
        7. Variable precedence
        8. The best practices for variable usage
        9. Review questions
        10. Summary
      13. 4. Bringing In Your Code – Custom Commands and Scripts
        1. The command modules
          1. Using the raw module
          2. Using the command module
          3. Using the shell module
          4. Using the script module
        2. Deploying a WordPress application – a hands-on approach
          1. Installing WordPress
            1. Controlling the idempotence of command modules
            2. The registered variables
            3. Extracting WordPress with a shell module
          2. Configuring WordPress
        3. Review questions
        4. Summary
      14. 5. Controlling Execution Flow – Conditionals
        1. The conditional control structure
          1. The when statements
            1. Fact-based selection
        2. Refactoring the MySQL role
          1. Multilevel variable dictionaries
            1. Merging hashes
          2. Configuring the MySQL server selectively
        3. Conditional control structure in Jinja2 templates
          1. Updating the MySQL template
        4. Running a task only once
        5. Executing roles conditionally
        6. Review questions
        7. Summary
      15. 6. Iterative Control Structures – Loops
        1. The omnipotent with statement
        2. Configuring WordPress requisites
        3. The PHP5-FPM role
          1. Defining an array
          2. Looping an array
        4. Creating MySQL databases and user accounts
          1. Creating a hash
            1. Nested hashes
          2. Iterating a hash
        5. Creating Nginx virtual hosts
          1. Defining the PHP site information
        6. Review questions
        7. Summary
      16. 7. Node Discovery and Clustering
        1. Node discovery with magic variables
        2. Creating the load balancer role
        3. Accessing facts for non-playbook hosts
          1. Facts caching with Redis
          2. Caching facts in files
        4. Review questions
        5. Summary
      17. 8. Encrypting Data with Vault
        1. Ansible-vault
          1. Advanced Encryption Standard
          2. What to encrypt with the vault?
        2. Using the Ansible-vault
          1. Encrypting the data
          2. Updating the encrypted data
          3. Rotating the encryption keys
        3. Encrypting the database credentials
        4. Using a password file
        5. Adding the vault password file option to the Ansible configuration
          1. Using encrypted data in templates
          2. Adding SSL support to Nginx
        6. Review questions
        7. Summary
      18. 9. Managing Environments
        1. Approaches for managing environments
          1. The inventory groups and variables
          2. Approach 1 – using nested groups in an inventory
          3. Approach 2 – using environment-specific inventory variables
        2. Creating a development environment
        3. Review questions
        4. Summary
      19. 10. Orchestrating Infrastructure with Ansible
        1. Ansible as an orchestrator
          1. Multiple playbooks and ordering
          2. Pre-tasks and post-tasks
          3. Delegation
          4. Rolling updates
          5. Tests
          6. Tags
          7. Patterns and limits
        2. Tagging the roles
        3. Creating an orchestration playbook for WordPress
        4. Review questions
        5. Summary
      20. A. References
      21. Index