Ansible Ad-hoc Commands

Published Nov 13, 2017

Ad-hoc commands are commands that we use only for quick purposes that we don't want to save for later, such as checking the status of a server or copying a file to the server — tasks we just want to do quickly by typing only few commands.

In these situations, Ansible ad-hoc commands are really helpful.

For example, if you have an inventory file with two groups, one group called dbservers and the other group called webservers, like below, if you want to check the status of both the web servers, Server A and Server B, you can do that simply by typing:

ansible webservers -a "pwd"

Sample inventory configuration:

[dbservers]
db1.mercury.com
db2.venus.com

[webservers]
servera.earth.com
serverb.jupyter.com

It will try to execute the pwd statement in the bash of both servers and return the result. If both servers are live, it will display some success result.

How to write an Ansible ad-hoc command?

What you just read above is a simple example of an Ansible ad-hoc command. Now we will move forward and understand its anatomy.