TExcelTAD
Home › Configuration Management: Ansible › Ad-hoc Commands

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".

text
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):

bash · try it yourself
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:

bash · try it yourself
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):

bash
$ 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 web1

A failed or unreachable host shows up like this:

text
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?

  1. shell
  2. command (the default module)
  3. raw
  4. setup

Why does ansible web -a "cat /etc/hosts | wc -l" not work as expected?

  1. cat is forbidden
  2. command runs without a shell, so | is passed to cat as an argument
  3. Pipes need -b
  4. wc is not installed

What does -f 1 change?

  1. Runs one task only
  2. Works on one host at a time
  3. Fails after one error
  4. Forces colour