Understanding Ad-hoc commands in Ansible

Understanding Ad-hoc commands in Ansible

Ansible ad-hoc commands serve as concise, one-line directives crafted to accomplish precise tasks swiftly across multiple machines. They function akin to quick snippets, acting as a compact Swiss Army knife for rapid tasks.

In essence, Ansible ad hoc commands resemble one-liner Linux shell commands, while playbooks act as comprehensive scripts, combining multiple commands with logic.

These ad hoc commands prove invaluable for swiftly executing immediate tasks.

Ansible Ad-Hoc Commands syntax:

Refer the following diagram to understand how the ansible ad hoc commands are framed.

ansible ad hoc commands

To run an ad hoc command, the command must be framed or have the following syntax.

ansible <host-pattern> [options]

for example. the command should be written as follows.

ansible appserverhostgroup -m <modulename> -a <arguments to the module>

A single ansible ad hoc command can have multiple options. -m and -a are one amongst them and widely used.

Lets understand concept with help of task.

Task-01

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

  • Write an ansible ad hoc command to check uptime

Solution:

Step 1: lets check /etc/ansible/host file with all server configuration

server details can see in host file.

Step 2: lets ping to server from inventory file. so our syntax will become like this.

ansible -i /etc/ansible/hosts Ansible-slave1:Ansible-slave2 -m ping

This Ansible ad hoc command uses the -i option to specify the inventory file path containing the servers to ping. The list of servers to ping includes Ansible-slave1 and Ansible-salve2. The -m ping option indicates the use of the ping module for the ping operation.

Step 2: Ansible ad hoc command to check uptime. Same way our adhoc command syntax become

ansible -i /etc/ansible/hosts all -m command -a uptime

This command employs the command module (-m command) to execute the uptime command on all servers specified in the inventory file. The -a uptime option provides the arguments for the command module.

Step 3: Ansible ad hoc command to check free memory

Step 4:Check disk space on all hosts

Step 5: List all running processes on a specific host

In above command utilizes the ps command to list all running processes on the specific host specified in the inventory file. The -m command option executes the command using the command module.

Step 6: Run a shell command with sudo on all hosts

Step 7: Install package on on all server with ad-hoc command.(Will install here docker)

We are able to see installation done for both server with single command

Lets verify the same.

Step 8: Copy a file to all hosts

The copy module is used to copy a file from the local machine to all hosts in the inventory file. The -a 'src=/local/path/to/file dest=/remote/path/to/file mode=0644' option specifies source and destination paths along with the desired file permissions.

Verify file in remote server.

Step 9: Create a directory and file with 755 permissions

Using the file module, command creates a directory at the specified path (/home/ubuntu/ansible) with the directory state and permissions (mode=0755). The -b option runs the command as the superuser (sudo).

Lets create directory first then create new file into directory.

Create file with same way instead state=directory we choose touch.

Verify file and folders details by connecting remote server.

We can see files and folder on remote server.

I hope you enjoy the session. Keep readings....!

Thanks,

Kishor Chavan