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 Tasks like pinging all the hosts to check if they are running, copying a file, rebooting servers, 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.

Ansible Ad hoc Command Guide with Examples - 92Ansible Ad hoc Command Guide with Examples - 90Ansible Ad hoc Command Guide with Examples - 68Ansible Ad hoc Command Guide with Examples - 83Ansible Ad hoc Command Guide with Examples - 50Ansible Ad hoc Command Guide with Examples - 70Ansible Ad hoc Command Guide with Examples - 35Ansible Ad hoc Command Guide with Examples - 51Ansible Ad hoc Command Guide with Examples - 8Ansible Ad hoc Command Guide with Examples - 99Ansible Ad hoc Command Guide with Examples - 32Ansible Ad hoc Command Guide with Examples - 34Ansible Ad hoc Command Guide with Examples - 7Ansible Ad hoc Command Guide with Examples - 28Ansible Ad hoc Command Guide with Examples - 71Ansible Ad hoc Command Guide with Examples - 15Ansible Ad hoc Command Guide with Examples - 70Ansible Ad hoc Command Guide with Examples - 4Ansible Ad hoc Command Guide with Examples - 17Ansible Ad hoc Command Guide with Examples - 65Ansible Ad hoc Command Guide with Examples - 58Ansible Ad hoc Command Guide with Examples - 67Ansible Ad hoc Command Guide with Examples - 30