Day 56 Task: Understanding Ad-hoc commands in Ansible

Day 56 Task: Understanding Ad-hoc commands in Ansible

ยท

3 min read

Ansible ad-hoc commands are a convenient way to perform simple, one-time tasks on remote servers using Ansible without the need for writing full playbooks. They are a valuable tool for system administrators and operators when quick actions are required.

๐Ÿ”ถ Task-01: write an ansible ad hoc ping command to ping 3 servers from the inventory file.

Prerequisites for this task please refer this Blog Day55

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

or
ansible -i /etc/ansible/hosts server1:server2:server3 -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

or
ansible -i /etc/ansible/hosts all -m command -a uptime

or 
 ansible -i /etc/ansible/hosts/ server1:server2:server3 -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 :)

    If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐Ÿš€ #devops #90daysofdevop #AWS#Ansible

ย