Day 56: Ansible Ad Hoc Commands: Your Quick Guide to Automation🚀
Day#56 Of 90 Days Of DevOps Challenge
🌈Introduction
Ansible, a powerful open-source automation tool, offers a range of capabilities to simplify the management and configuration of remote servers.
One of its handy features is Ad Hoc commands, designed for specific, one-off tasks.
In this blog post, we'll explore essential Ansible Ad Hoc command examples to enhance your automation journey.
📃Prerequisites
Before diving into Ansible Ad Hoc commands, ensure you have the following prerequisites in place:
Ansible Installation: Ansible should be installed on your control machine. Remember that Ansible is agentless, meaning it doesn't require installation on remote servers.
Remote Virtual Machines: You'll need remote virtual machines for testing purposes. Vagrant can help you create and manage these VMs. Refer to this article to get started.
SSH Connectivity: Make sure that both your control machine and VMs have SSH connectivity.
SSH Key-Based Authentication: For efficiency, it's recommended to enable SSH key-based authentication. Learn how to set it up in this article.
Now that you have the prerequisites in place, let's delve into Ansible Ad Hoc commands.
📚Understanding Ansible Ad Hoc Commands
Ad hoc, derived from Latin, means "something done for a precise and particular purpose." Ansible Ad Hoc commands are designed for precisely that—specific tasks. These commands are ideal for scenarios like:
Validating the uptime of multiple remote servers.
Retrieving disk space information from remote hosts.
Pinging servers to check if they're alive and responsive.
Simultaneously shutting down multiple remote hosts.
In this article, we'll provide real-world examples of Ansible Ad Hoc commands to help kickstart your automation journey.
1. Pinging Servers
Before diving into any action, it's crucial to ensure that your servers are reachable. Use the following Ansible ad hoc command to ping three servers from your inventory file:
ansible server1:server2 -m ping -i /path/to/inventory/file --key-file=~<private_key_path>
This command utilizes the ping
module to check the availability of the specified servers.
2. Checking Uptime
To get insights into server uptime, use the uptime
command through Ansible ad hoc:
ansible all -m command -a uptime -i /path/to/inventory/file --key-file=~<private_key_path>
This command utilizes the command
module to execute the uptime
command on all servers in your inventory.
3. 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 all -a "free -m" -i /path/to/inventory/file --key-file=~<private_key_path>
Here, the free -m
command is executed on all servers to display memory usage statistics.
4. 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.
5. 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.
6. 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.
7. 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>
- To check the git version on all three servers:
ansible all -b -m shell -a 'sudo git --version' -i /path/to/inventory/file --key-file=~<private_key_path>
8. 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.
For checking the status on a single server (e.g., server1):
ansible server1 -m service -a 'name=docker state=started' -i /path/to/inventory/file --key-file=~<private_key_path>
9. 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.
Before using this command, ensure you have a file named demo.txt
at the specified source path.
To check if the file is successfully copied to all servers, you can use:
ansible all -b -m shell -a 'sudo ls /remote/path/to/file' -i /path/to/inventory/file --key-file=~<private_key_path>
10. 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.
11. 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>
This command utilizes the file
module to create a file with the specified permissions.
To confirm that the file was created with the given permissions, use the following command:
ansible all -b -m shell -a 'sudo ls -l /path/to/file' -i /path/to/inventory/file --key-file=~<private_key_path>
👋Conclusion
Ansible Ad Hoc commands are powerful tools for performing quick, targeted tasks on remote hosts.
They streamline repetitive tasks, enhance efficiency, and enable effective server management.
With these examples, you have a solid foundation for using Ansible Ad Hoc commands in your automation projects.
Explore, experiment, and leverage Ansible to its full potential for your IT operations. Happy automating!
Happy learning & may your cloud journey be filled with exciting discoveries!🎊
Thank you for joining us on this exciting Day 56 of the 90 Days of DevOps challenge. I hope you found the information helpful and insightful.💥
So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on :
Hashnode: vishaltoyou.hashnode.dev