Understanding Ad-hoc commands in Ansible
#day68 of #90daysofdevops

Introduction:)
Welcome to the world of Ansible Ad-hoc commands! In this blog post, we'll delve into the syntax, structure, and practical usage of Ansible Ad-hoc commands. Whether you're a beginner exploring Ansible or an experienced user looking to enhance your skills, this guide is designed to provide a detailed understanding of Ad-hoc commands.
What is Ad-hoc commands?
Ad-hoc commands in Ansible are one-liners used for executing simple tasks on remote hosts without the need to write a playbook. They are helpful for quick and immediate tasks. Ad-hoc commands consist of a combination of Ansible command-line options, modules, and arguments. Here's a breakdown of the components:
ansible: The base command that initiates the execution of Ansible.
<host-pattern>: Specifies the target hosts or groups on which the ad-hoc command will be executed. It can be a single host, a group of hosts, or a pattern that matches multiple hosts.
-m <module>: Specifies the Ansible module to be used for the task. Modules are units of work that perform specific actions on the managed nodes. Examples of modules include
ping,shell,copy,apt, etc.-a "<arguments>": Specifies the arguments or parameters to be passed to the module. The arguments depend on the chosen module and the task you want to perform.
Ansible with "-m" tag
In Ansible, the -m option is used to specify the module to be executed. Modules in Ansible are discrete units of code that can be used to perform specific tasks on managed nodes. The -m option allows you to choose which module should be executed for a particular operation.
Here's a basic syntax of the ansible command with the -m option:
ansible <host-pattern> -m <module-name> -a <module-arguments>
<host-pattern>: Specifies the target hosts or groups.<module-name>: Specifies the Ansible module to be used.<module-arguments>: Specifies the arguments or parameters for the module.
For example, if you want to use the ping module to check the connectivity to your hosts, you would run:
ansible <host-pattern> -m ping
Here, <host-pattern> is replaced with the actual host or group of hosts you want to target.
Different modules perform different tasks, such as managing files, installing packages, restarting services, etc. The -m option allows you to choose the appropriate module for the task you want to accomplish. Always refer to the Ansible documentation for information on available modules and their usage.
Ansible with "-a" tag
In Ansible, the -a option is used to specify the arguments or parameters for an ad-hoc command.
Here's a basic syntax of an ad-hoc command with the -a option:
ansible <host-pattern> -a "<command>"
<host-pattern>: Specifies the target hosts or groups.<command>: Specifies the command or task to be executed on the hosts.
For example, if you want to use the shell module to run a simple command like ls on your hosts, you would run:
ansible <host-pattern> -m shell -a "ls"
Here, <host-pattern> is replaced with the actual host or group of hosts you want to target.
The -a option allows you to pass arguments to the module specified with the -m option. It enables you to customize the behavior of the module for the specific task you want to perform. Always ensure that the command is enclosed in double quotes when using the -a option.
Ansible Ad Hoc Command Examples
Certainly! Here are two Ansible ad-hoc commands:
Ping Command to Ping 2 Servers from Inventory File:
ansible server1:server2 -m ping # or ansible <server_group> -m ping
This command targets the servers
server1andserver2from the inventory file.The
-m pingoption specifies the use of thepingmodule to check connectivity.
Ad-hoc Command to Check Uptime on Servers:
ansible all -m command -a "uptime"
This command targets all servers from the inventory file using
all.The
-m commandoption specifies the use of thecommandmodule to run a shell command.The
-a "uptime"argument passes theuptimecommand to thecommandmodule, checking the system uptime.
Managing Files:
To copy a file from the local machine to remote servers:
ansible servers -m copy -a "src=/path/to/local/file.txt dest=/remote/path/"
This is server1 in which we see hello.txt is there

To delete a file on remote servers:
ansible servers -m file -a "path=/remote/path/file.txt state=absent"
From server1 file is removed

Installing Packages:
To install a package using the package manager (e.g., apt for Ubuntu):
ansible servers -m apt -a "name=package-name state=present"
Restarting Services:
To restart a service on remote servers:
ansible servers -m service -a "name=service-name state=restarted"Replace
service-namewith the actual name of the service.
For more you can go through this LINK
Connect with me:)
Thank you for diving into this blog with me! I trust you found the information both helpful and enlightening. To stay updated on the latest in DevOps 🚀, make sure to follow me. Remember, staying informed means staying ahead in the dynamic world of DevOps!
Feel free to connect with me on:
For more updates and engaging discussions on DevOps, let's connect! 🚀 #DevOpsCommunity




