ansibleRun one task across many servers (ad-hoc)
The ansible command runs a single module against a group of hosts from your inventory, over SSH, with no agent on the servers: check they're reachable, run a command everywhere, copy a file, restart a service or read facts. Playbooks (ansible-playbook) are the repeatable version.
ansible PATTERN -m MODULE [-a 'ARGS'] [-i INVENTORY] [-b] [-u USER] [-f FORKS] [--limit HOSTS] [--check]Before running anything, check that the control node can SSH into every host and run Python on it.
ansibleAd-hoc mode: run one module once against the matched hosts.
Ansible runs on a control node (your laptop or a CI runner); targets only need SSH and Python. It reads ansible.cfg and the inventory, expands the pattern into hosts and their variables, and starts up to -f forks. For each host it opens an SSH connection (reused through ControlPersist), and for a Python module it builds an AnsiballZ zip of the module and its arguments, copies it to a temporary directory on the target (or pipes it), runs it with the target's Python, optionally through sudo with -b, and deletes it. The module does its work idempotently, checking current state first, and prints JSON: changed true/false, and module-specific fields. Ansible prints one result per host: SUCCESS (green, nothing changed), CHANGED (yellow), FAILED (red) or UNREACHABLE (SSH failed). The raw module is the exception: it runs a command without Python, used to bootstrap Python.
-i FILEInventory file or directory (or set it in ansible.cfg)-m MODULEModule to run (default ansible.builtin.command)-a 'ARGS'Module arguments-b / -KBecome root via sudo / ask for the sudo password-u USERSSH as this user-f NParallel forks (default 5)-l, --limitFurther restrict the matched hosts-C, --checkDry run for modules that support it-e 'k=v'Extra variables (highest precedence)--list-hostsShow which hosts the pattern matches, run nothing-v ... -vvvvMore output; -vvvv shows SSH detailsansible all -m shell -a 'apt-get install -y nginx' -bNot idempotent, no clear changed/ok status, and it breaks if apt is locked or the package name differs per distro.
ansible all -m ansible.builtin.apt -a 'name=nginx state=present update_cache=true' -bRunning ad-hoc changes on prod as the normal way of workingNothing is recorded or reviewable; the next playbook run may undo it.
Use ad-hoc for checks and emergencies; put real changes in a playbook in GitForgetting -b for system changesThe module runs as the SSH user and fails with permission denied on root-owned files.
Add -b (and -K if sudo needs a password)ansible all ... with the default 5 forks on 500 hosts during an incidentIt processes 5 hosts at a time and takes ages.
-f 50 (within what the control node and SSH can handle)Try these on a Linux machine of your own. Each task has a hint and a solution.
Write the command that shows free disk space on every host in the db group.
Write the command that makes sure the user deploy exists with shell /bin/bash on all web hosts.
Read this output and explain it: 'web2 | UNREACHABLE! => ... "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey)."'
If ansible is installed in your environment, run an ad-hoc command against localhost. Otherwise run the equivalent check by hand in the sandbox: uptime and free disk space.
ansibleRun one task across many servers (ad-hoc)
The ansible command runs a single module against a group of hosts from your inventory, over SSH, with no agent on the servers: check they're reachable, run a command everywhere, copy a file, restart a service or read facts. Playbooks (ansible-playbook) are the repeatable version.
ansible PATTERN -m MODULE [-a 'ARGS'] [-i INVENTORY] [-b] [-u USER] [-f FORKS] [--limit HOSTS] [--check]Before running anything, check that the control node can SSH into every host and run Python on it.
ansibleAd-hoc mode: run one module once against the matched hosts.
Ansible runs on a control node (your laptop or a CI runner); targets only need SSH and Python. It reads ansible.cfg and the inventory, expands the pattern into hosts and their variables, and starts up to -f forks. For each host it opens an SSH connection (reused through ControlPersist), and for a Python module it builds an AnsiballZ zip of the module and its arguments, copies it to a temporary directory on the target (or pipes it), runs it with the target's Python, optionally through sudo with -b, and deletes it. The module does its work idempotently, checking current state first, and prints JSON: changed true/false, and module-specific fields. Ansible prints one result per host: SUCCESS (green, nothing changed), CHANGED (yellow), FAILED (red) or UNREACHABLE (SSH failed). The raw module is the exception: it runs a command without Python, used to bootstrap Python.
-i FILEInventory file or directory (or set it in ansible.cfg)-m MODULEModule to run (default ansible.builtin.command)-a 'ARGS'Module arguments-b / -KBecome root via sudo / ask for the sudo password-u USERSSH as this user-f NParallel forks (default 5)-l, --limitFurther restrict the matched hosts-C, --checkDry run for modules that support it-e 'k=v'Extra variables (highest precedence)--list-hostsShow which hosts the pattern matches, run nothing-v ... -vvvvMore output; -vvvv shows SSH detailsansible all -m shell -a 'apt-get install -y nginx' -bNot idempotent, no clear changed/ok status, and it breaks if apt is locked or the package name differs per distro.
ansible all -m ansible.builtin.apt -a 'name=nginx state=present update_cache=true' -bRunning ad-hoc changes on prod as the normal way of workingNothing is recorded or reviewable; the next playbook run may undo it.
Use ad-hoc for checks and emergencies; put real changes in a playbook in GitForgetting -b for system changesThe module runs as the SSH user and fails with permission denied on root-owned files.
Add -b (and -K if sudo needs a password)ansible all ... with the default 5 forks on 500 hosts during an incidentIt processes 5 hosts at a time and takes ages.
-f 50 (within what the control node and SSH can handle)Try these on a Linux machine of your own. Each task has a hint and a solution.
Write the command that shows free disk space on every host in the db group.
Write the command that makes sure the user deploy exists with shell /bin/bash on all web hosts.
Read this output and explain it: 'web2 | UNREACHABLE! => ... "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey)."'
If ansible is installed in your environment, run an ad-hoc command against localhost. Otherwise run the equivalent check by hand in the sandbox: uptime and free disk space.