Part: 1 - Belajar ansible
Installation
Ansible
https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html
Vagrant
https://www.vagrantup.com/docs/installation
https://www.vagrantup.com/downloads
VirtualBox
https://www.virtualbox.org/wiki/Downloads
- For Mac
- https://apple.stackexchange.com/a/373369/129518
- need to allow it in System Preferences → Security & Privacy → General
VMware (alternative for VirtualBox)
VMware
https://www.vmware.com/products/fusion.html
Getting started
Setup Vagrant boxes
Buat folder ansible-practice dan initialize vagrant untuk project ini.
$ mkdir ansible-practice
$ cd ansible-practice
$ vagrant init
Setelah itu akan ada sebuah Vagrantfile
yang dibuat secara otomatis, kita akan membuat 2 buah VM, dengan OS debian dan ubuntu, berikut caranya.
Edit Vagrantfile
menjadi seperti ini
Vagrant.configure("2") do |config|
config.vm.define "node-debian10" do |node|
node.vm.hostname = "node.debian10"
node.vm.network "private_network", ip: "192.168.10.10"
node.vm.box = "generic/debian10"
node.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.memory = 1024
vb.cpus = 2
end
end
config.vm.define "node-ubuntu1804" do |node|
node.vm.hostname = "node.ubuntu1804"
node.vm.network "private_network", ip: "192.168.10.11"
node.vm.box = "generic/ubuntu1804"
node.vm.provider "virtualbox" do |vb|
vb.gui = false
vb.memory = 1024
vb.cpus = 2
end
end
end
Lanjutkan dengan menjalankan vagrant
$ vagrant up
Bringing machine 'node-debian10' up with 'virtualbox' provider...
Bringing machine 'node-ubuntu1804' up with 'virtualbox' provider...
==> node-debian10: Checking if box 'generic/debian10' version '3.2.24' is up to date...
==> node-debian10: Clearing any previously set forwarded ports...
==> node-debian10: Clearing any previously set network interfaces...
==> node-debian10: Preparing network interfaces based on configuration...
node-debian10: Adapter 1: nat
node-debian10: Adapter 2: hostonly
==> node-debian10: Forwarding ports...
node-debian10: 22 (guest) => 2222 (host) (adapter 1)
==> node-debian10: Running 'pre-boot' VM customizations...
==> node-debian10: Booting VM...
==> node-debian10: Waiting for machine to boot. This may take a few minutes...
node-debian10: SSH address: 127.0.0.1:2222
node-debian10: SSH username: vagrant
node-debian10: SSH auth method: private key
==> node-debian10: Machine booted and ready!
==> node-debian10: Checking for guest additions in VM...
node-debian10: The guest additions on this VM do not match the installed version of
node-debian10: VirtualBox! In most cases this is fine, but in rare cases it can
node-debian10: prevent things such as shared folders from working properly. If you see
node-debian10: shared folder errors, please make sure the guest additions within the
node-debian10: virtual machine match the version of VirtualBox you have installed on
node-debian10: your host and reload your VM.
node-debian10:
node-debian10: Guest Additions Version: 5.1.38
node-debian10: VirtualBox Version: 6.1
==> node-debian10: Setting hostname...
==> node-debian10: Configuring and enabling network interfaces...
==> node-debian10: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> node-debian10: flag to force provisioning. Provisioners marked to run always will still run.
==> node-ubuntu1804: Checking if box 'generic/ubuntu1804' version '3.2.24' is up to date...
==> node-ubuntu1804: Clearing any previously set forwarded ports...
==> node-ubuntu1804: Fixed port collision for 22 => 2222. Now on port 2200.
==> node-ubuntu1804: Clearing any previously set network interfaces...
==> node-ubuntu1804: Preparing network interfaces based on configuration...
node-ubuntu1804: Adapter 1: nat
node-ubuntu1804: Adapter 2: hostonly
==> node-ubuntu1804: Forwarding ports...
node-ubuntu1804: 22 (guest) => 2200 (host) (adapter 1)
==> node-ubuntu1804: Running 'pre-boot' VM customizations...
==> node-ubuntu1804: Booting VM...
==> node-ubuntu1804: Waiting for machine to boot. This may take a few minutes...
node-ubuntu1804: SSH address: 127.0.0.1:2200
node-ubuntu1804: SSH username: vagrant
node-ubuntu1804: SSH auth method: private key
==> node-ubuntu1804: Machine booted and ready!
==> node-ubuntu1804: Checking for guest additions in VM...
node-ubuntu1804: The guest additions on this VM do not match the installed version of
node-ubuntu1804: VirtualBox! In most cases this is fine, but in rare cases it can
node-ubuntu1804: prevent things such as shared folders from working properly. If you see
node-ubuntu1804: shared folder errors, please make sure the guest additions within the
node-ubuntu1804: virtual machine match the version of VirtualBox you have installed on
node-ubuntu1804: your host and reload your VM.
node-ubuntu1804:
node-ubuntu1804: Guest Additions Version: 5.2.42
node-ubuntu1804: VirtualBox Version: 6.1
==> node-ubuntu1804: Setting hostname...
==> node-ubuntu1804: Configuring and enabling network interfaces...
==> node-ubuntu1804: Machine already provisioned. Run `vagrant provision` or use the `--provision`
==> node-ubuntu1804: flag to force provisioning. Provisioners marked to run always will still run.
Generate ssh-config untuk ansible
$ vagrant ssh-config > ssh.config
$ cat ssh.config
Host node-debian10
HostName 127.0.0.1
User vagrant
Port 2222
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/andrkrn/andrkrn/ansible-pratice/.vagrant/machines/node-debian10/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
Host node-ubuntu1804
HostName 127.0.0.1
User vagrant
Port 2200
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /Users/andrkrn/andrkrn/ansible-pratice/.vagrant/machines/node-ubuntu1804/virtualbox/private_key
IdentitiesOnly yes
LogLevel FATAL
Let's go ansible
Sekarang kita mempunyai 2 VM dan kita akan mencoba meng-install nginx
pada ke 2 VM yang kita punya.
sebelum mengenal ansible mungkin kita akan melakukan nya dengan
- connect ssh ke
node-debian10
- sudo apt-get update
- sudo apt-get install nginx
- exit
- dan lakukan lagi untuk
node-ubuntu1804
tetapi dengan ansible kita dapat membuat konfigurasi (roles) untuk meng-install nginx
dan menjalankan roles pada hosts yang dituju
kita buat ansible.cfg
terlebih dahulu
# ansible.cfg
[defaults]
interpreter_python = auto # menggunakan python3
[ssh_connection]
ssh_args = -F ./ssh.config # menggunakan ssh.config dari vagrant
lalu kita akan membuat sebuah configurasi untuk app1
.
├── Vagrantfile
├── ansible.cfg
├── app1
│ ├── hosts
│ ├── roles
│ │ └── nginx
│ │ └── tasks
│ │ └── main.yml
│ └── site.yml
└── ssh.config
# app1/hosts
node-debian10
node-ubuntu1804
# app1/site.yml
---
- hosts: all
remote_user: vagrant
become: true
roles:
- nginx
# app1/roles/nginx/tasks/main.yml
---
- name: install nginx
apt:
name: nginx
state: present
update_cache: true
setelah itu kita dapat mengjalankan configurasi yang telah kita buat dengan ansible-playbook
$ ansible-playbook -i app1/hosts app1/site.yml
PLAY [all] ****************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [node-debian10]
ok: [node-ubuntu1804]
TASK [nginx : install nginx] **********************************************************************************************************************************************
changed: [node-debian10]
changed: [node-ubuntu1804]
PLAY RECAP ****************************************************************************************************************************************************************
node-debian10 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node-ubuntu1804 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
nah sekarang, nginx telah terinstall di node-debian10
dan node-ubuntu1804
.
Jika kita mempunyai VM yang baru yang ingin kita setup sama percis dengan yang sebelumnya, kita tinggal menambahkannya di hosts
# app1/hosts
node-debian10
node-ubuntu1804
node-anotherone