Mastering Ansible - Third Edition

Book description

Design, develop, and solve real-world automation and orchestration problems by unlocking the automation capabilities of Ansible.

Key Features

  • Tackle complex automation challenges with the newly added features in Ansible 2.7

Book Description

Automation is essential for success in the modern world of DevOps. Ansible provides a simple, yet powerful, automation engine for tackling complex automation challenges.

This book will take you on a journey that will help you exploit the latest version's advanced features to help you increase efficiency and accomplish complex orchestrations. This book will help you understand how Ansible 2.7 works at a fundamental level and will also teach you to leverage its advanced capabilities. Throughout this book, you will learn how to encrypt Ansible content at rest and decrypt data at runtime. Next, this book will act as an ideal resource to help you master the advanced features and capabilities required to tackle complex automation challenges. Later, it will walk you through workflows, use cases, orchestrations, troubleshooting, and Ansible extensions. Lastly, you will examine and debug Ansible operations, helping you to understand and resolve issues.

By the end of the book, you will be able to unlock the true power of the Ansible automation engine and tackle complex, real- world actions with ease.

What you will learn

  • Gain an in-depth understanding of how Ansible works under the hood
  • Fully automate Ansible playbook executions with encrypted data
  • Access and manipulate variable data within playbooks
  • Use blocks to perform failure recovery or cleanup
  • Explore the Playbook debugger and the Ansible Console
  • Troubleshoot unexpected behavior effectively
  • Work with cloud infrastructure providers and container systems
  • Develop custom modules, plugins, and dynamic inventory sources

Who this book is for

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

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Mastering Ansible Third Edition
  3. About Packt
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Download the color images
      3. Code in Action
      4. Conventions used
    4. Get in touch
      1. Reviews
  6. Section 1: Ansible Overview and Fundamentals
  7. The System Architecture and Design of Ansible
    1. Technical requirements
    2. Ansible version and configuration
    3. Inventory parsing and data sources
      1. Static inventory
      2. Inventory ordering
      3. Inventory variable data
      4. Dynamic inventories
      5. Runtime inventory additions
      6. Inventory limiting
    4. Playbook parsing
      1. Order of operations
      2. Relative path assumptions
      3. Play behavior directives
      4. Execution strategies
      5. Host selection for plays and tasks
      6. Play and task names
    5. Module transport and execution
      1. Module reference
      2. Module arguments
      3. Module blacklisting
      4. Module transport and execution
        1. Task performance
    6. Variable types and location
      1. Variable types
      2. Magic variables
    7. Accessing external data
    8. Variable precedence
      1. Precedence order
      2. Variable group priority ordering
      3. Merging hashes
    9. Summary
  8. Protecting Your Secrets with Ansible
    1. Technical requirements
    2. Encrypting data at rest
      1. Vault IDs and passwords
      2. Things Vault can encrypt
      3. Creating new encrypted files
        1. Password prompt
        2. Password file
        3. Password script
      4. Encrypting existing files
      5. Editing encrypted files
      6. Password rotation on encrypted files
      7. Decrypting encrypted files
      8. Executing Ansible-playbook with encrypted files
    3. Mixing encrypted data with plain YAML
    4. Protecting secrets while operating
      1. Secrets transmitted to remote hosts
      2. Secrets logged to remote or local files
    5. Summary
  9. Ansible and Windows - Not Just for Linux
    1. Technical requirements
    2. Running Ansible from Windows
      1. Checking your build
      2. Enabling WSL
      3. Installing Linux under WSL
    3. Setting up Windows hosts for Ansible control
      1. System requirements for automation with Ansible
      2. Enabling the WinRM listener
      3. Connecting Ansible to Windows
    4. Handling Windows authentication and encryption
      1. Authentication mechanisms
      2. A note on accounts
      3. Certificate validation
    5. Automating Windows tasks with Ansible
      1. Picking the right module
      2. Installing software
      3. Extending beyond modules
    6. Summary
  10. Infrastructure Management for Enterprises with AWX
    1. Technical requirements
    2. Getting AWX up and running
    3. Integrating AWX with your first playbook
      1. Defining a project
      2. Defining an inventory
      3. Defining credentials
      4. Defining a template
    4. Going beyond the basics
      1. Role-based access control (RBAC)
      2. Organizations
      3. Scheduling
      4. Auditing
      5. Surveys
      6. Workflow templates
      7. Notifications
    5. Summary
  11. Section 2: Writing and Troubleshooting Ansible Playbooks
  12. Unlocking the Power of Jinja2 Templates
    1. Technical requirements
    2. 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
    3. 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
    4. Comparing values
      1. Comparisons
      2. Logic
      3. Tests
    5. Summary
  13. Controlling Task Conditions
    1. Technical requirements
    2. Defining a failure
      1. Ignoring errors
      2. Defining an error condition
    3. Defining a change
      1. Special handling of the command family
      2. Suppressing a change
    4. Error recovery
      1. Using the rescue section
      2. Using the always section
      3. Handling unreliable environments
    5. Iterative tasks with loops
    6. Summary
  14. Composing Reusable Ansible Content with Roles
    1. Technical requirements
    2. Task, handler, variable, and playbook inclusion 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. Task includes with loops
      3. Including handlers
      4. Including variables
        1. vars_files
        2. Dynamic vars_files inclusion
        3. include_vars
        4. extra-vars
      5. Including playbooks
    3. Roles
      1. Role structure
        1. Tasks
        2. Handlers
        3. Variables
        4. Modules and plugins
        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
        2. Role includes and imports
      4. Role sharing
        1. Ansible Galaxy
    4. Summary
  15. Troubleshooting Ansible
    1. Technical requirements
    2. Playbook logging and verbosity
      1. Verbosity
      2. Logging
    3. Variable introspection
      1. Variable subelements
        1. Subelements versus Python object method
    4. Debugging code execution
      1. Playbook debugging
      2. Debugging local code
        1. Debugging inventory code
        2. Debugging playbook code
        3. Debugging executor code
        4. Debugging remote code
        5. Debugging the action plugins
    5. Summary
  16. Extending Ansible
    1. Technical requirements
    2. Developing modules
      1. The basic module construct
      2. Custom modules
      3. Example – Simple module
        1. Documenting a module
        2. Providing fact data
        3. The check mode
          1. Supporting check mode
          2. Handling check mode
    3. 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
    4. Developing dynamic inventory plugins
      1. Listing hosts
      2. Listing host variables
      3. Simple inventory plugin
        1. Optimizing script performance
    5. Contributing to the Ansible project
      1. Contribution submissions
        1. The Ansible repository
        2. Executing tests
          1. Unit tests
          2. Integration tests
          3. Code-style tests
        3. Making a pull request
    6. Summary
  17. Section 3: Orchestration with Ansible
  18. Minimizing Downtime with Rolling Deployments
    1. Technical requirements
    2. In-place upgrades
    3. Expanding and contracting
    4. Failing fast
      1. The any_errors_fatal option
      2. The max_fail_percentage option
      3. Forcing handlers
    5. Minimizing disruptions
      1. Delaying a disruption
      2. Running destructive tasks only once
    6. Serializing single tasks
    7. Summary
  19. Infrastructure Provisioning
    1. Technical requirements
    2. Managing cloud infrastructures
      1. Creating servers
        1. Booting virtual servers
        2. Adding to runtime inventory
      2. Using OpenStack inventory sources
    3. Managing a public cloud infrastructure
    4. Interacting with Docker containers
      1. Building images
      2. Building containers without a Dockerfile
      3. Docker inventory
    5. Ansible Container
      1. Using ansible-container init
      2. Using ansible-container build
      3. Using ansible-container run
    6. Summary
  20. Network Automation
    1. Technical requirements
    2. Ansible for network manage ment
      1. Cross-platform support
      2. Configuration portability
      3. Backup, restore, and version control
      4. Automated change requests
    3. Handling multiple device types
      1. Researching your modules
      2. Configuring your modules
      3. Writing your playbooks
    4. Configuring Cumulus Networks switches with Ansible
      1. Defining our inventory
      2. Practical examples
      3. Best practices
        1. Inventory
        2. Gathering facts
        3. Jump hosts
    5. Summary
  21. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Mastering Ansible - Third Edition
  • Author(s): James Freeman, Jesse Keating
  • Release date: March 2019
  • Publisher(s): Packt Publishing
  • ISBN: 9781789951547