Understanding Ad-hoc commands in Ansible

Understanding Ad-hoc commands in Ansible

#90 Days of DevOps Challenge - Day 56

In Ansible, ad-hoc commands are used to execute simple tasks or run commands on remote systems without the need to create and run a playbook. Ad-hoc commands are useful for performing quick tasks or one-time operations. Here's an example of how to use ad-hoc commands in Ansible:

ansible <target> -m <module> -a "<arguments>"

Let's break down the components of the ad-hoc command:

  • <target>: Specifies the target hosts or groups of hosts where the command should be executed. You can specify hosts using their inventory name, IP address, or other patterns defined in your Ansible inventory.

  • <module>: Specifies the Ansible module to be used for the task you want to perform. Modules are Ansible's units of reusable automation, providing functionality for various operations like executing commands, managing packages, manipulating files, etc. Examples of modules include command, shell, copy, apt, service, etc.

  • <arguments>: Contains the specific arguments and options required by the chosen module. These arguments vary depending on the module being used. Refer to the Ansible documentation for each module's specific options.

Write an ansible ad hoc ping command to ping 3 servers from the inventory file.

ansible all -m ping -i <path_to_inventory_file>
Ex:- ansible -m ping -i /etc/ansible/hosts server1:server2:server3

In this command:

    • -m ping specifies the use of the ping module to execute the ping operation on the specified servers.

      • -i /etc/ansible/hosts indicates the inventory file to be used, which is located at /etc/ansible/hosts. You can replace this path with the actual path to your inventory file if it is different.

      • server1:server2:server3 is the list of target hosts separated by colons. It targets the three servers you want to ping.

Running this command will execute the ping module on the three specified servers from the /etc/ansible/hosts inventory file and provide you with the ping results.

Write an ansible ad hoc command to check uptime.

ansible all -a uptime -i <path_to_inventory_file>
Ex:- ansible all -a uptime -i /etc/ansible/hosts

Replace <path_to_inventory_file> with the actual path to your inventory file.

In this command:

  • all is the target, indicating that the uptime command should be executed on all hosts defined in the inventory file.

  • -a "uptime" specifies the argument to be passed to the command module, which is the uptime command in this case.

  • -i <path_to_inventory_file> indicates the inventory file to be used. Replace <path_to_inventory_file> with the actual path to your inventory file.

Running this command will execute the uptime command on all servers defined in the inventory file and display the uptime information for each server.

Write an ansible ad hoc command to check the free memory or memory usage of hosts using the ansible ad hoc command

ansible all -i <path_to_inventory_file> -m command -a "free -m"
Ex:- ansible all -i /etc/ansible/hosts -m command -a "free -m"

In this command:

  • all is the target, indicating that the command module should be executed on all hosts defined in the inventory file.

  • -i <path_to_inventory_file> specifies the inventory file to be used. Replace <path_to_inventory_file> with the actual path to your inventory file.

  • -m command indicates the use of the command module.

  • -a "free -m" specifies the argument to be passed to the command module, which is the free -m command in this case.

Running this command will execute the free -m command on all servers defined in the inventory file and display the memory usage information for each server.

Write an ansible Ad hoc command to get physical memory allocated to the host

ansible all -m shell -a "cat /proc/meminfo|head -2"

In this command:

  • all is the target, indicating that the shell module should be executed on all hosts defined in the inventory file.

  • -m shell specifies the use of the shell module.

  • -a "cat /proc/meminfo | head -2" specifies the argument to be passed to the shell module. It executes the cat /proc/meminfo | head -2 command, which reads the first two lines from the /proc/meminfo file.

  • -i <path_to_inventory_file> indicates the inventory file to be used. Replace <path_to_inventory_file> with the actual path to your inventory file.

Running this command will execute the cat /proc/meminfo | head -2 command on all servers defined in the inventory file and display the first two lines of the /proc/meminfo file, which contain memory-related information, for each server.

Write an ansible Ad hoc command to check the disk space on all hosts in an inventory file

ansible -i <path_to_inventory_file> all -m shell -a 'df -h'
Ex:- ansible -i /etc/ansible/hosts all -m shell -a 'df -h'
  • ansible: The Ansible command-line tool.

  • -i <path_to_inventory_file>: Specifies the path to the inventory file. The inventory file contains a list of hosts that Ansible will manage.

  • all: This is the inventory pattern that targets all hosts in the inventory.

  • -m shell: Specifies the Ansible module to use, in this case, the "shell" module, which allows executing shell commands on remote hosts.

  • -a 'df -h': The -a flag indicates the arguments to pass to the module. In this case, it specifies the shell command 'df -h', which will display disk usage information in a human-readable format.

When you run this command, Ansible will connect to each host specified in the inventory file and execute the df -h command on each host. The output will be displayed in your terminal or command prompt window.

Write an ansible Ad hoc command to Create a Directory with 755 permission

ansible all -m file -a "path=/home/ubuntu/ansible state=directory mode=0755" -b
  • ansible: The Ansible command-line tool.

  • all: This is the inventory pattern that targets all hosts in the inventory.

  • -m file: Specifies the Ansible module to use, in this case, the "file" module, which allows managing files and directories on remote hosts.

  • -a "path=/home/ubuntu/ansible state=directory mode=0755": The -a flag indicates the arguments to pass to the module. Here, it specifies the path, state, and mode of the directory to create.

    • path=/home/ubuntu/ansible: Specifies the path of the directory to be created.

    • state=directory: Specifies that the Ansible module should ensure the directory exists. If the directory doesn't exist, it will be created.

    • mode=0755: Sets the permissions of the directory to 0755, which allows read, write, and execute permissions for the owner and read and execute permissions for the group and others.

  • -b: The -b flag indicates that Ansible should become a privileged user, typically using sudo, to perform the task. This is necessary when creating directories in system locations that require administrative privileges.

When you run this command, Ansible will connect to each host specified in the inventory file and create the directory /home/ubuntu/ansible with the specified permissions.

Write an ansible Ad hoc command to Create a file with 755 permission

ansible all -m file -a "path=/home/ubuntu/testing.txt state=touch mode=0755"

Write an ansible Ad hoc command to run a shell command with sudo on all hosts

ansible all -b -m shell -a 'sudo apt-get update && sudo apt-get install docker.io -y'

Write an ansible Ad hoc command to check the version on all the servers

ansible all -b -m shell -a 'sudo docker --version'

Write an ansible Ad hoc command to copy a file to all hosts in an inventory file

ansible all -m copy -a 'src=/local/path/to/file dest=/remote/path/to/file mode=0644'
Ex:- ansible all -m copy -a 'src=/home/ubuntu/demo.txt dest=/home/ubuntu mode=0644'

This command uses the copy module 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 the source and destination paths for the file, as well as the desired file permissions.

first create a simple text file at any location, here create a text file at /home/ubuntu location with name demo.txt

Write an ansible Ad hoc command to list all the running processes on a specific host in an inventory file

ansible all -m command -a 'ps aux'

Devops#devops,#90daysofDevOps

Thank you for reading!! I hope you find this article helpful!!

if any queries or corrections to be done to this blog please let me know.

Happy Learning!!

Saikat Mukherjee

Printable, customizable thank you card templates | Canva

Did you find this article valuable?

Support Saikat Mukherjee by becoming a sponsor. Any amount is appreciated!