Day 56 of 90 Days of DevOps Challenge:

Tushar PantTushar Pant
3 min read

For Day 56 of the 90 Days of DevOps challenge, the focus is on Ansible ad-hoc commands. These are essentially one-liner commands designed for quick tasks across multiple machines, similar to how you would use quick shell commands without creating a full script.


What are Ad-hoc Commands in Ansible?

In Ansible, ad-hoc commands allow you to execute a specific command on your managed nodes without the need to write a complete playbook. These commands are especially useful when you want to perform quick actions or tests, such as restarting services, checking uptime, or pinging servers.


Task Breakdown for Day 56

Task-01: Ping 3 Servers Using Ansible Ad-hoc Command

To ping 3 servers from the Ansible inventory, you'll first need to ensure that your inventory file (/etc/ansible/hosts) is properly configured with the details of your servers. Assuming you have already set up an Ansible inventory and your 3 servers (nodes) are listed, you can use the following ad-hoc command to ping them:

ansible all -m ping
  • Explanation:

    • ansible: The command to run Ansible.

    • all: This specifies that the command should run on all hosts defined in the inventory file. You can replace this with a specific group or host if you want to limit the ping.

    • -m ping: The -m flag is used to specify the module to use. In this case, the ping module is used to check the connection to the servers.

Task-02: Check Uptime Using Ansible Ad-hoc Command

To check the uptime of the servers, you can use the command module in an Ansible ad-hoc command:

ansible all -m command -a "uptime"
  • Explanation:

    • all: This again specifies that the command should run on all hosts. You can replace it with specific host names or groups.

    • -m command: This specifies the command module, which allows you to run any command on the remote servers.

    • -a "uptime": The -a flag allows you to pass arguments to the module. In this case, you are running the uptime command on the servers.

Additional Ad-hoc Command Examples

Here are some other useful ad-hoc commands you can try for quick tasks:

  1. Reboot all servers:

     ansible all -m reboot
    
  2. Check disk space:

     ansible all -m command -a "df -h"
    
  3. Get system info:

     ansible all -m setup
    

Make sure you test these commands and try to run them on your configured EC2 instances. Post the screenshots of your results in your blog along with explanations of each command and its output. This will help you solidify your understanding of how Ansible ad-hoc commands work in real-time scenarios.

0
Subscribe to my newsletter

Read articles from Tushar Pant directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tushar Pant
Tushar Pant