What is an ad hoc command in Ansible?

Ansible is an open-source, agentless automation tool that makes IT job orchestration, application deployment, and system management more effortless. It enables automation across numerous platforms using YAML A human-readable data-serialization languageto define configurations and tasks. With a push-based paradigm, Ansible connects with distant hosts via SSH or WinRMWindows Remote Management, supporting both Linux and Windows environments. Thanks to its broad module library, users may automate software installation, configuration file management, and service control.

Ad hoc in Ansible

Without developing a playbook, Ansible's ad hoc commands quickly and straightforwardly complete one-off operations or gather data from remote systems. They offer simplicity and flexibility for tasks that don't need intricate orchestration.

Ad hoc commands enable processes like acquiring system data, running shell commands, managing services, copying files, and installing packages against specified hosts. They are carried out using the -m flag on the Ansible command-line tool. Ad hoc instructions are necessary for short, real-time jobs in everyday operations, while playbooks are advised for complicated procedures.

Syntax of ad hoc command

An ad hoc command must have the following syntax to be executed.

ansible <host-pattern> [options]
ad hoc command syntax

For example, the command should be written as follows.

ansible db_servers -m yum -a "name=mysql-server state=present"
This command installs the MySQL server package (mysql-server) on hosts

Here db_servers serves as the <hostgroup>, -m yum as <module name> and -a "name=mysql-server state=present" as the <module arguments>.

A single Ansible ad hoc command can have multiple flags. -m and -a are one of the widely used. -m is used to specify the module name that will be used and -a is used to provide the module arguments.

Use cases

Ad hoc commands can gather system facts, execute shell commands, file operations, manage services, and more. Any Ansible module can be used in an ad hoc task. Ad hoc activities, such as playbooks, employ a declarative model to determine and carry out the steps necessary to arrive at a predetermined ultimate state. They achieve a type of idempotencya characteristic of operations or API calls that guarantees numerous executions of the same operation yield the same outcome as a single execution. by determining the present condition before they start and taking no action unless it differs from the desired final state.

  • Managing services: Services on distant hosts can be started, stopped, restarted, or checked for status using ad hoc instructions. Without creating a playbook, this is useful for managing web servers, databases, or application services.

ansible web_servers -m service -a "name=httpd state=started"
Ensures that the httpd service is running on those hosts
  • Execute shell commands: Ad hoc commands allow shell commands to be run on distant hosts. This will enable admins to complete rapid activities like executing diagnostic commands, reviewing system logs, or double-checking setups.

ansible all -m shell -a "ls -l /var/log"
Lists the connects from the given directory on each host
  • Gathering system facts: You can retrieve system data and facts from distant hosts using ad hoc commands. Inspecting hardware, network settings, installed applications, and other system specifics may be done rapidly.

ansible all -m setup
Gathers information and facts from all hosts defined in the inventory
  • Copying files: Ad hoc commands enable the copying of files or directories to distant hosts, the modification of file permissions, or the deletion of files. This is useful for managing file-related operations, deploying scripts, or distributing configuration files.

ansible app_servers -m copy -a "src=/path/to/local/file.txt dest=/tmp/file.txt"
Copies a local file to the path on host
  • Packages installation: On distant hosts, packages can be installed or updated using ad hoc commands. This enables administrators to install updates or software packages across numerous systems swiftly.

$ ansible webservers -m ansible.builtin.yum -a "name=acme state=present"
Ensures that package is installed without updating it

These are just a few instances of the various use cases for ad hoc commands in Ansible. Due to its adaptability and flexibility, ad hoc can be used for multiple fast, one-time operations in IT infrastructure management and automation.

Copyright ©2024 Educative, Inc. All rights reserved