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

Ad-Hoc Commands in Ansible: Run One-Liners Like A Pro

Published On: 5 December 2025

Objective

In the world of IT automation, Ansible stands out because of its simplicity, power, and agentless architecture. These powerful one-liners allow system administrators to perform quick tasks like restarting services, installing packages, or checking system health without writing a single line of YAML. The goal of this blog is to simplify the concept of Ansible ad-hoc commands for beginners and RHCE candidates. We will understand what ad-hoc commands are, why they matter, and how you can use them efficiently in real-world tasks. If you are starting with Ansible or preparing for the RHCE exam, understanding these one-liners will help you in this competitive environment.

What Are Ad-Hoc Commands in Ansible?

Ansible ad-hoc commands are simple, one-line instructions which you can run directly from your terminal to perform tasks on remote systems. Think of them as quick commands to complete a specific task, such as installing a package, restarting a service, or checking disk space without the overhead of writing and executing a full playbook.

In short, ad-hoc commands are perfect for:

  • Quick fixes
  • Immediate changes
  • Testing connections
  • Gathering information

These are typically run using the ansible command (not ansible-playbook) and executed on one or more hosts defined in your inventory file.

Why Use Ad-Hoc Commands?

Before diving into examples, let's clarify why ad-hoc commands matter:

  • Speed: Ad-hoc commands execute tasks instantly without the need to write, save, or manage a playbook. This makes them ideal for quick changes or urgent fixes.
  • Testing: They are perfect for verifying your Ansible setup, checking SSH access, or confirming that a module behaves as expected before using it in a playbook.
  • Simplicity: For one-time or routine tasks like restarting a service or adding a user, ad-hoc commands offer a fast and straightforward approach with minimal overhead.
  • Learning: Ad-hoc commands are an excellent way for beginners to get comfortable with Ansible's syntax, inventory system, and modules before moving on to playbooks.

If you are studying for the RHCE or just want to get better at automation, then learning how to use ad-hoc commands is a must.

Basic Syntax

Here's the general syntax for an ad-hoc command:

ansible <target> -m <module_name> -a "<module_arguments>"
  • <target>: Could be a single host, group, or all
  • -m: Specifies the Ansible module to use
  • -a: Provides arguments to the module

You will also usually want to specify the user and become (sudo) privileges:

ansible all -m <module> -a "<arguments>" -u <username> --become

Step-by-Step: Practical Examples

Let's walk through some practical ad-hoc commands you can use every day as a sysadmin, with a clear explanation of what each command does.

1. Ping All Hosts

ansible all -m ping    # Checks SSH and Python connectivity to all hosts

This command verifies whether all the systems listed in your inventory are reachable via SSH and can execute Python, a key requirement for Ansible.

2. Gather System Facts

ansible all -m setup     # Gathers detailed system info like CPU, memory, and network

This command collects and displays system facts such as hardware details, IP addresses, OS versions, and more, useful for auditing and playbook conditionals.

3. Run Shell Commands

ansible all -m shell -a "uptime"     # Shows how long each server has been running

This executes the standard Linux uptime command on all hosts, which helps check how long systems have been up and their load averages.

4. Install a Package

ansible webservers -m yum -a "name=httpd state=present" --become    # Installs Apache on RHEL-based systems

This installs the Apache HTTP server (httpd) on all hosts in the 'webservers' group, using yum and escalates privileges to root.

5. Start and Enable a Service

ansible all -m service -a "name=httpd state=started enabled=yes" --become     # Starts Apache and enables it on boot

Starts the Apache service on all managed hosts and ensures it will automatically start on boot—crucial for persistent services.

6. Create a New User

ansible all -m user -a "name=devuser state=present" --become    # Creates a user named devuser on all machines

Adds a new user named devuser across all target systems. Handy for onboarding new team members or setting up system accounts.

7. Copy a File

ansible all -m copy -a "src=/home/user/file.txt dest=/tmp/file.txt" --become    # Copies a file to /tmp on all remote systems

This transfers a local file to a specified path on all remote nodes, ensuring consistent configuration or content distribution.

8. Check Disk Usage

ansible all -m shell -a "df -h"    # Displays disk usage in human-readable format

Runs the df -h command to quickly check disk space availability on each system, a common task for performance monitoring.

9. Reboot Remote Machines

ansible all -m reboot --become    # Reboots all systems and waits for them to come back online

Reboots all target systems gracefully while ensuring Ansible waits for the systems to come back up post-reboot.

10. Check Logged-In Users

ansible all -m command -a "who"    # Lists current logged-in users on each host

Executes the who command to see who is logged in and from where, useful for audits or troubleshooting user sessions.

Common Mistakes to Avoid

Even though ad-hoc commands are simple, beginners often make some common mistakes:

  • Forgetting --become: Many system administration tasks like installing packages or restarting services require root privileges. If you forget to add --become, the command will fail due to insufficient permissions.
  • Using shell when command will do: The command module is safer and more predictable since it does not execute through a shell. Only use shell if you need features like pipes, redirection, or variables otherwise stick with command.
  • Not specifying inventory: If your system is not using the default inventory file or you have not configured it properly, forgetting to use -i <inventory_file> can result in targeting the wrong hosts or failing to run the command entirely.

When Not to Use Ad-Hoc Commands

While ad-hoc commands are great, they are not meant for everything. Avoid using them when:

  • You need repeatable automation.
  • Tasks require complex logic or multiple steps.
  • You want version control for your automation logic.

In such cases, writing a playbook is the smarter choice. Think of ad-hoc commands as your quick Swiss Army knife and playbooks as your full toolkit.

Pro Tips for Using Ad-Hoc Commands Efficiently

Use --limit to target a specific host

ansible all -m ping --limit "web1.example.com"   # Runs the ping only on web1.example.com

This limits the ad-hoc command to run only on the specified host web1.example.com, even if the inventory contains multiple hosts. It is useful when you want to isolate execution to a specific machine.

Use --one-line with setup to make fact output manageable

ansible all -m setup -a 'filter=ansible_default_ipv4' --one-line   # Retrieves only default IPv4 info

This gathers only the default IPv4 address from all hosts, displaying the output in a concise, one-line format per host. It is great for quickly checking network info without being overwhelmed by full fact data.

Use --check for dry-run mode with supported modules

Although --check is not supported by all ad-hoc commands, it works with certain modules and allows you to simulate changes without applying them.

Conclusion

Ansible ad-hoc commands are one of the most powerful ways to automate system administration tasks. From verifying SSH connectivity to managing services and users, these one-liners are essential for both everyday use and for exams like RHCE. Mastering them boosts your confidence and also enhances your ability to manage Linux servers. If you are aiming to pass the RHCE exam or want to become more skilled in Linux automation, then you should start practicing ad-hoc commands regularly. They are the perfect foundation before diving into playbooks and Ansible roles. At LinuxCert.Guru your one-stop resource for expert guidance, lab exercises, and exam tips for RHCE candidates. Whether you are just starting or brushing up before the exam, LinuxCert.Guru helps you stay ahead of the curve with practical knowledge and structured learning paths.