Hey Learners! Welcome back. In the previous challenge, we successfully connected our nodes with Ansible Master to manage. Moving forward we'll understand the concept of Ad-hoc commands and perform some operations on nodes directly from Ansible Master. Let's start...
Ad-hoc commands in Ansible
Ansible ad-hoc commands are one-liners, designed to achieve a very specific task, making them like quick snippets or a compact Swiss army knife when you want to perform quick tasks across multiple machines.
Ansible ad-hoc commands come in handy when you want to perform a quick task.
Difference between Ansible Playbooks and Ad-hoc commands
Ansible Ad-hoc commands are one-liner Linux shell commands, while playbooks are a collection of many commands with logic and structure as shown below.
Ansible playbooks are well-structured recipes, comprising multiple steps and ingredients. Suited for orchestrating complex, multistep tasks.
Ansible Ad-hoc commands are used for rapid, straightforward tasks to be executed quickly.
Task 1- write an ansible ad hoc ping command to ping servers from the inventory file
To complete this task we can use the following commands
ansible <hosts/host-group> -m ping
ansible <host/host-group> -i <inventory/file/path> -m ping
Note- if you use the ansible command without an inventory file it will look for the default global inventory file located in /etc/ansible/ansible.cfg
where by default added /etc/ansible/hosts
file as a configuration/global file for hosts so that it manages hosts in that file. We already configured and managed hosts in the previous challenge.
if you want to use another host file for your operation then you have to use the -i option to provide a path to that host file.
Explanation:-
ansible
- command line toolhosts/host-groups
- specifies the target hosts or host groups. (We created groups in /etc/ansible/hosts file).all
used to use all mentioned hosts in the hosts file regardless of any group.-i
- option used to specify the inventory file rather than the global hosts file-m
- specifies the module to use. In this case, uses the ping module.
Use the following commands to demonstrate.
Task 2- Write an ansible ad hoc command to check uptime
If you have a task to monitor the uptime of all the servers then it takes too much time to do it manually on each server. Instead, you can use Ansible Ad-hoc commands as they are managed by Ansible.
Command ansible all -a 'uptime'
In this command, -a
option is used to pass an argument as a command directly to the target host or you can use ansible all -m command -a 'uptime'
command if you want to use the Ansible command with the module. Here the command
is the module.
Instead of a command
module, you can use a shell
module also.
Task 3- Different Ad-hoc commands
Some of the important Ad-hoc commands are listed below
ansible all -a 'free -m'
ansible all -m shell -a 'df -h | grep dev'
ansible all -a pwd
ansible all -m file -a 'path=/home/ubuntu state=touch mode=0755'
In the above command, we use the
file
module which is used for creating the new directory(state=directory) or empty file(state=touch) with permissions assigned (mode=permissions). We can also change the ownership of the file.ansible all -m apt -a 'name=nginx state=installed'
Use the
apt
module to install packages on managed nodes.ansible all -m service -a 'name=nginx state=started/stop enabled=yes'
Use the
service
module to start or stop service.
To know more about Ad-hoc commands used in Ansible read the below article.
Hope this helps you to understand the basic use of Ad-hoc commands in Ansible.
Thank you so much for taking the time to read till the end! Hope you found this blog informative.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Find me on:- Hashnode LinkedIn Github
Happy Learning!