10 Ansible Ad Hoc Commands That Will Make You More Efficient

ansible ad hoc command

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

Ansible Ad Hoc Commands are your best friend when managing, troubleshooting, and testing one-off tasks across dozens.

Ansible is a parallel automation solution utilizing playbooks and ad hoc commands to enhance the sysadmin’s workflow. For one-time tasks, these individual linear commands executed from controllers are beneficial, transforming traditional sequential scripting and enabling automation.

Let’s dive into what an Ansible ad hoc command is and the top 10 Ansible commands. So if you’re in IT, DevOps, and system administration, this guide is for you!

Lets Learn Along!

What Are Ansible Ad Hoc Commands?

Ansible Ad hoc commands are single, one-time commands used for quick tasks.

For example, if you need to reboot all company servers, you can use these commands from /usr/bin/ansible. Ad hoc commands are only for one-time use and are not for ongoing configuration or deployment.

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

Why Use Ad Hoc Commands Instead of Playbooks?

Commands can be run directly in the command line of an Ansible control node, eliminating the need for a playbook or extra setup. These commands enable users to swiftly carry out tasks on one or more managed nodes, like verifying a service’s status or installing software.

Ansible Ad-hoc commands are structured in a straightforward, single-line format that specifies the module to be used, any required parameters or options, and the name or IP address of the targeted managed nodes. This feature of Ansible is highly effective for the rapid and efficient management of IT infrastructure.

Ad hoc commands are useful for infrequent tasks. For instance, if you need to shut down all your work computers for a holiday, you can use a quick ad hoc command rather than creating a complete playbook.

Command:

ansible [target computers] -m [action] -a "[instructions]"

In the -a section, you provide additional information about your action. You can format these details in two ways:

Simple format: key=value
More detailed format: Start with { and end with }

Playbooks, on the other hand, are effective for structured and repetitive automation tasks. However, for quick tasks like checking uptime, applying a configuration, or restarting a service, using ad hoc commands is much faster.

Command 1: Ping All Hosts (ansible all -m ping)

This command checks if all your computers are online, kind of like knocking on each door to see if someone is home. When a computer replies, you’ll see “SUCCESS” along with “ping”: “pong”. This indicates which computers are ready to receive commands. It’s a fast way to confirm that everything is connected before diving into more complicated tasks.

Command:

ansible all -m ping

Command 2: Reboot Remote Servers

Managing Multiple Computers Simultaneously.

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

The Ansible ad hoc command generally defaults to the ‘command’ module, allowing you to perform simple tasks on multiple computers simultaneously. To restart all computers in a group named ‘Atlanta‘, use the command below. This command instructs all computers in the ‘Atlanta‘ group to restart.

By default, Ansible communicates with 5 computers at a time. To change this, you can use the command below. The “-f 10” option instructs Ansible to handle 10 computers at once.

Command:

ansible atlanta -a "/sbin/reboot" -f 10

Command 3: Run Shell Commands

Check out this command:

ansible all -m shell -a 'uptime'

Curious about how long your servers have been up? This is the go-to command for that.

You can also use ‘uptime’ in place of any shell command, such as df—h, whoami, or your custom scripts.

Command 4: Copy Files to Remote Hosts

ansible all -m copy -a "src=/home/user/config.txt dest=/tmp/config.txt"

Creating a new directory

$ Ansible abc -m file -a "dest = /path/user1/new mode = 777 owner = user1 group = user1 state = directory" 

Deleting the =whole directory and files

$ Ansible abc -m file -a "dest = /path/user1/new state = absent"

Command 5: Install Packages

ansible all -m apt -a "name=htop state=present update_cache=yes"

There are modules for yum and apt. Here are some yum examples.

To ensure a package is installed without updating it, use: $ ansible webservers -m yum -a “name=acme state=present”.

To install a specific version of a package, use:

$ ansible webservers -m yum -a "name=acme-1.5 state=present". 

To ensure a package is updated to the latest version, use:

$ ansible webservers -m yum -a "name=acme state=latest". 

To ensure a package is not installed, use:

$ ansible webservers -m yum -a "name=acme state=absent". 

Ansible has modules for many platforms. If your package manager lacks a module, you can use the command module to install packages or contribute a module for it. Check the mailing list for more information.

Command 6: Create Users

The ‘user’ module allows easy creation and manipulation of existing user accounts, as well as removal of user accounts that may exist:

$ ansible all -m user -a "name=foo password=<crypted password here>"

$ ansible all -m user -a "name=foo state=absent"

Best Practices For Using Ad Hoc Commands in Production

  • Use roles for reusable automation.
  • Organize inventory and tasks logically.
  • Employ ansible-lint to maintain standards.
  • Be cautious with root access; use become wisely.
  • Avoid hardcoding by utilizing variables, templates, and secure vaults.
  • Simplify your work – Create clear, easy-to-read, and organized tasks.
  • Keep configuration separate from logic for better scalability with templates and variables.
  • Implement handlers for idempotency to ensure a secure and predictable infrastructure.

Conclusion: When to Use Ansible Ad Hoc Commands vs. When to Use Playbooks

Ansible ad hoc commands are fast and effective for executing single tasks on several computers. In 2025, infrastructure has become extremely dynamic, fast command-line tools are required more than ever. It’s crucial to master them and you’ll unlock the true speed and simplicity of Ansible.

But they should be used cautiously for tasks that need to be repeated and verified before execution.

FAQ’s

1. Can I execute Ansible ad hoc commands with sudo? Yes, use

--become: ansible all -m shell -a 'apt update' --become.

2. Are Ansible ad hoc commands safe for production? They can be, but always test them first.

3. Can I use ad hoc commands in cloud environments? Yes, use dynamic inventories like the AWS EC2 plugin to target your hosts.

4. How do I run Ansible ad hoc commands on a specific host? Us

 --limit: ansible all -m ping --limit 'webserver1'.
Areeba Nauman
Areeba is a Content Writer with expertise in web content and social media, she can simplify complex concepts to engage diverse audiences. Fueled by creativity and driven by results, she brings a unique perspective and a keen attention to detail to every project she undertakes with her creativity and passion for delivering impactful content strategies for success. Let's connect on Linkedin: https://www.linkedin.com/in/areeba-bhatti/
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!