Ad-hoc Commands
The ansible command runs one module against a host pattern, without a playbook. It is the tool for quick questions and one-off fixes across a fleet: "which kernel do the web servers run?", "is the disk full anywhere?", "restart this service everywhere now".
ansible <pattern> -m <module> -a "<arguments>" [-i inventory] [-b] [-f forks] [--check --diff]| Option | Meaning |
|---|---|
-m |
the module (default: ansible.builtin.command) |
-a |
module arguments, as key=value pairs or free text for command/shell |
-b |
become root (sudo) on the host |
-f N |
how many hosts to work on in parallel (forks, default 5) |
-l |
--limit: narrow the pattern further |
-C, -D |
--check (dry run) and --diff (show changes) |
Running commands
The sessions use an ansible.cfg with forks = 1, so hosts answer in inventory order and the transcript is the same on every run (with more forks, whichever host finishes first prints first):
cat > ansible.cfg <<'INI'
[defaults]
inventory = hosts.ini
interpreter_python = auto_silent
forks = 1
INI
cat > hosts.ini <<'INI'
[web]
web1 ansible_connection=local
web2 ansible_connection=local
INI
ansible web -a "uname -s"
ansible web -m ansible.builtin.shell -a "echo \$HOME | tr a-z A-Z"web1 | CHANGED | rc=0 >>
Linux
web2 | CHANGED | rc=0 >>
Linux
web1 | CHANGED | rc=0 >>
/HOME/LEARNER
web2 | CHANGED | rc=0 >>
/HOME/LEARNER
CHANGED is reported for every command, because Ansible can't know whether an arbitrary command changed something. The commands lesson shows how to tell it.
command runs the program directly, without a shell: no pipes, redirects, $VARS or globs. Use shell when you need them, and quote carefully: the -a string passes through your local shell first (hence \$HOME).
Using real modules ad hoc
Any module works ad hoc. Modules with a desired state are idempotent here as well:
cat > ansible.cfg <<'INI'
[defaults]
inventory = hosts.ini
interpreter_python = auto_silent
INI
printf '[web]\nweb1 ansible_connection=local\n' > hosts.ini
ansible web -m ansible.builtin.file -a "path=/home/learner/releases state=directory mode=0755"
echo "=== again"
ansible web -m ansible.builtin.file -a "path=/home/learner/releases state=directory mode=0755" | head -1
echo "=== check a single fact"
ansible web -m ansible.builtin.setup -a "filter=ansible_system"web1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/opt/uv-tools/ansible-core/bin/python3.13"
},
"changed": true,
"gid": 1000,
"group": "learner",
"mode": "0755",
"owner": "learner",
"path": "/home/learner/releases",
"size": 4096,
"state": "directory",
"uid": 1000
}
=== again
web1 | SUCCESS => {
=== check a single fact
web1 | SUCCESS => {
"ansible_facts": {
"ansible_system": "Linux",
"discovered_interpreter_python": "/opt/uv-tools/ansible-core/bin/python3.13"
},
"changed": false
}
The setup module gathers facts (OS, network, hardware); filter= limits the output. The facts lesson covers them.
Typical fleet one-liners
On real servers (a sample session: these need SSH and root):
$ ansible all -m ansible.builtin.ping # can I reach everything?
$ ansible web -a "df -h /" -f 20 # disk usage, 20 hosts at a time
$ ansible web -b -m ansible.builtin.service -a "name=nginx state=restarted"
$ ansible db -b -m ansible.builtin.apt -a "name=openssl state=latest update_cache=true"
$ ansible all -m ansible.builtin.setup -a "filter=ansible_distribution*"
$ ansible 'web:!web1' -b -m ansible.builtin.reboot -f 1 # one at a time, except web1A failed or unreachable host shows up like this:
web3 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: ssh: connect to host 10.0.1.13 port 22: Connection timed out",
"unreachable": true
}When to stop using ad-hoc commands
Ad-hoc commands leave no record: next month nobody knows that openssl was upgraded by hand on db. Use them to look, and for emergencies. Anything you do twice, or anything that changes servers, belongs in a playbook in Git, where it is reviewed, repeatable and testable.
Test yourself
Which module does ansible web -a "uptime" use?
shellcommand(the default module)rawsetup
Why does ansible web -a "cat /etc/hosts | wc -l" not work as expected?
catis forbiddencommandruns without a shell, so|is passed to cat as an argument- Pipes need
-b wcis not installed
What does -f 1 change?
- Runs one task only
- Works on one host at a time
- Fails after one error
- Forces colour