While writing Ansible playbooks allows you to pre-plan automated software deployments and other tasks, you may find yourself in need of simpler commands that allow you to do less complex tasks such as powering down servers or managing users. For these tasks, you may want to use ad-hoc commands. In this article, we will outline how ad-hoc commands can be used to manage your Ansible workflow.
Use ad-hoc commands to get started with Ansible today!
What are ad-hoc Commands?
Ad-hoc commands are commands that use the
/usr/bin/ansible command-line interface to automate a single task across one or more nodes using built-in mechanisms such as modules. Modules are discrete units of code that can be used from the command-line or included in an Ansible playbook or role to carry out specific functions. These functions range from rebooting servers to managing users and software packages. Ad-hoc commands are well-suited for one-off tasks that don’t need to be repeated. Examples of such tasks are performing ping connection tests and gathering facts (system properties) about remote servers. As there are a wide variety of ad-hoc commands available for use, we will outline several examples of commonly used commands below and provide a brief description of each.
Ad-Hoc Command List
Prior to performing any of the tasks in this list, you will first need to set up your Ansible inventory.
The commands in the following chart use the placeholder term
servergroup. To use these commands, you will need to replace
servergroup with the group name of the servers being modified as specified in the inventory file.
|Reboot all servers in servergroup.|
|Reboot all servers in servergroup with 10 parallel forks. This facilitates a faster reboot cycle.|
|Reboot all servers in servergroup with 10 forks and as a given user.|
Please note, the reboot command will not work on Virtuozzo-based VPS platforms.
|Gathers facts about all servers in servergroup.|
|Pings all servers in servergroup.|
|Copies files from a single source to a given destination on all servers in servergroup.|
|Changes the permission of a given file on all servers in servergroup.|
|Changes ownership of a given file on all servers in servergroup|
|Creates a directory in a given location on all servers in servergroup.|
|Deletes a directory in a given location on all servers in servergroup.|
|Uses yum package manager to install a given package on all servers in servergroup.|
|Uses yum package manager to install a specific version of a package on all servers in servergroup.|
|Uses yum package manager to install the latest version of a package on all servers in servergroup.|
|Uses yum package manager to ensure a given package is not installed on all servers in servergroup.|
|Creates a user with a given password on all servers in inventory.|
|Deletes a user on all servers in inventory.|
|Starts a given service on all servers in servergroup.|
|Restarts a given service on all servers in servergroup.|
|Stops a given service on all servers in servergroup.|