Stage 4 · Provision
Ansible Fundamentals
Ad-Hoc Commands
Running modules on the fly with ansible and the -m/-a flags.
Ad-Hoc Command Basics
Ad-hoc commands let you run a single module against one or more hosts without writing a playbook. They are the quickest way to execute a task — check disk space, restart a service, or install a package.
ansible <host-pattern> -m <module> -a "<module-args>" [options]
# Example: check uptime on all servers
ansible all -m command -a "uptime"
# Example: check disk space on webservers
ansible webservers -m shell -a "df -h"The -m flag specifies the module. The -a flag passes arguments to that module. Without -m, Ansible defaults to the command module.
Common Modules
| Module | Purpose | Example |
|---|---|---|
| ping | Test connectivity | ansible all -m ping |
| command | Run a command | ansible all -m command -a 'uptime' |
| shell | Run with shell features | ansible all -m shell -a 'df -h | grep /dev' |
| package | Install packages | ansible all -m package -a 'name=nginx state=present' |
| service | Manage services | ansible all -m service -a 'name=nginx state=started' |
| copy | Copy files | ansible all -m copy -a 'src=file.txt dest=/tmp/file.txt' |
| user | Manage users | ansible all -m user -a 'name=deploy state=present' |
| setup | Gather facts | ansible all -m setup |
Targeting Hosts
The host pattern determines which hosts receive the command. Ansible supports patterns for groups, individual hosts, intersections, and exclusions.
# All hosts
ansible all -m ping
# A specific group
ansible webservers -m ping
# A specific host
ansible web1.example.com -m ping
# Wildcard matching
ansible 'web*.example.com' -m ping
# Intersection (hosts in both groups)
ansible 'webservers:dbservers' -m ping
# Exclusion
ansible 'all:!db1.example.com' -m ping
# Limit with --limit
ansible all -m ping --limit webserversUse single quotes around patterns containing wildcards or special characters to prevent shell expansion.
Privilege Escalation
Many tasks require root access. Use --become (-b) to enable privilege escalation. Ansible uses sudo by default but supports other methods.
# Install a package with sudo
ansible all -b -m package -a "name=nginx state=present"
# Specify become method
ansible all -b --become-method=su -m shell -a "cat /etc/shadow"
# Run as specific user
ansible all -b --become-user=root -m service -a "name=nginx state=restarted"The -b flag is shorthand for --become. Configure default become behavior in ansible.cfg to avoid typing it every time.
The command module does not use shell features (pipes, redirects, wildcards). Use the shell module when you need shell syntax. The shell module is slower but more flexible.
When to Use Ad-Hoc
- Quick system checks — disk, memory, processes, network
- Immediate fixes — restart a service, copy a config file
- Exploration — gather facts, test connectivity, verify inventory
- One-off tasks — create a user, update a package, check a file
Ad-hoc commands are ideal for debugging playbooks. Run the same module ad-hoc first to verify it works, then add it to your playbook. This isolates module issues from playbook logic.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.