Day 56 Task: Understanding Ad-hoc commands in Ansible

Day 56 Task: Understanding Ad-hoc commands in Ansible

Ansible ad hoc commands are one-liners designed to achieve a very specific task they are like quick snippets and your compact Swiss army knife when you want to do a quick task across multiple machines.

To put simply, Ansible ad hoc commands are one-liner Linux shell commands and playbooks are like a shell script, a collective of many commands with logic.

Ansible ad hoc commands come in handy when you want to perform a quick task.

🔶 Task-01: write an ansible ad hoc ping command to ping 3 servers from the inventory file.

ansible -i  /path/to/inventory/file server_1:server_2:server_3 -m ping

The Ansible command is used to perform a ping operation on specific hosts from your inventory file. Here's a breakdown of the command:

  • -i /path/to/inventory/file: This flag specifies the path to the Ansible inventory file. The inventory file lists the hosts or nodes that Ansible will manage.

  • server_1:server_2:server_3: These are the host patterns or names you want to target with the Ansible command. In this case, you've specified server_1, server_2, and server_3. Ansible will perform the ping operation on these specific hosts.

  • -m ping: This flag specifies the Ansible module to use, which is the ping module in this case. The ping module is used to check if hosts are responsive and reachable.

So, when you run this Ansible command, it will ping the hosts server_1, server_2, and server_3 to check if they are reachable and responsive. You should see an output indicating whether each host was reachable (SUCCESS) or not (UNREACHABLE) based on the results of the ping operation.

Please ensure that your inventory file (/path/to/inventory/file) is correctly configured with the hostnames or IP addresses of server_1, server_2, and server_3, along with the necessary SSH connection details if required for accessing these hosts.


🔶 Task-02: Write an ansible ad hoc command to check uptime.

ansible -i /path/to/inventory/file all -m command -a uptime

The command is an Ansible command that performs the uptime command on all hosts defined in the inventory file located at /path/to/inventory/file. Here's a breakdown of the command:

  • -i /path/to/inventory/file: This flag specifies the path to the Ansible inventory file. The inventory file lists the hosts or nodes that Ansible will manage. In your case, you've provided the path to this file.

  • all: This is an Ansible pattern that refers to all hosts defined in the inventory file. It means that Ansible will execute the following command on all hosts.

  • -m command: This flag specifies the Ansible module to use, which in this case is the command module. The command module is used to run shell commands on remote hosts.

  • -a uptime: This flag specifies the argument to pass to the command module. In this case, it's the uptime command, which is a standard Unix/Linux command that shows the current system uptime.

So, when you run this Ansible command, Ansible will connect to all hosts listed in the inventory file and execute the uptime command on each of them. It will then display the output of the uptime command for each host, showing how long each host has been running.

  • You can refer to this blog to understand the different examples of ad-hoc commands and try out them, post the screenshots in a blog with an explanation.

Happy Learning :)

Stay in the loop with my latest insights and articles on cloud ☁️ and DevOps ♾️ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes #AWS

Did you find this article valuable?

Support CHANDRESH PATLE by becoming a sponsor. Any amount is appreciated!