One of the great things about using Ansible as your configuration management tool of choice is its flexibility. You aren't tied down to any particular way of doing things, and with ad-hoc commands, you don't have to create an elaborate set of tasks just to perform simple operations on your nodes. Ad-hoc commands in Ansible allow you to execute simple tasks at the command line against one or all of your hosts.
An ad-hoc command consists of two parameters; the host group that defines on what machines to run the task against and the Ansible module to run. These parameters are passed to the ansible binary for invocation. If you'd like to run the ping module against all machines that are in the servers host group, the following would make that happen:
ansible servers -m ping
In fact, we don't even have to use an Ansible module at all. We can even specify the command to run on every machine right at the CLI itself. For example, if we need to reboot all of the hosts inside of that servers host group, we could initiate a reboot sequence by calling the reboot binary directly from Ansible.
ansible servers -a "/sbin/reboot"
Notice this time I use the -a parameter. This indicates that I'll be passing in a direct command and not a module. For a module, I'd use the -m parameter. Although not to confuse you, we can also use the -a parameter with the -m parameter as a way to pass different arguments to a module. For example, with the yum package, we need a way to specify the name of the package we're looking for, and it's state. We'd do that by using both parameters.
ansible servers -m yum -a "name=foo state=present"
Ad-hoc commands in Ansible also allow you to run commands in parallel. By using the -f switch and specifying how many parallel forks to run, we could reboot all of those hosts ten at a time.
ansible servers -a "/sbin/reboot" -f 10
Perhaps you're logged onto the console as a different user you'd like to execute a command against. You'll need to somehow tell Ansible to invoke that command as an alternative user without having to log off of the console and log back on as that user. Running ad-hoc commands as an alternative user is possible by using the -u, --become and --ask-become-pass parameters. These two options will force the command to run as that user and interactively prompt you for that user's password.
ansible servers -a "/sbin/reboot" -u differentuserhere --become --ask-become-pass
As our final example in working with ad-hoc commands, let's add a time-limited background operation. This comes in handy for those long-running commands that you'd rather not tie up your console so you can do other things. With Ansible, we can invoke the task in the background and set a timeout on it to ensure it doesn't run forever. Perhaps I'd like to execute a script on all of my hosts, kick it to the background and make sure it doesn't run for longer than an hour. This is done through the use of the -B and -P parameters.
ansible all -a "/usr/bin/somescript" -B 3600 -P 0
Ad-hoc commands are a powerful yet simple feature of Ansible. For a full list of what's possible and some great examples, refer to the Ansible documentation.