ratings-display.rating-aria-label(1)
Feb 27


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]
[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)
[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
[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
[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.
Comentários