Introduction to Ansible Ad-hoc Commands
Ansible ad hoc commands are powerful tools for executing simple tasks without writing a full playbook. They are particularly useful for quick, one-time operations such as checking the status of services, managing users, or gathering system information across multiple machines. Think of them as the command-line equivalent of Ansible playbooks, providing a quick and efficient way to perform tasks.
Task-01 : Writing an Ansible Ad-hoc Ping Command
Objective: Ping three servers listed in the inventory file to ensure they are reachable.
Inventory File Configuration Before running the ad hoc commands, ensure your inventory file (
/etc/ansible/hosts
) is correctly set up. Here’s an example configuration:[servers] server1 ansible_host=192.168.1.1 server2 ansible_host=192.168.1.2 server3 ansible_host=192.168.1.3
Ping Command To ping the three servers listed in the inventory, use the following ad hoc command:
ansible servers -m ping
This command uses the
ping
module to check connectivity. It will return a success message if the servers are reachable.Explanation:
ansible
: The command to run Ansible.servers
: The group of servers defined in the inventory file.-m ping
: Specifies the module to use (in this case, theping
module).
Expected Output The output should indicate whether each server is reachable:
server1 | SUCCESS => { "changed": false, "ping": "pong" } server2 | SUCCESS => { "changed": false, "ping": "pong" } server3 | SUCCESS => { "changed": false, "ping": "pong" }
Task-02 : Writing an Ansible Ad-hoc Command to Check Uptime
Objective: Check the uptime of all servers in the inventory to see how long each system has been running.
Uptime Command To check the uptime of the servers, use the following ad hoc command:
ansible servers -a "uptime"
This command runs the
uptime
command on all servers, displaying how long each system has been running.Explanation:
ansible
: The command to run Ansible.servers
: The group of servers defined in the inventory file.-a "uptime"
: Specifies the command to run on the servers.
Expected Output The output will show the uptime for each server:
server1 | CHANGED | rc=0 >> 14:10:17 up 20 days, 3:12, 2 users, load average: 0.00, 0.01, 0.05 server2 | CHANGED | rc=0 >> 14:10:17 up 15 days, 4:23, 1 user, load average: 0.02, 0.01, 0.00 server3 | CHANGED | rc=0 >> 14:10:17 up 10 days, 6:45, 3 users, load average: 0.01, 0.05, 0.02
Additional Useful Ad-hoc Commands
1. Check Disk Space
ansible servers -a "df -h"
Explanation:
df -h
: Displays disk space usage in a human-readable format.
2. Restart a Service
ansible servers -a "systemctl restart apache2"
Explanation:
systemctl restart apache2
: Restarts the Apache2 service on all servers.
3. Create a Directory
ansible servers -a "mkdir /tmp/test_directory"
Explanation:
mkdir /tmp/test_directory
: Creates a directory namedtest_directory
in the/tmp
directory on all servers.
4. Copy a File
ansible servers -m copy -a "src=/path/to/local/file dest=/path/to/remote/destination"
Explanation:
copy
: The Ansible module to copy files.src=/path/to/local/file
: Path to the local file to copy.dest=/path/to/remote/destination
: Destination path on the remote servers.