Pada halaman ini kita akan melakukan managing files dengan ansible. pada halaman sebelumnya dengan ansible playbook kita telah menginstall webserver, sekarang kita akan mengubah default halaman dari serbserver (index.html
) pada masing masing target hosts dengan module copy
. dan kita akan melanjutkan dengan extraction dengan module unarchive
.
Sebelum itu, kita akan membuat file html yang akan di jadikan default halaman nanti. buatlah file di folder yang sama dengan nama default_file.html
dan isikan.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Simple Webserver</title>
</head>
<body>
<h1>This is webserver</h1>
</body>
</html>
Buatlah file sites.yaml
dan isiskan:
---
- hosts: debian_based
become: true
tasks:
- name: Update apt repo chache dan Instal apache2.
apt:
update_cache: yes
name: apache2
state: present
- hosts: redhat_based
become: true
tasks:
- name: Update dnf repo chache dan Instal httpd.
dnf:
update_cache: yes
name: httpd
state: present
- name: Enable dan start httpd.
systemd:
name: httpd
enabled: yes
state: started
- hosts: all
become: true
tasks:
- name: mencopy default site ke server dan fix permission ownership
copy:
src: default_file.html
dest: /var/www/html/index.html
owner: root
group: root
mode: 0644
Lalu
ansible-playbook -K sites.yaml
Buka laman masing masing sites
Beberapa penginstalan website seperti wordpress, MediaWiki, dll. Kita harus mengunduh file templete sites dan File tersebut berbentuk compressed file lalu meng decompressnya. Untuk melakukan hal tersebut dalam ansible kita dapat menggunakan module unarchive
Kita akan membuat playbook untuk menginstall wordpress (only download and extract) di salah satu hosts. buatlah file wordpress.yaml
dan isikan:
---
- hosts: pod0
become: true
tasks:
- name: Download and extract WordPress template
unarchive:
src: https://wordpress.org/latest.zip
dest: /var/www/
remote_src: yes
owner: root
group: root
mode: 0755
Lalu Jalankan
ansible-playbook -K wordpress.yaml
playbook ini terjadi error dikarenakan host server tidak terdapat bin unzip
, Mari kita ubah playbook untuk memastikan paket unzip
tersintall.
---
- hosts: pod0
become: true
tasks:
- name: Install unzip
apt:
name: unzip
state: present
update_cache: yes
- name: Download and extract WordPress template
unarchive:
src: https://wordpress.org/latest.zip
dest: /var/www/
remote_src: yes
owner: root
group: root
mode: 0755
Lalu jalankan playbooknya lagi:
Sucsess..😊
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/copy_module.html
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/unarchive_module.html