There are multiple tasks in Ansible where you don’t need to write a separate Ansible playbook for it; you can just run an ansible ad-hoc command for that task. These are one-liner command to perform a single task on the target host. These commands are present in /usr/bin/ansible.
What is Ansible Ad-hoc Command?
An Ansible ad-hoc command is a useful tool that allows you to execute a single task on a group of servers without needing to create a full playbook. These commands are great for performing simple tasks like checking the status of a service or quickly running a command. However, for more complex tasks like software installations, it’s usually better to use a playbook. A single line of code containing the relevant information, such as the desired module name and any parameters that must be supplied to it, is entered to run an ad-hoc command. The syntax for an Ansible ad-hoc command is as follows: Here:
For example, if you wanted to check the disk space on a group of servers, you could use the df -h command with the shell module, like this: The shell module would run the df -h command on all servers in the web_servers group. Then, you’ll be able to see the output from each server in your console. It’s super easy and efficient!
Why use Ansible ad-hoc commands?
Apart from being quick and easy to use, ad-hoc commands are perfect for simple, one-time tasks that don’t require a lot of complexity. Also, they’re a great way to try out new modules or commands before adding them to a playbook. This can help you avoid mistakes and save time in the long run. If you’re troubleshooting an issue on a server, you can use them to check the status of a service, view log files, or perform other diagnostic tasks. Moreover, ad-hoc commands are also flexible! You can use them for all kinds of tasks, like installing packages, copying files, or running commands on multiple servers at once. If you want to make server management easier and more efficient, Ansible ad-hoc commands are the way to go! Here are some of the ways you can use them:
Rebooting servers Managing files Managing packages Managing users and groups Managing services Gathering facts Patterns and ad-hoc commands
Tasks like pinging all the hosts to check if they are running, copying a file, rebooting servers, and installing a package can be easily done through Ansible Ad-hoc Commands. Here is a list of fundamental Ansible Ad-hoc commands which you must know.
Basic Commands
The ad-hoc command below runs a ping module on all the hosts in the inventory file. Here -m is the option for a module. The command mentioned below runs setup module on a group of hosts – Client present in the inventory file ‘/etc/ansible/hosts’. The command below is used to prompt SSH password authentication. You need to add –ask-pass option at the end of the command. After running the command, it will ask you to enter the SSH password. The command below gives you the feature of running ad-hoc commands as a non-root user with root privileges. The option –become gives the root privileges and -K option asks for the password. This ad-hoc command is used to reboot the system with -f option for defining the number of forks.
File Transfer
The ansible ad-hoc command below is used to copy a file from a source to a destination for a group of hosts (Client) defined in the inventory file. After you enter the password, the output with “change” parameter will be “true”, which means the file has been copied to the destination. Run the below command to verify if the copy module worked correctly or not. The copied file should come at the destination mentioned in the previous command. I am creating a new directory to run fetch module n next ad-hoc command. The ansible ad-hoc command below is used to download a file from a host defined in the command. In this command, we are downloading a file using fetch module from node1 server to a local destination on ansible node. Check if the file got downloaded or not at the destination mentioned in the command.
Manage Packages
The command mentioned below installs nginx on a group of hosts (Client) mentioned using an apt module. The command mentioned below removes nginx on a group of hosts (Client) using apt module and purges all the related configurations.
Manage Services
The ansible ad-hoc command below runs service module to start nginx on the host. The state value should be started. The below runs service module to stop nginx on the host. The state value changes to stopped.
Checking the System
The ansible ad-hoc command mentioned below runs a shell module to check the disk available at root partitions. This command mentioned below runs a shell module to check the free memory (RAM) on the host. This command checks the uptime of each server running.
Gathering Facts
The ansible ad-hoc command below will give you all the ad-hoc information of your system, including all the variables present in the system. So, that was all about Ansible Ad-hoc Commands. Go ahead and try these commands on your ansible setup. Use these commands to run ansible modules without writing any Ansible playbook to perform those tasks. Drop a comment if you face any issue in running these commands.