Day 56: Understanding Ad-hoc commands in Ansible

Day 56: Understanding Ad-hoc commands in Ansible

Welcome back to the 90DaysOfDevOps challenge! Today, we're diving into the world of Ansible ad-hoc commands. Ad-hoc commands are powerful tools in Ansible that allow us to perform quick tasks without the need for writing a playbook. Let's explore what they are and how to use them effectively.

What is an Ansible Ad-hoc command?

An Ansible ad-hoc command is a one-liner command used to perform simple tasks on remote hosts without writing a playbook. These commands are useful for tasks that don't require a complex playbook structure or for quick troubleshooting.

Now, let's tackle some tasks using Ansible ad-hoc commands.

Task 1:

Ping 3 servers from the inventory file:

ansible all -i inventory_file -m ping

Replace inventory_file with the path to your inventory file containing the list of servers. This command will ping all servers listed in the inventory file.

Check uptime:

ansible all -i inventory_file -m command -a "uptime"

This command uses the command module to check the uptime of all servers listed in the inventory file.

Listing out all ad-hoc commands:

Here are some commonly used ad-hoc commands:

  1. Ping: To check connectivity to remote hosts.

     ansible all -m ping
    
  2. Command Execution: Execute commands on remote hosts.

     ansible all -a "command_to_execute"
    
  3. Check Uptime: Check uptime of remote hosts.

     ansible all -m command -a "uptime"
    
  4. Copy: Copy files to remote hosts.

     ansible all -m copy -a "src=file.txt dest=/path/to/destination/"
    
  5. Package Management: Install packages on remote hosts.

     ansible all -m apt -a "name=package state=installed" # for Debian based systems
     ansible all -m yum -a "name=package state=installed" # for Red Hat based systems
    
  6. Service Management: Start, stop, restart services on remote hosts.

     ansible all -m service -a "name=service_name state=started"
    

These are just a few examples of what you can do with Ansible ad-hoc commands. They offer a quick and efficient way to perform tasks on remote hosts without the need for writing lengthy playbooks.

That's it for today's learning on Ansible ad-hoc commands. Stay tuned for more exciting topics in the 90DaysOfDevOps challenge! Happy automating!