Ansible ad hoc commands are one-liners designed to achieve a very specific task they are like quick snippets and your compact swiss army knife when you want to do a quick task across multiple machines.
To put simply, Ansible ad hoc commands are one-liner Linux shell commands and playbooks are like a shell script, a collective of many commands with logic.
Ansible ad hoc commands come in handy when you want to perform a quick task.
Task-01
1. Write an ansible ad hoc ping command to ping 3 servers from 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: specifies the path to the inventory file containing the servers we want to ping. /etc/ansible/hosts is by default path of inventory file. there is no need to write path in ansible ad hoc command. If your inventory file is at different location then you need to write the path of inventory file in ad hoc command.
server1:server2:server3: specifies the list of servers to ping, separated by colons.
-m ping: specifies that we want to use the ping module to ping the servers.
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 ansible command with the following options:
-m command: specifies that we want to use the command module to execute the uptime command on the remote servers.
-a uptime: specifies the arguments to pass to the command module, which in this case is simply the uptime 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 the arguments to pass to the command to be executed on the remote hosts. In this case, the free -m command is executed to show 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 module to use to execute the command on the remote hosts. In this case, the shell module is used to execute the command.
-a "cat /proc/meminfo|head -2": the -a option specifies the arguments to pass to the module to execute the command on the remote hosts. In this case, the cat /proc/meminfo|head -2 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 in 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 executing the command with the shell module. Replace command with the command you want to run as sudo.
- Use sudo command 'sudo apt-get install docker.io -y', so by using this ad hoc command you can install jenkins, nginx, apache, nodejs etc. on your 3 or more servers.
Check docker is install or not in all the 3 servers.
2. Use sudo command 'sudo git --version' which shows all 3 servers git version.
3. Use sudo command 'sudo python --version' which shows all 3 server python version.
8. To check the status of a specific service on all hosts in an inventory file
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 of the service to check and the desired state, which is started in this case. (Instead of using Docker, you have the option to use any other service, such as Apache, Nginx, Jenkins, and so on)
You can check the status of a specific service on single hosts or server1
ansible -i inventory_file server1 -m service -a 'name=docker state=started'
9. To 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 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
Check file is successfully copied to all the servers using 'sudo ls command'
10. Create a Directory with 755 permission using ansible ad hoc command
ansible all -m file -a "path=/home/ubuntu/ansible state=directory mode=0755" -b
-a "path=/home/ubuntu/ansible state=directory mode=0755": This is the argument to the -m option. It tells Ansible to create a directory at the path /home/ubuntu/ansible with the directory state (i.e., to create a directory 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 the superuser (i.e., with sudo).
11. 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 a file at the path /path-to-file with the touch state (i.e., 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 the superuser (i.e., with sudo).
Check file is created with given permissions using sudo ls -l command
Thank you for reading!! I hope you find this article helpful!!
Happy Learning!!
Sayali✨