top of page

Ansible Ad-hoc

Ansible Ad hoc commands can be run to perform some quick operations on bunch of nodes at the same time. These commands are one liner with the combination of action of modules.

Ad hoc command has two main parameters, the group of host and the ansible module to run.


Ad hoc allow us to run some quicker task without setting up Playbook from scratch.


Ansible Ad hoc command syntax :


Lets assume that you already have setup SSH key base authentication between Ansible Tower and client.


So here I have two servers under chatbot category for enabling automation through Ansible.


[root@tecgeek ~]# vim /etc/ansible/hosts

[chatbot]


Case 1. Validate connection with your clients.


[root@tecgeek ~]# ansible -m ping -i chatbot

automation.tecgeek.info | success >> {

"changed": false,

"ping": "pong"

}

automation1.tecgeek.info | success >> {

"changed": false,

"ping": "pong"

}

[root@tecgeek ~]#


Here :

-m ping : Ansible Module Ping

-I chatbot : Ansible HostGroup (Inventory)


Case 2. Get memory usage of clients.


[root@tecgeek ~]# ansible -a "free -m" -i chatbot

automation.tecgeek.info | SUCCESS | rc=0 >>

total used free shared buff/cache available

Mem: 1819 108 1270 8 160 1963

Swap: 1023 0 1023


automation1.tecgeek.info | SUCCESS | rc=0 >>

total used free shared buff/cache available

Mem: 1876 100 1508 8 160 1271

Swap: 1022 0 1022


Here :

-a "free -m" : -a switch allows to run command

-i chatbot : Inventory Group


Case 3. Get Physical memory utilization status.


[root@tecgeek ~]# ansible -m shell -a "cat /proc/meminfo|head -2" -i chatbot

automation.tecgeek.info | SUCCESS | rc=0 >>

MemTotal: 1583298 kB

MemFree: 1394042 kB


automation1.tecgeek.info | SUCCESS | rc=0 >>

MemTotal: 1232338 kB

MemFree: 1012393 kB


Here :

-m shell : Ansilbe shell module

-a "cat /proc/meminfo|head -2" : Run this command to fetch top two records of meminfo file

-I chatbot : Inventory Group


Case 4. Create a group with ad hoc command

[root@tecgeek ~]# ansible -m group -a "name=sidtest state=present" -i chatbot

automation.tecgeek.info| SUCCESS => {

"changed": true,

"gid": 1001,

"name": "sidtest",

"state": "present",

"system": false

}

automation1.tecgeek.info | SUCCESS => {

"changed": true,

"gid": 1001,

"name": "sidtest",

"state": "present",

"system": false

}


Here :

-m group : Ansible module group.

-a "name=sidtest state=present" : Create group sidtest and status should be present.


Please visit here to get list of all available module of Ansible.




bottom of page