Using ad-hoc Commands in Ansible

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. 

Topics Include:

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.

Rebooting Servers

Command Function
ansible servergroup -a "/sbin/reboot"Reboot all servers in servergroup.
ansible servergroup -a "/sbin/reboot" -f 10Reboot all servers in servergroup with 10 parallel forks. This facilitates a faster reboot cycle. 
ansible servergroup -a "/sbin/reboot" -f 10 -usernameReboot 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.

Gathering Facts

CommandFunction
ansible servergroup -m setupGathers facts about all servers in servergroup. 

Pinging Servers

CommandFunction
ansible servergroup -m setupPings all servers in servergroup. 

Managing Files

CommandFunction
ansible servergroup -m copy -a "src=/etc/hosts dest=/tmp/hosts"Copies files from a single source to a given destination on all servers in servergroup. 
ansible servergroup -m file -a "dest=/path/to/file.txt mode=600"Changes the permission of a given file on all servers in servergroup. 
ansible servergroup -m file -a "dest=/path/to/file.txt mode=600 owner=username group=groupname"Changes ownership of a given file on all servers in servergroup
ansible servergroup -m file -a "dest=/path/to/file.txt mode=755 owner=username group=groupname state=directory"Creates a directory in a given location on all servers in servergroup.
ansible servergroup -m file -a "dest=/path/to/file.txt state=absent"Deletes a directory in a given location on all servers in servergroup.

Managing Packages

CommandFunction
ansible servergroup -m yum -a "name=packagename state=present"Uses yum package manager to install a given package on all servers in servergroup. 
ansible servergroup -m yum -a "name=packagename.1.5 state=presentUses yum package manager to install a specific version of a package on all servers in servergroup.
ansible servergroup -m yum -a "name=packagename state=latest"Uses yum package manager to install the latest version of a package on all servers in servergroup. 
ansible servergroup -m yum -a "name=packagename state=absent"Uses yum package manager to ensure a given package is not installed on all servers in servergroup. 

Managing Users

CommandFunction
ansible all -m user -a "name=username password=<password>"Creates a user with a given password on all servers in inventory. 
ansible all -m user -a "name=username state=absent"Deletes a user on all servers in inventory.

Managing Services

CommandFunction
ansible servergroup -m service -a "name=servicename state=started"Starts a given service on all servers in servergroup. 
ansible servergroup -m service -a "name=servicename state=restarted"Restarts a given service on all servers in servergroup.
ansible servergroup -m service -a "name=servicename state=stopped"Stops a given service on all servers in servergroup.

Was this article helpful? Let us know!