Ansible ad hoc commands let you run one-off tasks directly from the CLI without writing a playbook. Current stable version: Ansible-core 2.20.2 (released January 29, 2026). These commands are perfect for quick checks, system maintenance, and simple administrative tasks across multiple hosts.
ansible <host-pattern> -m <module> -a "<module arguments>" [options]
Examples:
ansible all -m ping
command module by default):ansible webservers -a "uptime"
ansible dbservers -m command -a "id"
-i inventory.ini – inventory file if not using default.
-u username – remote user.
-b / --become – use privilege escalation (sudo).
ansible all -m copy -a "src=/tmp/file dest=/etc/file" -b
-k – ask for SSH password, -K – ask for become password.
-f 10 – set forks, i.e., parallelism.
-v, -vv, -vvv, -vvvv – increase verbosity level.
--check – run in check mode (dry run).
--diff – show differences when changing files.
-e "variable=value" – set additional variables.
Specify which hosts to target with various patterns:
all – all hosts in inventorywebservers – all hosts in the ‘webservers’ groupwebservers:dbservers – hosts in either groupwebservers:&dbservers – hosts in both groups (intersection)webservers:!frontend – hosts in webservers but not in frontendwebservers[0] – first host in webservers groupwebservers[0:2] – first two hosts in webservers groupweb[1:5] – web1, web2, web3, web4, web5Check connectivity to all hosts:
ansible all -m ping
Get system facts:
ansible webservers -m setup
Check disk space:
ansible all -m shell -a "df -h"
Check uptime:
ansible all -m command -a "uptime"
Install a package with privilege escalation:
ansible app -b -m yum -a "name=vim state=latest"
# or Debian/Ubuntu:
ansible app -b -m apt -a "name=vim state=latest update_cache=yes"
Check if a package is installed:
ansible all -b -m package_facts -a "manager=auto"
ansible all -m debug -a "var=packages['vim']"
Copy a file to all hosts:
ansible all -m copy -a "src=/path/to/local/file dest=/path/on/remote"
Create a directory in an idempotent way:
ansible nodes -m file -a "path=/tmp/mydir state=directory mode='0755'"
Create or modify a file:
ansible webservers -b -m lineinfile -a "path=/etc/motd line='Welcome to our server' state=present"
Manage a service:
ansible web -b -m systemd -a "name=nginx state=restarted"
# or for older systems:
ansible web -b -m service -a "name=nginx state=restarted"
Check service status:
ansible all -b -m systemd -a "name=httpd state=started"
Add a user:
ansible all -b -m user -a "name=johndoe comment='John Doe' shell=/bin/bash"
Kill a process:
ansible all -b -m shell -a "pkill -f 'process_name'"
Check listening ports:
ansible all -m shell -a "netstat -tlnp"
Update firewall rules:
ansible webservers -b -m firewalld -a "port=80/tcp permanent=true state=enabled"
ansible webservers -e "target_port=80" -m shell -a "netstat -an | grep :{{ target_port }}"
ansible all -m shell -a "yum update -y" -b --limit "ansible_os_family == 'RedHat'"
ansible all -m setup -a "filter=ansible_distribution*"
Format output in YAML:
ansible all -m ping -o # One-line output
Save results to a file:
ansible all -m setup > facts_output.json
--check mode to see what changes would be made-f to control how many hosts run simultaneouslyCommon issues and solutions:
Any questions?
Feel free to contact us. Find all contact information on our contact page.