Ad-hoc Commands and Playbooks
Last updated: January 12, 2026
📚 Reference: This guide aligns with the official Using Ansible command line tools and Using Ansible playbooks documentation.
From Quick Tasks to Orchestrated Automation
When I first started with Ansible, I found myself in a situation where I needed to check disk space on 50+ servers immediately. I didn't have time to write a script, and logging into each server individually would have taken hours. That's when I discovered the power of Ansible ad-hoc commands. With a single line, I was able to check disk usage across all my servers in seconds. Later, as my automation needs grew more complex, I graduated to playbooks for repeatable, documented automation. In this post, I'll share my journey with both these powerful Ansible features.
Ad-hoc Commands: The Command-Line Magic
Ad-hoc commands are Ansible's way of providing command-line magic for one-off tasks. They're perfect for when you need to do something quick without the overhead of creating a playbook. Think of them as the Swiss Army knife in your Ansible toolkit.
The Basic Syntax
The basic syntax of an ad-hoc command is:
ansible [pattern] -m [module] -a "[module options]"Where:
[pattern]is the host or group you're targeting-m [module]specifies which module to use (defaults tocommandif omitted)-a "[module options]"provides arguments to the module
When to Use Ad-hoc Commands
I've found ad-hoc commands invaluable for:
Quick checks: Verifying system status across multiple servers
Simple changes: Making the same change on multiple systems
Emergency fixes: Addressing urgent issues quickly
Fact gathering: Collecting information about your infrastructure
Testing: Confirming connectivity or module behavior before adding to playbooks
Real-world Examples for Linux
Checking System Status
One of the first things I do when troubleshooting is check system load across all affected servers:
Or check disk space usage:
Running Commands with Privilege Escalation
For tasks that require root access:
The --become flag (or -b for short) makes Ansible use privilege escalation, running the command as root.
Managing Packages Quickly
Need to install a package on all database servers? No problem:
Or remove a vulnerable package from everywhere:
Real-world Examples for Windows
Ad-hoc commands work just as well for Windows systems when you have the right setup:
Checking Windows Services
Installing Windows Updates
Checking Disk Space on Windows
Playbooks: Automation as Code
While ad-hoc commands are great for one-off tasks, I quickly found myself repeating certain sequences of operations. This is where Ansible playbooks shine. Playbooks are like automation scripts on steroids – they define a set of tasks to be executed in sequence, with powerful features like variables, conditionals, and loops.
Anatomy of a Playbook
Playbooks are written in YAML format. Here's the basic structure:
The Power of Idempotence
One thing I love about Ansible is its idempotent nature. This means you can run a playbook multiple times, and after the first run, no changes will be made unless they're needed. This gives me confidence to run playbooks regularly to ensure systems stay in the desired state.
My First Multi-OS Playbook Journey
Early in my Ansible journey, I needed to create a playbook that would work across both Linux and Windows servers. Here's a simplified version of what I created:
This playbook taught me the value of using conditionals based on the operating system (ansible_os_family) to apply the right tasks to the right systems.
Sequence Diagram: Ad-hoc Command vs. Playbook Execution
To better understand how ad-hoc commands and playbooks work under the hood, let me share a sequence diagram that compares their execution flows:
The key differences illustrated here are:
Ad-hoc commands execute a single module once
Playbooks organize multiple tasks with logic and structure
Playbook execution includes variable processing and handler management
Best Practices I've Learned
After years of using Ansible, I've developed some best practices that have saved me countless hours:
For Ad-hoc Commands
Use
--limitfor testing: When trying a new command, limit it to one server first.Leverage
--checkmode: See what would change without making actual changes.Increase parallelism for large environments: Use
-fto set the number of parallel processes.Save complex ad-hoc commands: If you find yourself using the same ad-hoc command regularly, it's time for a playbook.
For Playbooks
Start simple, grow incrementally: Begin with a basic playbook and add complexity as needed.
Use roles for organization: As your playbooks grow, refactor common tasks into roles for reusability.
Always test playbooks in dev: Never run a new playbook against production servers first.
Use variables for environment-specific values: This keeps your playbooks flexible.
Include helpful comments: Your future self will thank you.
Real-world Playbook Examples
Linux Server Hardening
Here's a simplified version of a security hardening playbook I've used:
Windows Server Setup
And here's a playbook for setting up IIS on Windows servers:
Migrating from Ad-hoc to Playbooks
As your automation needs grow, you'll likely start with ad-hoc commands and gradually move to playbooks. Here's the process I follow:
Identify repetitive ad-hoc commands: If you're running the same ad-hoc command regularly, it's a candidate for a playbook.
Document the steps: Write down what you're trying to accomplish.
Create a simple playbook: Start with a basic version that does what your ad-hoc command did.
Test and expand: Test your playbook, then gradually add error handling, variables, and conditionals.
Refactor into roles: As your playbook grows, consider splitting it into reusable roles.
For example, I started with this ad-hoc command to check disk space:
Which evolved into this playbook for monitoring and alerting:
Conclusion: The Right Tool for the Job
After years of working with Ansible, I've come to appreciate having both ad-hoc commands and playbooks in my toolbox. They each have their place:
Ad-hoc commands are perfect for quick, one-time tasks, troubleshooting, and information gathering.
Playbooks shine for repeatable processes, complex orchestration, and documented automation.
The best Ansible users know when to use each tool. Start with ad-hoc commands for simplicity, then graduate to playbooks as your automation needs mature. Together, they form a powerful combination that can tackle virtually any automation challenge across both Linux and Windows environments.
The journey from running a simple command on multiple servers to orchestrating complex, multi-tier application deployments becomes much more manageable when you master these two fundamental aspects of Ansible.
What's your favorite ad-hoc command or playbook trick? I'd love to hear your experiences in the comments below!
Last updated