You are previewing Mastering Ansible.
O'Reilly logo
Mastering Ansible

Book Description

Design, develop, and solve real world automation and orchestration needs by unlocking the automation capabilities of Ansible

About This Book

  • Discover how Ansible works in detail
  • Explore use cases for Ansible’s advanced features including task delegation, fast failures, and serial task execution
  • Extend Ansible with custom modules, plugins, and inventory sources

Who This Book Is For

This book is intended for Ansible developers and operators who have an understanding of the core elements and applications but are now looking to enhance their skills in applying automation using Ansible.

What You Will Learn

  • Understand Ansible’s code and logic flow
  • Safeguard sensitive data within Ansible
  • Access and manipulate complex variable data within Ansible playbooks
  • Handle task results to manipulate change and failure definitions
  • Organize Ansible content into a simple structure
  • Craft a multi-tier rollout playbook utilizing load balancers and manipulating your monitoring system
  • Utilize advanced Ansible features to orchestrate rolling updates with almost no service disruptions
  • Troubleshoot Ansible failures to understand and resolve issues
  • Extend Ansible with custom modules, plugins, or inventory sources

In Detail

Automation is critical to success in the world of DevOps. How quickly and efficiently an application deployment can be automated, or a new infrastructure can be built up, can be the difference between a successful product or a failure. Ansible provides a simple yet powerful automation engine. Beyond the basics of Ansible lie a host of advanced features which are available to help you increase efficiency and accomplish complex orchestrations with ease.

This book provides you with the knowledge you need to understand how Ansible works at a fundamental level and leverage its advanced capabilities. You'll learn how to encrypt Ansible content at rest and decrypt data at runtime. You will master the advanced features and capabilities required to tackle the complex automation challenges of today and beyond. You will gain detailed knowledge of Ansible workflows, explore use cases for advanced features, craft well thought out orchestrations, troubleshoot unexpected behaviour, and extend Ansible through customizations. Finally, you will discover the methods used to examine and debug Ansible operations, helping you to understand and resolve issues.

Style and approach

A clear, practical guide that covers best practise, system architecture and design aspects that will help you master Ansible with ease.

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. Mastering Ansible
    1. Table of Contents
    2. Mastering Ansible
    3. Credits
    4. About the Author
    5. Acknowledgment
    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. 1. System Architecture and Design of Ansible
      1. Ansible version and configuration
      2. Inventory parsing and data sources
        1. The static inventory
        2. Inventory variable data
        3. Dynamic inventories
        4. Run-time inventory additions
        5. Inventory limiting
      3. Playbook parsing
        1. Order of operations
        2. Relative path assumptions
        3. Play behavior keys
        4. Host selection for plays and tasks
        5. Play and task names
      4. Module transport and execution
        1. Module reference
        2. Module arguments
        3. Module transport and execution
          1. Task performance
      5. Variable types and location
        1. Variable types
        2. Accessing external data
      6. Variable precedence
        1. Precedence order
          1. Extra-vars
          2. Connection variables
          3. Most everything else
          4. The rest of the inventory variables
          5. Facts discovered about a system
          6. Role defaults
        2. Merging hashes
      7. Summary
    10. 2. Protecting Your Secrets with Ansible
      1. Encrypting data at rest
        1. Things Vault can encrypt
        2. Creating new encrypted files
          1. The password prompt
          2. The password file
          3. The password script
        3. Encrypting existing files
        4. Editing encrypted files
        5. Password rotation for encrypted files
        6. Decrypting encrypted files
        7. Executing ansible-playbook with Vault-encrypted files
      2. Protecting secrets while operating
        1. Secrets transmitted to remote hosts
        2. Secrets logged to remote or local files
      3. Summary
    11. 3. Unlocking the Power of Jinja2 Templates
      1. Control structures
        1. Conditionals
          1. Inline conditionals
        2. Loops
          1. Filtering loop items
          2. Loop indexing
        3. Macros
          1. Macro variables
            1. name
            2. arguments
            3. defaults
            4. catch_kwargs
            5. catch_varargs
            6. caller
      2. Data manipulation
        1. Syntax
        2. Useful built-in filters
          1. default
          2. count
          3. random
          4. round
        3. Useful Ansible provided custom filters
          1. Filters related to task status
          2. shuffle
          3. Filters dealing with path names
            1. basename
            2. dirname
            3. expanduser
          4. Base64 encoding
          5. Searching for content
        4. Omitting undefined arguments
        5. Python object methods
          1. String methods
          2. List methods
          3. int and float methods
      3. Comparing values
        1. Comparisons
        2. Logic
        3. Tests
      4. Summary
    12. 4. Controlling Task Conditions
      1. Defining a failure
        1. Ignoring errors
        2. Defining an error condition
      2. Defining a change
        1. Special handling of the command family
        2. Suppressing a change
      3. Summary
    13. 5. Composing Reusable Ansible Content with Roles
      1. Task, handler, variable, and playbook include concepts
        1. Including tasks
          1. Passing variable values to included tasks
          2. Passing complex data to included tasks
          3. Conditional task includes
          4. Tagging included tasks
        2. Including handlers
        3. Including variables
          1. vars_files
          2. Dynamic vars_files inclusion
          3. include_vars
          4. extra-vars
        4. Including playbooks
      2. Roles
        1. Role structure
          1. Tasks
          2. Handlers
          3. Variables
          4. Modules
          5. Dependencies
          6. Files and templates
          7. Putting it all together
        2. Role dependencies
          1. Role dependency variables
          2. Tags
          3. Role dependency conditionals
        3. Role application
          1. Mixing roles and tasks
        4. Role sharing
          1. Ansible Galaxy
      3. Summary
    14. 6. Minimizing Downtime with Rolling Deployments
      1. In-place upgrades
      2. Expanding and contracting
      3. Failing fast
        1. The any_errors_fatal option
        2. The max_fail_percentage option
        3. Forcing handlers
      4. Minimizing disruptions
        1. Delaying a disruption
        2. Running destructive tasks only once
      5. Summary
    15. 7. Troubleshooting Ansible
      1. Playbook logging and verbosity
        1. Verbosity
        2. Logging
      2. Variable introspection
        1. Variable sub elements
          1. Subelement versus Python object method
      3. Debugging code execution
        1. Debugging local code
          1. Debugging inventory code
          2. Debugging Playbook code
          3. Debugging runner code
        2. Debugging remote code
          1. Debugging the action plugins
      4. Summary
    16. 8. Extending Ansible
      1. Developing modules
        1. The basic module construct
        2. Custom modules
        3. Simple module
          1. Module documentation
          2. Providing fact data
          3. Check mode
            1. Supporting check mode
            2. Handling check mode
      2. Developing plugins
        1. Connection type plugins
        2. Shell plugins
        3. Lookup plugins
        4. Vars plugins
        5. Fact caching plugins
        6. Filter plugins
        7. Callback plugins
        8. Action plugins
        9. Distributing plugins
      3. Developing dynamic inventory plugins
        1. Listing hosts
        2. Listing host variables
        3. Simple inventory plugin
          1. Optimizing script performance
      4. Summary
    17. Index