Day 56: Understanding Ad-hoc commands in Ansible

Day 56: 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 handy when you want to perform a quick task.

What is Ad hoc commands..??

Ad hoc commands are one-off commands that are executed on the command line of an Ansible control node, without the need for a playbook or any additional configuration. These commands are used to quickly perform tasks on one or more managed nodes, such as checking the status of a service or installing a package.

Ad hoc commands are written in a simple, one-line format that includes the name of the module to be executed, any necessary parameters or options, and the name or IP address of the managed nodes to be targeted. Ad hoc commands are a powerful feature of Ansible that allow for quick and efficient management of IT infrastructure.

When and Why to Use Them Over Playbooks

While playbooks are great for structured, repeatable tasks, there are instances where you might need to perform an action only once or infrequently. In such scenarios, writing an entire playbook might be overkill. That's where ansible ad hoc commands come into play. These commands are perfect for tasks like quickly restarting a service, creating a user, or fetching system information from a set of servers.

Structure of Ansible Ad Hoc Commands

One of the strengths of ansible ad hoc commands is their simplicity, but understanding their structure is crucial for effective usage. Delving into the architecture of these commands can help users maximize their utility.

Syntax:

ansible [host-pattern] -m [module] -a "[module arguments]"
  • host-pattern determines which hosts from your inventory the command will target.

  • -m specifies the module you'd like to use.

  • -a allows you to define arguments for the chosen module.

    Ansible ad-hoc Commands:

    Here are some examples of commonly used Ansible ad-hoc commands:

    Monitoring Memory Usage

    Keeping an eye on memory usage is essential for server health. You can use Ansible to check memory usage with the following command

       ansible -i /path/to/inventory/file all -a "free -m"
    

    Here, the free -m command is executed on all servers to display memory usage statistics.

    Retrieving Physical Memory Information

    To fetch information about the physical memory allocated to your servers, you can use this command:

      ansible all -m shell -a "cat /proc/meminfo|head -2" -i /path/to/inventory/file --key-file=~<private_key_path>
    

    This command uses the shell module to execute a command that displays the first two lines of the /proc/meminfo file on each server.

    Checking Disk Space

    Checking disk space is vital for maintaining server storage. Ansible allows you to check disk space on all hosts in your inventory:

      ansible all -m shell -a 'df -h' -i /path/to/inventory/file --key-file=~<private_key_path>
    

    The df -h command is executed on all servers to show disk space usage.

    The -m shell option is used to execute the command on the remote hosts using the shell.

    No alt text provided for this image

    Listing Running Processes

    To see a list of all running processes on a specific host in your inventory file, you can use this command:

      ansible specific_host -m command -a 'ps aux' -i /path/to/inventory/file --key-file=~<private_key_path>
    

    This command employs the command module to execute the ps aux command on the specified host.

    No alt text provided for this image

    Executing Commands with Sudo

    Sometimes, you need elevated permissions to perform certain tasks. Ansible allows you to run commands with sudo. For example:

      ansible all -b -m shell -a 'sudo-command' -i /path/to/inventory/file --key-file=~<private_key_path>
    

    Replace 'sudo-command' with your desired command. This command becomes especially useful for tasks like installing software, checking versions, or managing system services.

    • To check if Docker is installed on all three servers:
    ansible all -b -m shell -a 'sudo apt-get install docker.io -y' -i /path/to/inventory/file --key-file=~<private_key_path>
  • To check the Docker version on all three servers:
    ansible all -b -m shell -a 'sudo docker --version' -i /path/to/inventory/file --key-file=~<private_key_path>

Service Status Check

To verify the status of a specific service on all hosts in your inventory, use the service module:

    ansible all -m service -a 'name=docker state=started' -i /path/to/inventory/file --key-file=~<private_key_path>

This command checks if the Docker service is started on all servers. You can replace "docker" with the name of any other service you want to monitor, such as Apache, Nginx, or Jenkins.

Copying Files

Copying files to multiple hosts can be accomplished with Ansible. Here's how you can copy a file to all hosts in your inventory:

    ansible all -m copy -a 'src=/local/path/to/file dest=/remote/path/to/file mode=0644' -i /path/to/inventory/file --key-file=~<private_key_path>

This command uses the copy module to copy a file from the local machine to all hosts in your inventory. The mode=0644 argument specifies the desired file permissions.

Creating Directories

If you need to create directories on your servers, Ansible can help with that too. For example, to create a directory with 755 permissions:

    ansible all -m file -a "path=/home/ubuntu/ansible state=directory mode=0755" -b -i /path/to/inventory/file --key-file=~<private_key_path>

This command uses the file module to create a directory at the specified path with the specified permissions.

Creating Files

Creating files with specific permissions is another task you can tackle with Ansible. To create a file with 755 permissions:

    ansible all -m file -a "path=/path/to/file state=touch mode=0755" -i /path/to/inventory/file --key-file=~<private_key_path>
  • -a "path=/path/to/file state=touch mode=0755": This is the argument to the -m option. It tells Ansible to create a file at the path /path-to-file with the touch state (i.e., to create the file if it doesn't exist). The mode argument sets the file permissions to 0755, which means that the owner has read, write, and execute permissions, and everyone else has read and execute permissions.

    No alt text provided for this image

  • -b: This tells Ansible to run the command as the superuser (i.e., with sudo).

Task-01

  • write an ansible ad hoc ping command to ping 3 servers from inventory file

    1. we have created 1 master server and 3 node servers and made a connection between them through public keys and integrated in the inventory file.

ansible -i /path/to/inventory/file server1:server2:server3 -m ping
  • This command uses the ansible command with the following options:

    • -i /path/to/inventory/file: specifies the path to the inventory file containing the servers we want to ping. /etc/ansible/hosts is by default path of inventory file. there is no need to write path in ansible ad hoc command. If your inventory file is at different location then you need to write the path of inventory file in ad hoc command.

    • server1:server2:server3: specifies the list of servers to ping, separated by colons.

    • -m ping: specifies that we want to use the ping module to ping the servers.

  • Write an ansible ad hoc command to check uptime

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

    This command uses the ansible command with the following options:

    • -m command: specifies that we want to use the command module to execute the uptime command on the remote servers.

    • -a uptime: specifies the arguments to pass to the command module, which in this case is simply the uptime command.

      The output indicates the total time for which all the 3 node servers are up and running.

Happy Learning😊😊!!

Thank you........