Ansible is a powerful automation tool that allows you to manage and configure infrastructure as code. Back when we were running 100s of hadoop nodes these we the helpful commands which could quickly get information from the node.

While playbooks are the core of Ansible’s functionality, ad-hoc commands offer a way to perform quick, one-off tasks without the need for writing a full playbook. This blog post will showcase the power of Ansible ad-hoc commands and demonstrate how they can be used for everyday system administration tasks.

What are Ansible Ad-Hoc Commands?

Ad-hoc commands are simple, one-line commands that you can run directly from the command line using the ansible command-line tool. They are perfect for tasks that don’t require the complexity of a full playbook, such as checking server status, restarting services, or gathering system information.

Why Use Ad-Hoc Commands?

  • Speed and Simplicity: Execute tasks quickly without writing and managing complex playbooks.
  • Immediate Results: Get immediate feedback on the status of your infrastructure.
  • Flexibility: Adapt to changing needs and perform tasks on the fly.
  • Debugging: Useful for quickly troubleshooting issues and gathering information.

Basic Syntax

The basic syntax of an Ansible ad-hoc command is as follows:

ansible <inventory> -m <module> -a "<module_arguments>"
  • <inventory>: Specifies the target hosts. This can be a hostname, an IP address, or a group defined in your Ansible inventory file.
  • -m <module>: Specifies the Ansible module to use (e.g., ping, command, shell, apt).
  • -a "<module_arguments>": Specifies the arguments to pass to the module. Arguments are passed as a single quoted string.

Examples of Ad-Hoc Commands

Let’s look at some practical examples of using ad-hoc commands:

1. Checking Server Uptime

To check the uptime of all servers in your inventory, you can use the command module:

ansible all -m command -a "uptime"

This command will execute the uptime command on all hosts defined in your inventory file and display the output.

2. Rebooting a Group of Machines

To reboot a group of machines (e.g., a group named “webservers”), you can use the reboot module:

ansible webservers -m reboot -a "reboot_timeout=600" -b
  • webservers: The group name defined in your inventory.
  • -m reboot: Uses the reboot module.
  • -a "reboot_timeout=600": Sets the timeout for the reboot operation to 600 seconds.
  • -b: Runs the command with elevated privileges (sudo).

3. Checking Disk Space

To check the disk space on all servers, you can use the shell module in conjunction with the df -h command:

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

This command executes the df -h command on all hosts and displays the human-readable output.

4. Pinging Hosts

To quickly check if hosts are reachable, you can use the ping module:

ansible all -m ping

This will return “SUCCESS” if the hosts are reachable, and “FAILED” if they are not.

5. Installing a Package

To install a package, for example, nginx, on Debian/Ubuntu based systems you can use the apt module:

ansible webservers -m apt -a "name=nginx state=present" -b
  • webservers: Target hosts.
  • -m apt: Uses the apt module for package management.
  • -a "name=nginx state=present": Specifies that we want to install the nginx package and ensure it is present.
  • -b: Executes with sudo privileges.

6. Copying a File

To copy a file from your Ansible control node to the managed hosts, you can use the copy module:

ansible all -m copy -a "src=/path/to/local/file dest=/path/to/remote/file owner=root group=root mode=0644" -b
  • src: The source file on the Ansible control node.
  • dest: The destination path on the managed hosts.
  • owner, group, and mode: Set the file ownership and permissions.
  • -b: Runs the command with elevated privileges (sudo).

Tips and Best Practices

  • Use Groups: Organize your hosts into groups in your inventory file for easier management.
  • Be Careful with the shell Module: Use the shell module only when necessary, as it executes arbitrary shell commands. Prefer using specific modules when available.
  • Use -k for Password Authentication: If you don’t have SSH keys configured, use the -k option to prompt for a password.
  • Use -K for Become Password Authentication: Use the -K option to prompt for the become (sudo) password.
  • Check Module Documentation: Refer to the Ansible documentation for detailed information on each module’s options and usage. You can usually do this using ansible-doc <module_name>.

Conclusion

Ansible ad-hoc commands are a valuable tool for system administrators and DevOps engineers. They provide a quick and efficient way to perform common tasks without the overhead of writing and managing full playbooks. By understanding the basic syntax and available modules, you can leverage ad-hoc commands to streamline your workflow and improve your productivity. Remember to always exercise caution and test your commands in a safe environment before running them on production systems.