Ansible: Up and Running, 3rd Edition

Book description

Among the many configuration management tools available, Ansible has some distinct advantages: It's minimal in nature. You don't need to install agents on your nodes. And there's an easy learning curve. With this updated third edition, you'll quickly learn how to be productive with Ansible whether you're a developer deploying code or a system administrator looking for a better automation solution.

Authors Bas Meijer, Lorin Hochstein, and Rene Moser show you how to write playbooks (Ansible's configuration management scripts), manage remote servers, and explore the tool's real power: built-in declarative modules. You'll learn how Ansible has all the functionality you need--and the simplicity you desire.

  • Explore Ansible configuration management and deployment
  • Manage Linux, Windows, and network devices
  • Learn how to apply Ansible best practices
  • Understand how to use the new collections format
  • Create custom modules and plug-ins
  • Generate reusable Ansible content for open source middleware
  • Build container images, images for cloud instances, and cloud infrastructure
  • Automate CI/CD development environments
  • Learn how to use Ansible Automation Platform for DevOps

Publisher resources

View/Submit Errata

Table of contents

  1. Preface to the Third Edition
    1. Conventions Used in This Book
    2. Using Code Examples
    3. O’Reilly Online Learning
    4. How to Contact Us
    5. Acknowledgments
      1. From Lorin
      2. From René
      3. From Bas
  2. 1. Introduction
    1. A Note About Versions
    2. Ansible: What Is It Good For?
    3. How Ansible Works
    4. What’s So Great About Ansible?
      1. Simple
      2. Powerful
      3. Secure
    5. Is Ansible Too Simple?
    6. What Do I Need to Know?
    7. What Isn’t Covered
    8. Moving Forward
  3. 2. Installation and Setup
    1. Installing Ansible
      1. Loose Dependencies
      2. Running Ansible in Containers
      3. Ansible Development
    2. Setting Up a Server for Testing
      1. Using Vagrant to Set Up a Test Server
      2. Telling Ansible About Your Servers
      3. Simplifying with the ansible.cfg File
      4. Kill Your Darlings
    3. Convenient Vagrant Configuration Options
      1. Port Forwarding and Private IP Addresses
      2. Enabling Agent Forwarding
    4. The Docker Provisioner
    5. The Ansible Local Provisioner
    6. When the Provisioner Runs
    7. Vagrant Plug-ins
      1. Hostmanager
      2. VBGuest
    8. VirtualBox Customization
    9. Vagrantfile Is Ruby
    10. Production Setup
    11. Conclusion
  4. 3. Playbooks: A Beginning
    1. Preliminaries
    2. A Very Simple Playbook
      1. Specifying an NGINX Config File
      2. Creating a Web Page
      3. Creating a Group
    3. Running the Playbook
    4. Playbooks Are YAML
      1. Start of Document
      2. End of File
      3. Comments
      4. Indentation and Whitespace
      5. Strings
      6. Booleans
      7. Lists
      8. Dictionaries
      9. Multiline Strings
      10. Pure YAML Instead of String Arguments
    5. Anatomy of a Playbook
    6. Plays
      1. Tasks
      2. Modules
      3. Viewing Ansible Module Documentation
      4. Putting It All Together
    7. Did Anything Change? Tracking Host State
    8. Getting Fancier: TLS Support
      1. Generating a TLS Certificate
      2. Variables
      3. Quoting in Ansible Strings
      4. Generating the NGINX Configuration Template
      5. Loop
      6. Handlers
      7. A Few Things to Keep in Mind About Handlers
      8. Testing
      9. Validation
      10. The Playbook
      11. Running the Playbook
    9. Conclusion
  5. 4. Inventory: Describing Your Servers
    1. Inventory/Hosts Files
      1. Preliminaries: Multiple Vagrant Machines
    2. Behavioral Inventory Parameters
      1. Changing Behavioral Parameter Defaults
    3. Groups and Groups and Groups
      1. Example: Deploying a Django App
      2. Aliases and Ports
      3. Groups of Groups
      4. Numbered Hosts (Pets Versus Cattle)
    4. Hosts and Group Variables: Inside the Inventory
    5. Host and Group Variables: In Their Own Files
    6. Dynamic Inventory
      1. Inventory Plug-ins
      2. Amazon EC2
      3. Azure Resource Manager
      4. The Interface for a Dynamic Inventory Script
      5. Writing a Dynamic Inventory Script
    7. Breaking the Inventory into Multiple Files
    8. Adding Entries at Runtime with add_host and group_by
      1. add_host
      2. group_by
    9. Conclusion
  6. 5. Variables and Facts
    1. Defining Variables in Playbooks
      1. Defining Variables in Separate Files
      2. Directory Layout
    2. Viewing the Values of Variables
      1. Variable Interpolation
    3. Registering Variables
    4. Facts
      1. Viewing All Facts Associated with a Server
      2. Viewing a Subset of Facts
      3. Any Module Can Return Facts or Info
      4. Local Facts
      5. Using set_fact to Define a New Variable
    5. Built-In Variables
      1. hostvars
      2. inventory_hostname
      3. groups
    6. Extra Variables on the Command Line
    7. Precedence
    8. Conclusion
  7. 6. Introducing Mezzanine: Our Test Application
    1. Why Is Deploying to Production Complicated?
    2. Postgres: The Database
    3. Gunicorn: The Application Server
    4. NGINX: The Web Server
    5. Supervisor: The Process Manager
    6. Conclusion
  8. 7. Deploying Mezzanine with Ansible
    1. Listing Tasks in a Playbook
    2. Organization of Deployed Files
    3. Variables and Secret Variables
    4. Installing Multiple Packages
    5. Adding the Become Clause to a Task
    6. Updating the apt Cache
    7. Checking Out the Project Using Git
    8. Installing Mezzanine and Other Packages into a Virtual Environment
    9. Complex Arguments in Tasks: A Brief Digression
    10. Configuring the Database
    11. Generating the local_settings.py File from a Template
    12. Running django-manage Commands
    13. Running Custom Python Scripts in the Context of the Application
      1. Setting Service Configuration Files
    14. Enabling the NGINX Configuration
    15. Installing TLS Certificates
    16. Installing Twitter Cron Job
    17. The Full Playbook
    18. Running the Playbook Against a Vagrant Machine
    19. Troubleshooting
      1. Cannot Check Out Git Repository
      2. Cannot Reach 192.168.33.10.nip.io
      3. Bad Request (400)
    20. Conclusion
  9. 8. Debugging Ansible Playbooks
    1. Humane Error Messages
    2. Debugging SSH Issues
    3. Common SSH Challenges
      1. PasswordAuthentication no
      2. SSH as a Different User
      3. Host Key Verification Failed
      4. Private Networks
    4. The debug Module
    5. Playbook Debugger
    6. The assert Module
    7. Checking Your Playbook Before Execution
      1. Syntax Check
      2. List Hosts
      3. List Tasks
      4. Check Mode
      5. Diff (Show File Changes)
      6. Tags
      7. Limits
    8. Conclusion
  10. 9. Roles: Scaling Up Your Playbooks
    1. Basic Structure of a Role
    2. Example: Deploying Mezzanine with Roles
      1. Using Roles in Your Playbooks
      2. Pre-Tasks and Post-Tasks
      3. A database Role for Deploying the Database
      4. A mezzanine Role for Deploying Mezzanine
    3. Creating Role Files and Directories with ansible-galaxy
    4. Dependent Roles
    5. Ansible Galaxy
      1. Web Interface
      2. Command-Line Interface
      3. Role Requirements in Practice
      4. Contributing Your Own Role
    6. Conclusion
  11. 10. Complex Playbooks
    1. Dealing with Badly Behaved Commands
    2. Filters
      1. The default Filter
      2. Filters for Registered Variables
      3. Filters That Apply to Filepaths
      4. Writing Your Own Filter
    3. Lookups
      1. file
      2. pipe
      3. env
      4. password
      5. template
      6. csvfile
      7. dig
      8. redis
      9. Writing Your Own Lookup Plug-in
    4. More Complicated Loops
      1. With Lookup Plug-in
      2. with_lines
      3. with_fileglob
      4. with_dict
      5. Looping Constructs as Lookup Plug-ins
    5. Loop Controls
      1. Setting the Variable Name
      2. Labeling the Output
    6. Imports and Includes
      1. Dynamic Includes
      2. Role Includes
      3. Role Flow Control
    7. Blocks
    8. Error Handling with Blocks
    9. Encrypting Sensitive Data with ansible-vault
      1. Multiple Vaults with Different Passwords
    10. Conclusion
  12. 11. Customizing Hosts, Runs, and Handlers
    1. Patterns for Specifying Hosts
    2. Limiting Which Hosts Run
    3. Running a Task on the Control Machine
    4. Manually Gathering Facts
    5. Retrieving an IP Address from the Host
    6. Running on One Host at a Time
    7. Running on a Batch of Hosts at a Time
    8. Running Only Once
    9. Limiting Which Tasks Run
      1. step
      2. start-at-task
      3. Running Tags
      4. Skipping Tags
    10. Running Strategies
      1. Linear
      2. Free
    11. Advanced Handlers
      1. Handlers in Pre- and Post-Tasks
      2. Flush Handlers
      3. Meta Commands
      4. Handlers Notifying Handlers
      5. Handlers Listen
      6. The SSL Case for the listen Feature
    12. Conclusion
  13. 12. Managing Windows Hosts
    1. Connection to Windows
    2. PowerShell
    3. Windows Modules
    4. Our Java Development Machine
    5. Adding a Local User
    6. Windows Features
    7. Installing Software with Chocolatey
    8. Configuration of Java
    9. Updating Windows
    10. Conclusion
  14. 13. Ansible and Containers
    1. Kubernetes
    2. Docker Application Life Cycle
    3. Registries
    4. Ansible and Docker
    5. Connecting to the Docker Daemon
    6. Example Application: Ghost
    7. Running a Docker Container on Our Local Machine
    8. Building an Image from a Dockerfile
    9. Pushing Our Image to the Docker Registry
    10. Orchestrating Multiple Containers on Our Local Machine
    11. Querying Local Images
    12. Deploying the Dockerized Application
      1. Provisioning MySQL
      2. Deploying the Ghost Database
      3. Frontend
      4. Frontend: Ghost
      5. Frontend: NGINX
      6. Cleaning Out Containers
    13. Conclusion
  15. 14. Quality Assurance with Molecule
    1. Installation and Setup
    2. Configuring Molecule Drivers
    3. Creating an Ansible Role
    4. Scenarios
      1. Desired State
      2. Configuring Scenarios in Molecule
      3. Managing Virtual Machines
      4. Managing Containers
    5. Molecule Commands
    6. Linting
      1. YAMLlint
      2. ansible-lint
      3. ansible-later
    7. Verifiers
      1. Ansible
      2. Goss
      3. TestInfra
    8. Conclusion
  16. 15. Collections
    1. Installing Collections
    2. Listing Collections
    3. Using Collections in a Playbook
    4. Developing a Collection
    5. Conclusion
  17. 16. Creating Images
    1. Creating Images with Packer
      1. Vagrant VirtualBox VM
      2. Combining Packer and Vagrant
      3. Cloud Images
      4. Google Cloud Platform
      5. Azure
      6. Amazon EC2
      7. The Playbook
    2. Docker Image: GCC 11
    3. Conclusion
  18. 17. Cloud Infrastructure
    1. Terminology
      1. Instance
      2. Amazon Machine Image
      3. Tags
    2. Specifying Credentials
      1. Environment Variables
      2. Configuration Files
    3. Prerequisite: Boto3 Python Library
    4. Dynamic Inventory
      1. Inventory Caching
      2. Other Configuration Options
    5. Defining Dynamic Groups with Tags
      1. Applying Tags to Existing Resources
      2. Nicer Group Names
    6. Virtual Private Clouds
    7. Configuring ansible.cfg for Use with ec2
    8. Launching New Instances
    9. EC2 Key Pairs
      1. Creating a New Key
      2. Uploading Your Public Key
    10. Security Groups
      1. Permitted IP Addresses
      2. Security Group Ports
    11. Getting the Latest AMI
    12. Create a New Instance and Add It to a Group
    13. Waiting for the Server to Come Up
    14. Putting It All Together
    15. Specifying a Virtual Private Cloud
      1. Dynamic Inventory and VPC
    16. Conclusion
  19. 18. Callback Plug-ins
    1. Stdout Plug-ins
      1. ARA
      2. debug
      3. default
      4. dense
      5. json
      6. minimal
      7. null
      8. oneline
    2. Notification and Aggregate Plug-ins
      1. Python Requirements
      2. foreman
      3. jabber
      4. junit
      5. log_plays
      6. logentries
      7. logstash
      8. mail
      9. profile_roles
      10. profile_tasks
      11. say
      12. slack
      13. splunk
      14. timer
    3. Conclusion
  20. 19. Custom Modules
    1. Example: Checking That You Can Reach a Remote Server
      1. Using the Script Module Instead of Writing Your Own
      2. can_reach as a Module
    2. Should You Develop a Module?
    3. Where to Put Your Custom Modules
    4. How Ansible Invokes Modules
      1. Generate a Standalone Python Script with the Arguments (Python Only)
      2. Copy the Module to the Host
      3. Create an Arguments File on the Host (Non-Python Only)
      4. Invoke the Module
    5. Expected Outputs
      1. Output Variables That Ansible Expects
    6. Implementing Modules in Python
      1. Parsing Arguments
      2. Accessing Parameters
      3. Importing the AnsibleModule Helper Class
      4. Argument Options
      5. AnsibleModule Initializer Parameters
      6. Returning Success or Failure
      7. Invoking External Commands
      8. Check Mode (Dry Run)
    7. Documenting Your Module
    8. Debugging Your Module
    9. Implementing the Module in Bash
    10. Specifying an Alternative Location for Bash
    11. Conclusion
  21. 20. Making Ansible Go Even Faster
    1. SSH Multiplexing and ControlPersist
      1. Manually Enabling SSH Multiplexing
      2. SSH Multiplexing Options in Ansible
    2. More SSH Tuning
      1. Algorithm Recommendations
    3. Pipelining
      1. Enabling Pipelining
      2. Configuring Hosts for Pipelining
    4. Mitogen for Ansible
    5. Fact Caching
      1. JSON File Fact-Caching Backend
      2. Redis Fact-Caching Backend
      3. Memcached Fact-Caching Backend
    6. Parallelism
    7. Concurrent Tasks with Async
    8. Conclusion
  22. 21. Networking and Security
    1. Network Management
      1. Supported Vendors
      2. Ansible Connection for Network Automation
      3. Privileged Mode
      4. Network Inventory
      5. Network Automation Use Cases
    2. Security
      1. Comply with Compliance?
      2. Secured, but Not Secure
      3. Shadow IT
      4. Sunshine IT
      5. Zero Trust
    3. Conclusion
  23. 22. CI/CD and Ansible
    1. Continuous Integration
      1. Elements in a CI System
      2. Jenkins and Ansible
      3. Running CI for Ansible Roles
    2. Staging
    3. Ansible Plug-in
    4. Ansible Tower Plug-in
    5. Conclusion
  24. 23. Ansible Automation Platform
    1. Subscription Models
      1. Ansible Automation Platform Trial
    2. What Ansible Automation Platform Solves
      1. Access Control
      2. Projects
      3. Inventory Management
      4. Run Jobs by Job Templates
    3. RESTful API
    4. AWX.AWX
      1. Installation
      2. Create an Organization
      3. Create an Inventory
      4. Running a Playbook with a Job Template
    5. Using Containers to Run Ansible
      1. Creating Execution Environments
    6. Conclusion
  25. 24. Best Practices
    1. Simplicity, Modularity, and Composability
    2. Organize Content
    3. Decouple Inventories from Projects
    4. Decouple Roles and Collections
    5. Playbooks
    6. Code Style
    7. Tag and Test All the Things
    8. Desired State
    9. Deliver Continuously
    10. Security
    11. Deployment
    12. Performance Indicators
    13. Benchmark Evidence
    14. Final Words
  26. Bibliography
  27. Index
  28. About the Authors

Product information

  • Title: Ansible: Up and Running, 3rd Edition
  • Author(s): Bas Meijer, Lorin Hochstein, René Moser
  • Release date: July 2022
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098109158