DEV Community


Ansible Ad-Hoc commands for easy use

・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.

To get route table of a node

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

To check the available disk space

root@kube-master:~# ansible all -m shell -a 'df -h' | 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%

To call a script

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

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

Discussion (0)