Ansible ad-hoc commands cheat sheet
One-line ansible commands for quick jobs across many servers: ping hosts, run a command, copy files, manage packages, users and services, gather facts, with the flags you need.
Drafted with AI from official documentation. Scripts were run in a test environment and repository links verified. Awaiting a human check. Did it work for you? Last edited Outdated?
An ad-hoc command runs a single module against a group of hosts without writing a playbook. Use it for quick checks and one-off tasks; anything you will repeat belongs in a playbook. The general form is:
ansible <pattern> -i <inventory> -m <module> -a "<arguments>" [options]
<pattern> is a host, a group, or all. If you leave out -m, the command module is used. Arguments are key=value pairs or a JSON string.
Common options
-i FILE: inventory to use.-m MODULE: module to run, for exampleansible.builtin.copy.-a ARGS: module arguments.-u USER: remote SSH user (default: your local user name).-b/--become: escalate privileges (sudo by default). No password is implied.-K/--ask-become-pass: prompt for the sudo password. It does not turn on become by itself, so combine it with-b.--become-user USER: become this user instead of root (also needs-b).-f N: number of parallel forks (default 5).-C/--check: dry run, report what would change.-vto-vvvv: more verbose output;-vvvvincludes SSH connection details.
Connectivity and facts
ansible all -i inventory.ini -m ping
ansible web -i inventory.ini -m ansible.builtin.setup
ansible web -i inventory.ini -m ansible.builtin.setup -a "filter=ansible_distribution*"
ansible-inventory -i inventory.ini --graph
Run commands
ansible web -i inventory.ini -a "uptime"
ansible web -i inventory.ini -a "df -h /"
ansible web -i inventory.ini -m ansible.builtin.shell -a "ps aux | grep [n]ginx"
The default command module does not use a shell, so pipes, redirects and variables like $HOME do not work. Use ansible.builtin.shell when you need them, and put the argument in single quotes if it contains $ so your local shell does not expand it.
Files
ansible web -i inventory.ini -b -m ansible.builtin.copy -a "src=./motd dest=/etc/motd mode=0644"
ansible web -i inventory.ini -b -m ansible.builtin.file -a "path=/srv/app state=directory mode=0755 owner=appuser"
ansible web -i inventory.ini -m ansible.builtin.fetch -a "src=/etc/os-release dest=./collected/"
fetch copies files from the hosts to the control node, into one subfolder per host.
Packages
ansible web -i inventory.ini -b -m ansible.builtin.apt -a "name=htop state=present update_cache=true"
ansible rhel -i inventory.ini -b -m ansible.builtin.dnf -a "name=htop state=present"
ansible all -i inventory.ini -b -m ansible.builtin.package -a "name=htop state=present"
ansible.builtin.package picks the right package manager for each host, which is handy for mixed fleets when the package name is the same everywhere.
Services and users
ansible web -i inventory.ini -b -m ansible.builtin.service -a "name=nginx state=restarted"
ansible web -i inventory.ini -b -m ansible.builtin.service -a "name=nginx enabled=true"
ansible all -i inventory.ini -b -m ansible.builtin.user -a "name=appuser state=present shell=/bin/bash"
Rebooting safely
Warning: a reboot hits every host in the pattern at once (up to the fork count). List the targets first with --list-hosts, and use -f 1 or a smaller group to go one by one.
ansible web -i inventory.ini --list-hosts
ansible web -i inventory.ini -b -f 1 -m ansible.builtin.reboot
The reboot module waits for the host to come back before it reports success, which a plain -a "/sbin/reboot" does not do.
Official documentation
Did this work for you?
No votes yet. Be the first.
If it did not work, the report form below tells the editors what went wrong.
See also
- 0181 Ansible concepts: inventory, modules, playbooks, idempotency and roles Ansible configures many servers over SSH from one control machine. Learn the core pieces: inventory, modules, tasks, playbooks, roles, and why runs… Beginner · Explains an idea · Linux, macOS Article Ansible 15 Oct 2026
- 0182 Your first Ansible playbook: install nginx and publish a page A complete beginner playbook that installs nginx on Debian or Ubuntu servers, starts it at boot and deploys a home page, with… Beginner · Step by step · Linux, macOS Script Ansible 15 Oct 2026
- 0183 Ansible variables, templates and handlers: configure an nginx site Use group variables, Jinja2 templates and handlers together: render an nginx server block from variables, test the config and reload nginx only… Intermediate · Step by step · Linux, macOS Script Ansible 15 Oct 2026
- 0184 Ansible Vault: keep passwords and keys encrypted in your repo Ansible Vault encrypts variable files or single values so secrets can live in Git next to your playbooks. Create, edit and rekey… Advanced · Step by step · Linux, macOS Article Ansible 15 Oct 2026
- 0186 Fix: Ansible UNREACHABLE! Failed to connect to the host via ssh Ansible could not open an SSH session to the host, so no task ran. The text after the colon tells you why:… Intermediate · Fixes an error · Linux, macOS Article Ansible · SSH & servers 15 Oct 2026
- 0254 Automate three servers with Ansible: roles, templates, Vault and safe dry runs Build a real Ansible project for two web servers and one database server: inventory groups, SSH keys, roles, templates, handlers, an encrypted… Intermediate · Guided project · Linux, macOS Article Ansible · Keys & config 15 Oct 2026
Repos in the wild
- Ansible: agentless IT automation ansible/ansible The source repository of ansible-core, the engine behind Ansible playbooks, ad-hoc commands and the ansible-galaxy and ansible-vault tools. GitHub
- dev-sec ansible-collection-hardening: tested hardening roles for Linux, SSH and nginx dev-sec/ansible-collection-hardening An Ansible collection that applies a well-documented hardening baseline to Linux hosts, the SSH server and client, nginx and MySQL, with every setting adjustable through variables. GitHub
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Dominik +3,597 chars