We earn commission when you buy through affiliate links.

This does not influence our reviews or recommendations.Learn more.

This article is all about Ansible Ad-hoc Command.

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?

However, for more complex tasks like software installations, its usually better to use a playbook.

Then, youll be able to see the output from each server in your console.

Its super easy and efficient!

Why use Ansible ad-hoc commands?

Also, theyre 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.

Moreover, ad-hoc commands are also flexible!

Here is a list of fundamental Ansible Ad-hoc commands which you must know.

Here-mis the option for a module.

The command mentioned below runs setup module on a group of hosts Clientpresent in the inventory file /etc/ansible/hosts.

The command below is used to prompt SSH password authentication.

you gotta addask-passoption 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–becomegives the root privileges and-Koption asks for the password.

This ad-hoc command is used to reboot the system with -f option for defining the number of forks.

launch 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.

look to see if the file got downloaded or not at the destination mentioned in the command.

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.

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.

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.