Apa sih ad Hoc command itu? dan fungsinya apa di ansible?, ad hoc command adalah command atau perintah/syntax yang terletak di file directory /usr/bin/ansible dan fungsi digunakan untuk mengotomatisasikan satu tugas / single tasks dari sebuah inventory-book ansible, yang dimana sebelum membuat playbook ansible (kita diwajibkan tahu tentang ad hoc command terlebih dahulu).
Lihat juga artikel yang saya buat tentang Ansible di bawah ini :
- Install Ansible di WSL Ubuntu
- Login SSH Server tanpa Password
- Tutorial Belajar YAML Syntax
- Study Case – Menulis Inventory Hosts Ansible (INI Syntax)
Disini ada beberapa hal yang harus kalian persiapkan / lakukan sebelum menjelankan ad hoc command di ansible, sbb :
- Recommended: Setup SSH Key Pair (Login tanpa password)
- Inventory Hosts : Kumpulan alamat server (bisa juga menentukan variable: user,connection)
Disini saya menggunakan 2 node dengan (1 cluster/group web) seperti gambar dibawah ini topologinya:

Dan inventory Hosts saya menggunakan YAML dan di WSL Ubuntu saya taruh di filenya di directory /etc/ansible/lab-1.yml sebagai berikut :
all:
children:
web:
hosts:
webserver1:
ansible_host: 192.168.56.200
ansible_user: gunawan
ansible_ssd_private_key_file: ~/.ssh/id.rsa
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
Webserver2:
ansible_host: 192.168.56.201
ansible_user: gunawan
ansible_ssd_private_key_file: ~/.ssh/id.rsa
ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
Penjelasan tentang Ad Hoc command :
$ansible web -i /etc/ansible/lab-1.yml -m ping
Hasilnya adalah:

Penjelasan tentang penulisan ad hoc command :
Ansiblecommand ansible (kita jalankan)webadalah command group/cluster (web) yang ditulis di inventory hosts ansible-ioption untuk menambahkan path inventory host (NB : -i ini digunakan jika path directorynya tidak default ya)/etc/ansible/hostsadalah path inventory/etc/ansible/lab-1yml(file directory) dikarenakan path saya masih default , jadi sesuaikan path host kalian.-moption modulepingnama module
1. Setup: Gathering server information
$ansible web -i lab-1.yml -m setup
Isinya adalah semuat informasi tentang server/node yang ada didalam inventory hosts tersebut.
2. Setup: Filter output
$ansible webserver1,webserver2, -i lab-1.yml -m setup -a "filter=*ip*"
Isinya adalah informasi yang di filter (hanya melihat bagian IPnya saja) .
3. Menampilkan informasi Sistem Operasi (distro, versi, nama mesin dll).
Ada dua versi yaitu model default & spesifik
$ansible web -i /etc/ansible/lab-1.yml -a "uname -a" --> model default $ansible web -i /etc/ansible/lab-1.yml -m shell -a "uname -a" ---> adalah command yang spesifik mengarah ke output informasi server (mengeksekusi perintah dari server) $ansible web -i /etc/ansible/lab-1.yml -m shell -a "ifconfig" --> adalah command yang spesifik mengarah ke output informasi server (mengeksekusi perintah dari server)
4. Menampilan informasi bahwa public key WSL / hosts ansible sudah dicopy ke node
$ ansible web -i /etc/ansible/lab-1.yml -a "ls ~/.ssh"
5. Melihat isi folder /home dengan category host "webserver1" pada inverntory file Default "/etc/ansible/lab-1.yml".
$ ansible webserver1 -i /etc/ansible/lab-1.yml -a "ls /home"
6. Melihat daftar hosts didalam inventory file default /etc/ansible/lab-1.yml
Default $ansible all --list-hosts Spesific $ansible all -i /etc/ansible/lab-1.yml --list-hosts
7. Request password
Digunakan jika user yang kita setup ansible tidak mengandung privilages SUDO (karena disini saya menggunakan ROOT sebagai ansbile host di inventory host)
$ansible all -i /etc/ansible/lab-1.yml --ask-pass -m ping
8. Melihat plugin ansible
Melihat nama Plugin (modul) dan source file –> disini adalah tempat dimana plugins dari azure, jenkins dll sudah terinstall di ansbile dan sudah di support oleh ansible.
$ansible-doc -F
9. Copy file dari local ke server with Ansible
Disini saya mencoba copy file iso dari laptop ke server node (2 server sekaligus).
penjelasan src lokasi dari direktori laptop saya berada di /mnt/d/software/iso/ nama file isonya ubuntu18XXX, dan destinasinya adalah folder yang sudah saya buat tadi di atas /home/gunawan/iso jika sudah selesai copy tinggal check
$ansible all -i lab-1.yml -m copy -a "src=/mnt/d/sofware/iso/ubuntu-18.04-live-server-amd64.iso dest=/home/gunawan/iso"
10. Create Folder untuk lokasi file yang akan dicopy
- Penjelasanya web (group/cluster) dengan argument. saya membuat filenya di directory “/home/gunawan/iso” dengan code mode 775, dengan owner user & group sebagai gunawan.
ansible web -i lab-1.yml -m file -a "dest=/home/gunawan/iso mode=775 owner=gunawan group=gunawan state=directory"
- Delete Directory folder
$ansible web -i lab-1.yml -m file -a "dest=/home/gunawan/iso state=absent"
- Check ISO yang dicopy
$ansible all -i lab-1.yml -a "ls /home/gunawan/iso
11. File directory
- Chmod file directory
Penjelasan web (adalah group / cluster di inventory) dengan tujuan file yang saya buat tadi di /home/gunawan/iso dengan mode 776
$ansible web -i /etc/ansible/lab-1.yml -m file -a "dest=/home/gunawan/iso mode=775"
- Change owner directory
Penjelasan jika warna hijau & success berarti tidak ada perubahan sedangkan jika warna kuning dengan tulisan Changed berarti ada perubahan
$ansible web -i /etc/ansible/lab-1.yml -m file -a "dest=/home/gunawan/iso owner=root group=root"
12. Install packet secara automatis menggunakan ad hoc command
- install apache/web server
Penjelasan web (group/cluster) dengan argument bahwa saya akan install service “apache2” di 2 server yang ada digroup tsb, dengan module option apt dan state “present”
$ansible web -i lab-1.yml -m apt -a "name=apache2 state=present"
- otomatis install apache/web server (latest version)
$ansible web -i lab-1.yml -m apt -a "name=apache2 state=latest"
- unistall paket apache/web server
$ansible web -i lab-1.yml -m apt -a "name=apache2 state=absent
13. Manage services suatu paket (contoh web server/apache)
- manage semua service paket apache
- start service
- restart service
- stop service
$ ansible web -i lab-1.yml -m service -a "name=apache2 state=started" $ ansible web -i lab-1.yml -m service -a "name=apache2 state=restarted" $ ansible web -i lab-1.yml -m service -a "name=apache2 state=stopped"
14. Manipulasi user / manage user
- Create user
- Delete user
$ansible web -i lab-1.yml -m user -a "name=rara password=123" $ansible web -i lab-1.yml -m user -a "name=rara state=absent"
15. System di node server
- Update system
- Dist-upgrade system
- Reboot node/system
$ansible web -i lab-1.yml -a "apt update -y" $ansible web -i lab-1.yml -a "apt dist-upgrades -y" $ansible web -i lab-1.yml -m reboot
References by docs ad hoc command ansible

Admin website igunawan.com, System Administrator, DevOps Engineer
