DEV Community

loading...

Ansible Ad-Hoc commands for easy use

Sidharthan
Engineer
・2 min read

Hello Everyone,
I would like to share my first post here,
we know ansible is a great tool for automation in DevOps field. it has a great documentation though.
Ansible is maintaining its playbooks documentation great.
here, i would like to introduce some of you may know.
ad-hoc command is the one-liner ansible command that performs one task on the target hosts. It allows you to execute simple one line task against one or group of hosts defined on the inventory file configuration.
eg:

To get route table of a node

root@kube-master:~# ansible all -m shell -a 'route '
192.168.122.3 | CHANGED | rc=0 >>
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default _gateway 0.0.0.0 UG 0 0 0 enp1s0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 enp1s0

To check the available disk space

root@kube-master:~# ansible all -m shell -a 'df -h'
192.168.122.3 | CHANGED | rc=0 >>
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 394M 1.6M 393M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 19G 15G 3.5G 81% /
tmpfs 2.0G 16K 2.0G 1% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 2.0G 0 2.0G 0% 19G 15G 3.5G 81%
root@kube-master:~#

To call a script

root@kube-master:~# ansible all -m shell -a 'bash test.sh'
192.168.122.3 | CHANGED | rc=0 >>
hell world
root@kube-master:~#

here, we are using a module "shell" and applying the command on a target server.

Hope you may get some extra knowledge from this post
Thanks
Sid

Discussion (0)