Understanding Ad-hoc Commands in Ansible: A Quick and Powerful Tool

Understanding Ad-hoc Commands in Ansible: A Quick and Powerful Tool

·

6 min read

introduction:

Ansible, a popular configuration management and automation tool, provides a versatile set of features for managing IT infrastructure. One of its powerful features is the ad hoc command that allows system administrators to perform quick and specific tasks on multiple servers. Command acts as a lightweight Swiss army knife when you need to move quickly without writing extensive playbooks. In this blog, we'll dive into the world of Ansible ad hoc commands, understand their role, and explore practical examples.

What are Ansible Ad-hoc Commands? Responsive custom commands are shortcuts designed to perform certain tasks on a remote server without requiring the full game. This command can be executed on a remote machine to directly execute the module. Unlike playbooks, custom commands are not written in YAML; instead, they are simple shell commands that are executed directly on the command line.

Task-01

1. Write an ansible ad hoc ping command to ping 3 servers from 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: Specify the path to the inventory file containing the server we want to deploy. /etc/ansible/hosts is the initial path to the inventory file. No need to type the path in the ad hoc command answered. If your inventory file is in a different location, you must write the path to the inventory file in a special command.

  • server1: server2: server3: specifies a list of servers separated by columns.

    -m ping: Specifies that we will use the ping module to open the server.

    In my case, I added the naming convention as Ansible-Server1 in the /etc/ansible/host file, so I used the command as shown below.

  • Ansible Ansible-Server1: ping Ansible-Server2

2. Write an ansible ad hoc command to check uptime

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

This command uses the command prompt with the following options:

-m command: specifies that we will use the command module to run runtime commands on the remote server.

-a runtime: Specifies the arguments to pass to the command module, in this case just the runtime command.

  • 3. Ansible ad hoc command to check the free memory or memory usage of hosts.

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

    -a "free -m": The -a option specifies arguments to pass to the command to be executed on the remote host. In this case, the -m command is freely executed to display the memory usage on each host.

4. ad-hoc command to get physical memory allocated to the host

ansible all -m shell -a "cat /proc/meminfo|head -2"
  • -m shell: The -m option specifies the shell used to execute commands on the remote host. In this case, the shell module is used to execute commands.

  • -a "cat /proc/meminfo | head -2": The -a option specifies the arguments to pass to the module to execute commands on the remote host. In this case, cat /proc/meminfo | The -2 head command is executed to display the first two lines of the /proc/meminfo file on each host.

  • 5. To check the disk space on all hosts in an inventory file

        ansible -i inventory_file all -m shell -a 'df -h'
    

    This command uses the df command to show the disk space usage on each host in the inventory file.

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

  • 6. To list all the running processes on a specific host in an inventory file

        ansible -i inventory_file specific_host -m command -a 'ps aux'
    

    This command uses the ps command to list all the running processes on the specific host inThis command uses the ps command to list all processes running on specific host in an inventory file. The -m command option is used to execute commands on a remote host using the command module. the inventory file. The -m command option is used to execute the command on the remote hosts using the command module.

7. To run a shell command with sudo on all hosts in an inventory file

  •         ansible -i inventory_file all -b -m shell -a 'sudo-command'
    

    This command uses the -b option to become the sudo user before running the command using the shell module. Replace command with the command you want to run as sudo.

  • Use the command "sudo apt-get install docker.io -y" so you can install 2 or more servers like Jenkins, nginx, Apache, nodejs etc using this special command. you can build

  •         ansible -i inventory_file all -m service -a 'name=docker state=started'
    

    This command uses the service module to check the status of the Docker service on all hosts in the inventory file. The -a 'name=docker state=started' option specifies the name and desired state of the service to check, which in this case is started. (Instead of using Docker, you have the option of using any other service, like Apache, Nginx, Jenkins, etc.)

  •     ansible -i inventory_file server1 -m service -a 'name=docker state=started'
    

    Copy a file to all hosts in an inventory file

        ansible -i inventory_file all -m copy -a 'src=/local/path/to/file dest=/remote/path/to/file mode=0644'
    

    This command uses the copy module to copy the 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 in any location, here create a text file in /home/ubuntu location with name testansible.txt .Check file is successfully copied to all the servers using the 'sudo ls command'

  •         ansible all -m file -a "path=/home/ubuntu/ansible state=directory mode=0755" -b
    

Create a file with 755 permission using ansible ad hoc commands

  •         ansible all -m file -a "path=/path/to/file state=touch mode=0755"
    
    • -a "path=/path/to/file state=touch mode=0755": This is the argument to the -m option. It tells Ansible to create the file at path/path-to-file with the touch state (that is, 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.

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

Conclusion:

Ad commands offer great flexibility and are ideal for one-on-one troubleshooting or troubleshooting. However, for more complex and repetitive tasks, Ansible playbooks are recommended because they provide better organization, reusability, and continuity. Combine the power of custom commands and playbooks to unlock the full potential of Ansible and streamline infrastructure management. Happy automation!

480+ Devops Team Stock Photos, Pictures & Royalty-Free ...

Follow for more:

https://www.linkedin.com/in/subhodey/

Did you find this article valuable?

Support Subho Dey by becoming a sponsor. Any amount is appreciated!