Running Ad Hoc Commands
Explore the common ad hoc commands available in Ansible and learn how to use them.
Introduction to Ansible ad hoc commands
Ansible provides modules that give us the power to manage our list of hosts. These modules can be referenced from within our playbooks or by using ad hoc commands. Ad hoc commands are one-liners that we can run to quickly execute a particular task. The basic syntax of an ad hoc command is shown below:
ansible <hosts> -i <inventory_file> -m <module_name> -a <module_arguments> -u <remote_user> --private-key=<path_to_ssh_key>
Ad hoc command syntax
The syntax for ad hoc commands includes the following parameters:
hosts
: The name of the host or group of hosts against which to run the command.inventory_file
: The path to the inventory file to use. It is optional.module_name
: The Ansible module to run, e.g.,shell
.module_arguments
: The needed arguments for the module in use.remote_user
: The specific user on the managed node that Ansible uses to execute the command. It is optional.path_to_ssh_key
: The path to the SSH key for the connection. It is optional.