🎉 NEW YEAR SALE! 40% OFF on Annual Premium+ Plan - Till 31st Dec! Use SUPERSALE40 Shop Now →

10 Essential Ansible Ad-Hoc Commands Every Sysadmin Should Know

Published On: 22 December 2025

Objective

In the fast-paced world of Linux system administration, efficiency is everything. Whether you are managing one server or hundreds, having the ability to execute quick, one-time tasks without writing a full playbook is a game-changer. That is where Ansible ad-hoc commands come into play. These powerful one-liners allow sysadmins to manage systems instantly from installing packages and checking disk space to rebooting servers and managing users.

This blog aims to equip Linux system administrators and RHCE aspirants with the most practical and essential Ansible ad-hoc commands. Ad-hoc commands are the Swiss Army knife of automation, which is perfect for quick, one-time tasks without the complexity of playbooks. Whether you are troubleshooting, managing services, or deploying software, these one-liners can save you time and effort. If you are preparing for the RHCE exam, mastering these commands is critical. In this blog, we will explore 10 essential Ansible ad-hoc commands that every sysadmin should know. Whether you are just starting with Ansible or preparing for the RHCE exam, mastering these commands will boost your confidence as well as productivity.

What Are Ad-Hoc Commands in Ansible?

Ad-hoc commands in Ansible are used for quick and direct execution of tasks on remote hosts using modules like ping, yum, copy, and service. Unlike playbooks, they are ideal for short, immediate tasks. They are executed via the ansible command-line tool and can be used to manage inventory, gather facts, run shell commands, and more.

Prerequisites

Before running these commands, ensure you have:

  • Ansible installed on your control node
  • SSH access configured to your managed nodes
  • An inventory file set up (default: /etc/ansible/hosts)
  • Python installed on all managed nodes
  • Appropriate sudo privileges where needed

10 Essential Ad-Hoc Commands

1. Ping All Hosts

ansible all -m ping

Purpose: Tests SSH connectivity and Python availability on all managed nodes.

This command ensures that all your systems are reachable via SSH and are ready to be managed by Ansible.

Expected Output:

server1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

2. Gather System Facts

ansible all -m setup

Purpose: Collects detailed facts (e.g., IP address, memory, OS version) from all nodes.

Useful for auditing and troubleshooting, this command retrieves system-level data that can also be used in conditional playbooks.

Expected Output: A large JSON structure containing system information like ansible_distribution, ansible_memtotal_mb, ansible_processor_cores, etc.

3. Run a Shell Command

ansible all -m shell -a "uptime"

Purpose: Shows how long the servers have been running and their load averages.

Executes any shell command directly on all target systems, helping with real-time monitoring.

Expected Output:

server1 | SUCCESS | rc=0 >>
 14:23:45 up 10 days,  3:42,  2 users,  load average: 0.15, 0.10, 0.08

4. Install a Package Using YUM

ansible all -m yum -a "name=vim state=present" --become

Purpose: Installs the 'vim' text editor on all RHEL-based systems.

This is especially useful for deploying common tools or dependencies across multiple servers. The --become flag escalates privileges to root.

Expected Output:

server1 | CHANGED => {
    "changed": true,
    "msg": "Package vim installed"
}

5. Start and Enable a Service

ansible all -m service -a "name=sshd state=started enabled=yes" --become

Purpose: Ensures SSH is running and enabled at boot.

Automate service management to maintain consistency across your infrastructure.

Expected Output:

server1 | SUCCESS => {
    "changed": false,
    "enabled": true,
    "name": "sshd",
    "state": "started"
}

6. Create a New User

ansible all -m user -a "name=devuser state=present" --become

Purpose: Adds a user named 'devuser' to all systems.

Ideal for onboarding new team members or deploying system accounts for automation.

Expected Output:

server1 | CHANGED => {
    "changed": true,
    "name": "devuser",
    "state": "present"
}

7. Copy a File to Remote Systems

ansible all -m copy -a "src=/home/admin/notes.txt dest=/tmp/notes.txt" --become

Purpose: Copies a local file to all remote hosts.

Great for distributing configuration files, templates, or documentation.

Expected Output:

server1 | CHANGED => {
    "changed": true,
    "dest": "/tmp/notes.txt",
    "mode": "0644"
}

8. Check Disk Usage

ansible all -m shell -a "df -h"

Purpose: Displays disk space usage in a human-readable format.

Quickly monitor storage capacity on all managed servers to prevent downtime.

Expected Output:

server1 | SUCCESS | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   35G   15G  70% /
tmpfs           7.8G     0  7.8G   0% /dev/shm

9. Reboot All Servers

ansible all -m reboot --become

Purpose: Reboots all target machines and waits for them to come back online.

Useful during kernel updates or scheduled maintenance windows. Ansible will wait for systems to become responsive again before completing.

Expected Output:

server1 | CHANGED => {
    "changed": true,
    "elapsed": 45,
    "rebooted": true
}

10. Check Who is Logged In

ansible all -m command -a "who"

Purpose: Lists current users logged into each machine.

Provides a snapshot of active user sessions, useful for security auditing.

Expected Output:

server1 | SUCCESS | rc=0 >>
root     pts/0        2024-12-05 14:10 (192.168.1.50)
admin    pts/1        2024-12-05 13:45 (192.168.1.51)

Explanation of Each Command

  • Ping All Hosts: This command uses the ping module to verify connectivity between your Ansible control node and all target machines. It confirms that SSH access is working and Python is installed on remote nodes.
  • Gather System Facts: The setup module collects detailed information about remote systems including OS version, memory, network interfaces, and more. This data is useful for inventory auditing or use in playbook conditionals.
  • Run Shell Commands: The shell module allows execution of shell-based commands (including pipelines and redirection). Using uptime shows system load averages and uptime durations for each server. Note: Use shell only when you need shell features; otherwise, prefer the command module for security and performance.
  • Install a Package: This command uses the yum module to install the vim editor. The --become flag is necessary because installing software typically requires root privileges.
  • Start and Enable Service: Ensures the sshd service is running and will start automatically on boot. This is vital for maintaining SSH access after system restarts.
  • Create User: Adds a user named devuser using the user module. It is a simple way to onboard new users across multiple systems simultaneously.
  • Copy File: The copy module transfers a file from the control node to all managed hosts. This is often used for configuration files, scripts, or documentation.
  • Check Disk Usage: This shell command executes df -h to show human-readable disk usage. It helps identify full or nearly-full partitions quickly.
  • Reboot Servers: Uses the reboot module to restart machines. Ansible waits for each system to go down and come back online before reporting success.
  • Check Logged-In Users: The command module runs who to display currently logged-in users. Useful for tracking system access and detecting suspicious sessions.

Targeting Specific Hosts

Instead of using all, you can target specific hosts or groups:

  • ansible webservers -m ping - Target a specific group
  • ansible server1 -m ping - Target a single host
  • ansible all -l server1,server2 -m ping - Limit to specific hosts
  • ansible all -i /path/to/inventory -m ping - Use a custom inventory file

Try the ad-hoc lab to get immediate hands-on practice and experiment with the concepts at your own pace.

Common Mistakes to Avoid

  • Missing --become: Tasks like package installation or user creation need root privileges. Always use --become where necessary.
  • Using shell When command Would Work: The shell module has more overhead and potential security risks. Use command for basic tasks that don't require shell features like pipes, redirects, or environment variables.
  • Not Specifying Inventory: If your inventory file is not in the default location, use -i /path/to/inventory to avoid targeting the wrong systems.
  • Forgetting to Test First: Always test commands on a single host or small group before running them against all systems using the -l flag to limit scope.
  • Ignoring Error Messages: If a command fails, read the error output carefully. Common issues include SSH key problems, missing Python, or insufficient permissions.

Troubleshooting Tips

  • If SSH fails, verify your SSH keys are properly configured
  • Use -vvv flag for verbose output to debug issues
  • Check that Python is installed on target hosts
  • Ensure your user has appropriate sudo privileges when using --become
  • Verify your inventory file syntax is correct

Conclusion

Ad-hoc commands are critically important for every Linux sysadmin who uses Ansible. They offer a powerful way to quickly perform administrative tasks without diving into playbooks. Whether you are managing 5 servers or 500, these one-liners can make your job easier and your systems more reliable. Mastering these commands is not just good practice, it is a requirement if you are preparing for the RHCE certification or working in any DevOps or system administration role. Start practicing these commands in your test environment today, and soon they will become second nature in your daily workflow.