Run one-off Ansible tasks on remote hosts without a playbook.
medium risk
Ansible ad-hoc commands run single module tasks against inventory hosts without writing a playbook. Useful for quick checks, one-time changes, and testing connectivity. The -m flag selects the module; -a passes arguments.
| Flag | Meaning |
|---|---|
-m --module-name | Ansible module to run |
-a --args | Module arguments |
-i --inventory | Inventory file or host list |
-u --user | Remote user |
--become | Run with privilege escalation (sudo) |
-f --forks | Parallel processes (default: 5) |
ansible webservers -i inventory.ini -m pingPing all webservers · explain & risk-check →ansible all -m command -a 'uptime'Run uptime on all hosts · explain & risk-check →ansible db -m service -a 'name=postgresql state=restarted' --becomeRestart PostgreSQL with sudo · explain & risk-check →ansible all -m gather_facts -a 'filter=ansible_os_family'Gather OS family facts · explain & risk-check →Use `ansible all -m ping -i inventory.ini` as a connectivity health check before running playbooks — catches SSH key issues and unreachable hosts before they cause partial failures.
Reference text is hand-written and may be incomplete — check man ansible for your version. Test destructive commands on a non-production system first.