The Ansible Workshop
The Ansible Workshop
Hands-On Learning For Rapid Mastery
About the Book
In "The Ansible Workshop," you will deep dive into the world of Ansible, the amazing automation tool that converts complex IT tasks into a set of easy, simple-to-repeat steps. This guide is your roadmap to mastering Ansible, whether you are a novice just starting to use Ansible or an intermediate user who has already used Ansible and is looking for additional information and tricks to enhance your skills.
To start with, we will unwrap the essence of Ansible. You will explore what Ansible is, its declarative vs imperative automation approach, and dive into its architecture that exposes Ansible's inner workings. This is basically building the foundation for everything shown henceforth.
At its core, Ansible is configuration management and application deployment. You will learn how to use ad-hoc commands and playbooks to automate server configurations and application deployment. We will cover creating simple playbooks and developing advanced ones, giving you the tools required to manage multiple scenarios and orchestrate complex tasks.
In today's dynamic containerized environment, the integration of Ansible with Docker and Kubernetes is crucial. You will learn how to build, tag, and push Docker images, as well as manage Docker containers, networks, and volumes, and even orchestrate Docker using Ansible. For the fans of Kubernetes out there, we dive into deploying and managing Kubernetes clusters using Ansible and Kubespray, then we explore how to deploy your workloads to Kubernetes using Ansible.
This guide provides comprehensive coverage of each and every minute part of Ansible:
- Inventory: know and make use of advanced inventories.
- Facts: How to locate and utilize Ansible facts.
- Vaults: The art of managing sensitive data in a better way.
- Blocks: Managing errors gracefully, using Ansible blocks.
- Modules and plugins: Expanding the horizon of Ansible.
- Filters: Using Ansible filters for data transformation.
- Roles: Running many playbooks through Ansible roles.
- Collections: Discovering and using collections of Ansible.
- Ansible Galaxy: Everything about Ansible Galaxy and how it can be used, explained.
- Developing your modules: Briefing on how one can develop their Ansible module.
- Develop custom filters: Learn how to develop your own Ansible filters.
- Integrate other tools with Ansible (e.g. Kubernetes, Docker, ChatGPT, or whatever).
- And much more!
Every concept is supported by examples from real-world use and best practices. You will find out how to use IDEs for better writing playbooks, debugging, and optimizing Ansible performance. We also cover how to create custom modules and filters – including a special module leveraging generative AI (ChatGPT).
Team Discounts
Get a team discount on this book!
Table of Contents
- Preface
- What Are You Going To Learn?
- Who Is This Guide For?
- About The Author
- The Companion Toolkit
- Join the Community
- Your Feedback Matters
- Overview of Ansible
- What is Ansible?
- The Tale of Ansible
- Declarative vs. Imperative Automation
- Ansible Architecture
- Inventory
- Playbooks
- Modules
- Plugins
- Roles
- Ansible Vault
- Ansible Engine
- How Does Ansible Work?
- Pros of Ansible
- Cons of Ansible
- Ansible vs Other Tools
- Chef vs Ansible
- Puppet vs Ansible
- SaltStack vs Ansible
- Introduction to Ansible
- Prerequisites
- Installing and Configuring Ansible
- Ansible Inventory
- Understanding the Ansible Inventory
- Creating Advanced Ansible Inventories
- Advanced Techniques To Target Specific Managed Nodes
- Using the Default Inventory
- Dynamic Inventories
- Creating and Using Playbooks
- Ad-hoc Commands vs Playbooks
- Creating a Playbook
- Playbook, Play, and Tasks
- Creating Advanced Playbooks - Part 1
- Creating Advanced Playbooks - Part 2
- Ansible Facts
- What are Ansible Facts and Why Do We Need Them?
- Finding Ansible Facts
- How To Use Ansible Facts
- Advanced Usage of Ansible Facts
- Debugging Ansible
- Enabling Debugging
- Debugging Tasks
- Identify The Task That Failed
- List The Task Variables
- Identify The Host Where The Task Has Failed
- Print The Execution Result of The Task
- List All Task Arguments
- Getting a Single Task Argument
- Using the Debugger to Fix the Error
- Enabling Debugging for All Tasks
- Choosing When to Debug
- Low-Level Debugging
- Dry Run: Testing Playbooks Without Executing Them
- Callback Plugins
- Printing Variables
- Printing The Output Of A Command
- Printing The Output Of A Task
- Using Your IDE to Write Better Playbooks
- Ansible Vault and Sensitive Data Management
- What is Ansible Vault?
- How to Use Ansible Vault
- Using Password Files
- Using Vault IDs
- Ansible Blocks
- What are Ansible Blocks?
- How to Use Ansible Blocks
- Error Handling with Ansible Blocks
- Using “Rescue” and “Always” Together
- Ansible Blocks with Asynchronous Tasks
- Ansible Modules, Plugins, and Filters
- What is an Ansible Module?
- What is an Ansible Plugin?
- Modules vs Plugins
- Installing Ansible Modules and Plugins
- Exploring the Lookup Plugins
- Creating Ansible Modules
- Documenting Your Module
- Using The AnsibleModule Utility Class
- A Full Example
- Imports And Metadata
- Function
run_module()
- Function
main()
- Execution Check
- Developing an Ansible Module That Uses Generative AI
- What Is An Ansible Filter?
- Using Ansible Filters
- Creating Ansible Filters
- Ansible Roles
- Role Directory Structure
- Using “Defaults”: Default Variables
- Using “Files”: Static Files
- Using “Handlers”: Actions Triggered by Tasks
- Using “Meta”
- Using “Tasks”: Main List of Tasks
- Using “Templates”: Template Files
- Using “Tests”: Testing the Role
- Using “Vars”: Variables for the Role
- Creating a Role
- Role Directory Structure
- Making Ansible Faster
- Disabling SSH Host Key Checking
- Disabling Fact Gathering
- Using More Forks
- Optimize How Ansible Uses SSH
- Use Asynchronous Tasks
- Sequential vs Batch Mode
- Using Execution Strategies
- Managing Docker and Docker Compose with Ansible
- Installation and Prerequisites
- Building Docker Images from Ansible Playbooks
- Tagging and Pushing Docker Images
- Managing Docker Containers from Ansible Playbooks
- Managing Docker Networks from Ansible Playbooks
- Managing Docker Volumes from Ansible Playbooks
- Docker Compose Up and Running with Ansible
- Automating Kubernetes with Ansible
- What is a Highly Available Kubernetes Cluster?
- Etcd Nodes
- Control Plane Nodes
- Worker Nodes
- Creating a Highly Available Kubernetes Cluster with Kubespray and Ansible
- Available Options to Deploy Kubernetes
- Prerequisites and Initial Setup
- Creating The Inventory
- Creating the Cluster
- How Kubespray Works
- Step 1: Pre-Configuration Checks and Setup
- Step 2: Setting Up Container Runtime
- Step 3: Network Plugin Installation
- Step 4: Kubernetes Components Installation
- Step 5: Initialization of the First Control Plane Node
- Step 6: Setting Up etcd Cluster
- Step 7: Joining Additional Control Plane Nodes
- Step 8: Joining Worker Nodes
- Step 9: Cluster Configuration With Addons
- Step 10: Cluster Verification And Health Checks
- Step 11: Security Configuration
- Step 12: Applying Custom Configurations
- Step 13: Cluster Ready For Deployment
- Start Using The Cluster
- Deploying to Kubernetes with Ansible
- Deploying to Kubernetes with Ansible and the K8s Module
- Using Remote Manifests
- Inline Manifests
- Validating Kubernetes Definitions
- Managing Multiple Clusters
- Patching Resources
- Restarting Deployments
- Deploying to Kubernetes with Ansible and Helm
- Templating Kubernetes Resources With Ansible
- Troubleshooting Kubernetes with Ansible
- Creating Generic Debug Tasks to View Kubernetes Resources
- Viewing Kubernetes Pod Logs with Ansible
- Ansible + Kubernetes = A Powerful Combination
- What is a Highly Available Kubernetes Cluster?
- Ansible Cheat Sheet
- Installing Ansible
- On Linux
- On macOS
- On Windows
- Configuring Ansible
- Inventory
- Setting Up the Inventory File (INI)
- Setting Up the Inventory File (YAML)
- Ansible Host Variables
- Testing Your Setup
- Playbook Structure
- Playbook Keywords
- Common Modules
- Running a Command
- Running a Shell Command
- Running a Raw Command
- Copying a File
- Installing a Package
- Restarting a Service
- Copying a File with Permissions
- Copying a File with Permissions and Context
- Playbooks
- Running a Playbook
- Running a Playbook with Inventory
- Running a Playbook with Inventory and Limit
- Running a Playbook with Limit
- Running a Playbook with Tags
- Running a Playbook with Skip Tags
- Running a Playbook with Extra Variables
- Running a Playbook with a Vault Password File
- Running a Playbook with Ask Vault Password
- Running a Playbook with Ask Sudo Password
- Running a Playbook with Ask Pass
- Running a Playbook in Check Mode
- Running a Playbook in Diff Mode
- Running a Playbook in Verbose Mode
- Running a Playbook in Extra Verbose Mode
- Running a Playbook in Extra Extra Verbose Mode
- Running a Playbook in Extra Extra Extra Verbose Mode
- Running a Playbook in Extra Extra Extra Extra Verbose Mode
- Running a Playbook with Forks
- Running a Playbook with Timeout
- Jinja2
- Using Jinja2
- Using Jinja2 with Filters
- Jinja2 Template
- Jinja2 Template with Variables
- Templates with Loops
- Templates with Conditionals
- Templates with Filters
- Host Patterns
- Debugging
- Syntax Check a Playbook
- Check if Hosts Are Reachable
- Check if Hosts Are Reachable with SSH
- Check if Hosts Are Reachable with WinRM
- Check if Hosts Are Reachable Locally
- Verbose Mode
- Debug Mode
- Capture Output
- Capture Output and Show Only Specific Fields
- Capture Output and Show Only Specific Fields with Jinja2
- Gathering Facts
- Gathering Facts for All Hosts
- Gathering Facts for a Specific Host
- Gathering Facts for a Specific Host and Saving to a File
- Variables
- Setting a Variable
- Setting a Variable with Multiple Lines
- Setting a Variable with Multiple Lines and Indentation
- Conditionals
- When
- When with Multiple Conditions
- Loops
- Looping Over a List
- Looping Over a Dictionary
- Looping Over a Dictionary Using “with_dict”
- Looping Over a List with “with_items”
- Handlers
- Using Handlers
- Using Handlers with Multiple Tasks
- Using Handlers with Multiple Tasks and Different Handlers
- Conditional Execution
- Using “ignore_errors” to Continue Execution Even After Failures
- Using “changed_when” to Control When a Task Is Considered Changed
- Using “failed_when” to Control When a Task Is Considered Failed
- Failing a Playbook with “fail”
- Vault
- Editing an Encrypted File
- Updating Encryption Password
- Viewing an Encrypted File
- Encrypting a File
- Encrypting a File with a Password File
- Decrypting a File
- Encrypting a String
- Encrypting a String with a Password File
- Asynchronous Tasks
- Running a Task Asynchronously
- Checking the Status of Asynchronous Tasks
- Roles
- Creating a Role
- Using a Role
- Using a Role with Variables
- Using a Role with Multiple Variables
- Using a Role with Tags
- Ansible Galaxy
- Searching for Roles
- Installing a Role
- Installing a Role with a Specific Version
- Ansible Collections
- Installing a Collection
- Installing a Collection with a Specific Version
- Installing a Collection from a File
- Installing a Collection from a Directory
- Resources About Ansible
- Official Ansible Documentation
- Ansible Blogs and Articles
- Ansible Community and Forums
- Ansible GitHub Repository
- Ansible Videos
- Ansible Tools
- IDE Extensions
- VSCode
- PyCharm
- Sublime
- Vim
- Newsletters
- Installing Ansible
- Afterword
- Your Feedback Matters
- Preface
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them