Ansible – Ad-hoc Command Structure

Ansible provides us the option to either run playbooks or ad-hoc commands on the local and remote nodes through ansible engines.

Let us look at an Ansible ad-hoc command example

> ansible localhost -m shell -a "uptime"

The above command can be divided into the following parts,

  • localhost – You can specify the target host/groups here where you want the command to be executed.
  • -m shell – You define the Ansible module, in our example shell.
  • -a “uptime” – Pass the command arguments in double quotes string.

To run the commands on custom inventory host files,

> ansible localhost -m shell -a "uptime" -i custom_inventory_file

On a similar line, we can create our own ad-hoc commands.