Xem thêm về cách dùng role Ansible tại đây : Tạo và dùng Role với Ansible
Việc cài đặt Oracle 11g cần có python(2 hoặc 3), unzip cài đặt sẵn như khi ta cài Tomcat 9 ở bài trên. Dưới đây ta sẽ tạo một role có tên oracle-server cho phép ta cài đặt, cấu hình và tạo một cơ sở dữ liệu, rồi gỡ cài đặt này (uninstall) nếu muốn. Oracle ta sẽ cài là Oracle 11g XE, ta có thể tải gói này về từ trang chính thức của Oracle (phải login vào để tải về). Ở đây tôi dùng một đường dẫn có sẵn trên mạng để tải gói này về. Ta phải chuyển định dạng gói này từ .rpm sang .deb để có thể cài đặt nó trong Ubuntu.
Cấu trúc thư mục role oracle-server như sau :
vagrant@ansible:~$ tree /vagrant/toan/
/vagrant/toan/
├── inventory-all-vms.yml
├── playbook-oracle-server.yml
└── roles
├── oracle-server
│ ├── defaults
│ │ └── main.yml
│ ├── tasks
│ │ ├── main.yml
│ │ ├── oracle-toan-configure.yml
│ │ ├── oracle-ubuntu-install.yml
│ │ └── oracle-ubuntu-uninstall.yml
│ └── templates
│ ├── addToanTables.sql.j2
│ ├── chkconfig.j2
│ ├── configureSystemParameters.sql.j2
│ ├── createUserToan.sql.j2
│ ├── modifyPassword.sql.j2
│ ├── oracle-toan-configure.j2
│ └── oracle-xe-conf.j2
├── python
│ └── tasks
│ └── main.yml
├── python3
│ └── tasks
│ └── main.yml
└── unzip
└── tasks
└── main.yml
Nội dung của role oracle-server như sau :
oracle-server\defaults\main.yml : tập tin này định nghĩa các biến dùng cho role oracle-server
---
oracleserver_url: "http://download.xskernel.org/soft/linux-rpm/oracle-xe-11.2.0-1.0.x86_64.rpm.zip"
oracleserver_package: "oracle-xe-11.2.0-1.0.x86_64.rpm"
toan_user: toan
toan_password: toan
oracle_sys_password: syspwd
oracle_system_password: manager
config_file_path: /tmp/oracleToan
oracle_install: True
oracle_toan_configure: True
oracle_uninstall: False
oracle_http_port: 8081
oracle_listener_port: 1521
oracle_system_pwd: sysadmin
oracle_start_boot: y
* oracle-server\tasks
oracle-server\tasks\main.yml :
- name : uninstall oracle
include_tasks: oracle-ubuntu-uninstall.yml
when: oracle_uninstall == True
- name : install oracle
include_tasks: oracle-ubuntu-install.yml
when: oracle_install == True
- name : configure oracle for toan
include_tasks: oracle-toan-configure.yml
when: oracle_toan_configure == True
Ansible sẽ đọc tập tin này và thực hiện tuần tự các tác vụ được định nghĩa trong đó. Ở đây, tùy vào giá trị của các biến oracle_uninstall, oracle_install và oracle_toan_configure là True hay False mà ansible sẽ thực hiện các tác vụ định nghĩa trong những tập tin tương ứng.
Giá trị mặc định của các biến này được định nghĩa là oracle_install: True; oracle_toan_configure: True . Vậy mặc định khi ta thực hiện lệnh ansible-playbook, ansible sẽ cài đặt oracle và tạo một CSDL như định nghĩa trong tập tin oracle-toan-configure.yml
oracle-server\tasks\oracle-ubuntu-install.yml :
- name: install "yum", "libaio1", "bc", "alien"
become: yes
apt:
name: ["yum", "libaio1", "bc", "alien"]
state: latest
force_apt_get: yes
- name: download and unarchive {{ oracleserver_url }}
become: yes
unarchive:
src: "{{ oracleserver_url }}"
dest: "/tmp"
remote_src: yes
- name: Convert package {{ oracleserver_package }} to oracle-xe_11.2.0-2_amd64.deb
become: yes
shell: cd /tmp/Disk1 && alien --scripts --veryverbose {{ oracleserver_package }} >> /tmp/oracle-convert.log
- name: Copy work around /sbin/chkconfig file
become: yes
template:
src: templates/chkconfig.j2
dest: /sbin/chkconfig
mode: 0755
- name: Give permissions to oracle-xe_11.2.0-2_amd64.deb
become: yes
file:
path: "/tmp/Disk1/oracle-xe_11.2.0-2_amd64.deb"
mode: a+xrw
- name: install oracle-xe_11.2.0-2_amd64.deb
become: yes
apt:
deb: /tmp/Disk1/oracle-xe_11.2.0-2_amd64.deb
- name: Copy oracle config file
become: yes
template:
src: templates/oracle-xe-conf.j2
dest: /tmp/oracle-xe.conf
mode: 0755
- name: Test if execution of oracle_env.sh is already added in /etc/bash.bashrc
shell: grep -c "^/u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh" /etc/bash.bashrc || true
register: test_oracle_env
- name: add oracle environement variables by adding execution of oracle_env.sh in /etc/bash.bashrc
become: yes
lineinfile:
dest: /etc/bash.bashrc
line: ". /u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh"
when: test_oracle_env.stdout == "0"
- name: Export oracle environnement variables by executing /u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh
become: yes
shell: . /u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh
- name: configure oracle-xe
become: yes
shell: /etc/init.d/oracle-xe configure < /tmp/oracle-xe.conf >> /tmp/oracle-xe.log
- name: Start Oracle-xe service
become: yes
shell: /etc/init.d/oracle-xe start
#- name: Add start oracle-xe service at boot
# become: yes
# shell: update-rc.d oracle-xe defaults
Tập tin này định nghĩa các bước để cài đặt Oracle 11g XE. Cụ thể là :
- Cài đặt những thư viện mà oracle cần dùng.
- Tải về gói rpm để cài oracle và giải nén nó.
- Chuyển gói rpm thành gói deb để có thể cài đặt nó trong Ubuntu.
- Tạo tập tin chkconfig nhằm cho phép cấu hình oracle khởi động khi boot và chép nó vào /sbin/chkconfig . Đây là cách hack để sử dụng lệnh "update-rc.d oracle-xe defaults" vì chkconfig không còn được dùng trong Ubuntu nữa mà quá trình cài đặt của gói oracle lại dùng đến chkconfig.
- Cài gói .deb vừa tạo
- Thực hiện lệnh "configure oracle-xe" để cấu hình oracle vừa cài đặt, nội dung thông tin cần trả lời cho quá trình cài đặt này được định nghĩa trong tập tin oracle-xe.conf
- Sau cùng là khởi động oracle một khi đã cài đặt xong.
oracle-server\tasks\oracle-toan-configure.yml :
- name: Create {{ config_file_path }}
file:
path: "{{ config_file_path }}"
state: directory
mode: "u+rwx,g+rwx,o=rwx"
recurse: yes
- name: Copy oracle-toan-configure.sh file to {{ config_file_path }}
become: yes
template:
src: templates/oracle-toan-configure.j2
dest: "{{ config_file_path }}/oracle-toan-configure.sh"
mode: 0755
- name: Copy modifyPassword.sql file to {{ config_file_path }}
become: yes
template:
src: templates/modifyPassword.sql.j2
dest: "{{ config_file_path }}/modifyPassword.sql"
mode: 0755
- name: Copy createUserToan.sql file to {{ config_file_path }}
become: yes
template:
src: templates/createUserToan.sql.j2
dest: "{{ config_file_path }}/createUserToan.sql"
mode: 0755
- name: Copy configureSystemParameters.sql file to {{ config_file_path }}
become: yes
template:
src: templates/configureSystemParameters.sql.j2
dest: "{{ config_file_path }}/configureSystemParameters.sql"
mode: 0755
- name: Copy addToanTables.sql file to {{ config_file_path }}
become: yes
template:
src: templates/addToanTables.sql.j2
dest: "{{ config_file_path }}/addToanTables.sql"
mode: 0755
- name: update oracle user password
become: yes
user:
name: oracle
password: "{{ 'oracle' | password_hash('sha512') }}"
- name: configure oracle for toan
become: yes
become_user: oracle
shell: "{{ config_file_path }}/oracle-toan-configure.sh"
args:
chdir: "{{ config_file_path }}"
Tập tin này định nghĩa việc thực hiện việc cấu hình và tạo một CSDL cho user toan, nó sẽ gọi đến tập tin oracle-toan-configure.sh để thực thi các script sql trong thư mục templates.
oracle-server\tasks\oracle-ubuntu-uninstall.yml :
- name: Stop Oracle-xe service
become: yes
service:
name: "oracle-xe"
state: stopped
- name: Create archive directory /dev/rmt0
file:
path: /dev/rmt0
state: directory
mode: "u+rwx,g+rwx,o=rwx"
recurse: yes
- name: Compress directory /u01/app/oracle into /dev/rmt0/oracle-bkup.tgz
archive:
path: /u01/app/oracle
dest: /dev/rmt0/oracle-bkup.tgz
- name: Remove oracle-xe package
become: yes
apt:
name: oracle-xe
state: absent
purge: yes
- name: Clean up /u01
become: yes
file:
path: /u01
state: absent
- name: Clean up /etc/oratab
become: yes
file:
path: /etc/oratab
state: absent
- name: Delete /etc/init.d/oracle-xe
become: yes
file:
path: /etc/init.d/oracle-xe
state: absent
- name: Delete /etc/default/oracle-xe
become: yes
file:
path: /etc/default/oracle-xe
state: absent
- name: Delete {{ config_file_path }}
become: yes
file:
path: "{{ config_file_path }}"
state: absent
- name: Remove the start of oracle-xe service at boot
become: yes
shell: update-rc.d -f oracle-xe remove
Tập tin này định nghĩa các bước để backup rồi xóa oracle (uninstall)
* oracle-server\templates
Các script SQL để cấu hình CSDL cho user toan
oracle-server\templates\modifyPassword.sql.j2 :
set echo on
spool ./modifyPassword.log
alter profile default LIMIT PASSWORD_LIFE_TIME UNLIMITED;
alter user system identified by {{ oracle_system_password }};
alter user sys identified by {{ oracle_sys_password }};
commit;
spool off
exit 0
oracle-server\templates\configureSystemParameters.sql.j2 :
set echo on
spool ./configureSystemParameters.log
alter system set processes=1200 scope=spfile;
alter system set sessions=1822 scope=spfile;
alter system set transactions=3000 scope=spfile;
alter system set open_cursors=4000 scope=spfile;
spool off
exit 0
oracle-server\templates\createUserToan.sql.j2 :
set echo on
spool ./createUserToan.log
create user {{ toan_user }} identified by {{ toan_password }};
grant connect to {{ toan_user }};
grant resource to {{ toan_user }};
grant exp_full_database to {{ toan_user }};
grant imp_full_database to {{ toan_user }};
alter user {{ toan_user }} default tablespace TOAN_MDATA;
commit;
spool off
exit 0
oracle-server\templates\addToanTables.sql.j2 :
set echo on
spool ./addToanTables.log
drop table USER_LOGIN cascade constraints;
create table USER_LOGIN (
login varchar2(255) not null,
password varchar2(255),
primary key (login)
);
insert into USER_LOGIN (LOGIN, PASSWORD) values ('toan','test');
spool off
exit 0
Các tập tin lệnh bash và cấu hình
oracle-server\templates\oracle-xe-conf.j2 :
{{ oracle_http_port }}
{{ oracle_listener_port }}
{{ oracle_system_pwd }}
{{ oracle_system_pwd }}
{{ oracle_start_boot }}
Tập tin này được dùng trong lúc cấu hình oracle lần đầu tiên bằng lệnh "configure oracle-xe" như ta thấy ở trên.
oracle-server\templates\chkconfig.j2 :
#!/bin/bash
# Oracle 11gR2 XE installer chkconfig work around for Ubuntu
file=/etc/init.d/oracle-xe
if [[ ! `tail -n1 $file | grep INIT` ]]; then
cat >> $file <<-EOM
#
### BEGIN INIT INFO
# Provides: OracleXE
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Oracle 11g Express Edition
### END INIT INFO
EOM
fi
case "$1" in
--add)
echo "Add start oracle-xe service at boot"
update-rc.d oracle-xe defaults
;;
--del)
echo "Remove the start of oracle-xe service at boot"
update-rc.d -f oracle-xe remove
;;
esac
exit 0
Tập tin này cấu hình việc khởi động tự động oracle khi boot. Nó được chép vào /sbin/chkconfig và sẽ được gọi đến khi cài hoặc xóa oracle.
oracle-server\templates\oracle-toan-configure.j2 :
#!/bin/bash
. /u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh
START_TIME=$SECONDS
echo BEGIN: `date "+%Y-%m-%d %H:%M:%S"`
sqlplus '/ as sysdba' @ {{ config_file_path }}/modifyPassword.sql
sqlplus system/{{ oracle_system_password }} @ {{ config_file_path }}/configureSystemParameters.sql
sqlplus '/ as sysdba' @ {{ config_file_path }}/createUserToan.sql
sqlplus {{ toan_user }}/{{ toan_password }} @ {{ config_file_path }}/addToanTables.sql
echo END: `date "+%Y-%m-%d %H:%M:%S"`
ELAPSED_TIME=$(($SECONDS - $START_TIME))
echo "DURATION=$ELAPSED_TIME seconds"
exit 0
Tập tin này định nghĩa việc thực thi các script sql để cấu hình và tạo một CSDL cho user toan.
* Các tập tin inventory và playbook
inventory-all-vms.yml :
all:
children:
ubuntu_servers:
hosts:
jenkins_ubuntu18:
ansible_host: 10.0.0.11
toan_local:
hosts:
toan_ubuntu16:
ansible_host: 10.0.0.12
Trên đây là tập tin inventory định nghĩa các host
playbook-oracle-server.yml :
- name: Content of all VMs
hosts: toan_ubuntu16
become: yes
roles:
- { role: ./roles/unzip }
- { role: ./roles/python3 }
- { role: ./roles/python }
- name: Content of all parkfolio servers
hosts: toan_ubuntu16
become: yes
roles:
- { role: ./roles/oracle-server }
Trên đây là tập tin playbook để thực hiện việc cài đặt/cấu hình/xóa oracle.
* Tập tin Vagrantfile định nghĩa các máy ảo như sau :
Vagrant.configure("2") do |config|
#common part of vms
BOX_IMAGE = "ubuntu/bionic64" # bionic 64 = ubuntu 18.04 64 bits
#ansible vm description
config.vm.define "ansible" do |ansible|
ansible.vm.box = BOX_IMAGE
ansible.vm.hostname = "ansible"
ansible.vm.network "private_network", ip: "10.0.0.10", virtualbox__intnet: true
ansible.vm.provision "shell", inline: "apt install python -yq", privileged: true
ansible.vm.provision "shell", inline: "sudo apt-get install -yq software-properties-common", privileged: true
ansible.vm.provision "shell", inline: "sudo apt-add-repository -y ppa:ansible/ansible", privileged: true
ansible.vm.provision "shell", inline: "sudo apt-get update -yq", privileged: true
ansible.vm.provision "shell", inline: "sudo apt-get install -yq ansible", privileged: true
ansible.vm.provision "shell", inline: "sudo apt-get install -yq mc", privileged: true
end
#jenkins vm description
config.vm.define "jenkins" do |jenkins|
jenkins.vm.box = BOX_IMAGE
jenkins.vm.hostname = "jenkins"
jenkins.vm.network "private_network", ip: "10.0.0.11", virtualbox__intnet: true
jenkins.vm.provision "shell", inline: "sudo apt-get install -yq mc", privileged: true
end
config.vm.define "toan" do |toan|
toan.vm.box = "ubuntu/xenial64"
toan.vm.hostname = "toan"
toan.disksize.size = '10GB'
toan.vm.network "private_network", ip: "10.0.0.12", virtualbox__intnet: true
toan.vm.network "forwarded_port", guest: 8084, host: 8084
toan.vm.network "forwarded_port", guest: 1521, host: 1524
toan.vm.provision "shell", inline: "sudo apt-get install -yq mc", privileged: true
end
# Provider-specific configuration
config.vm.provider "virtualbox" do |v|
v.memory = "1024"
end
end
* Cài đặt Oracle 11g XE trong Ubuntu có thể tóm gọn vào các lệnh sau :
vagrant@toan:~$ sudo apt-get install unzip
vagrant@toan:~$ sudo apt-get install alien
vagrant@toan:~$ sudo apt-get install libaio1
vagrant@toan:~$ cd /tmp
vagrant@toan:/tmp$ wget http://download.xskernel.org/soft/linux-rpm/oracle-xe-11.2.0-1.0.x86_64.rpm.zip
vagrant@toan:/tmp$ unzip oracle-xe-11.2.0-1.0.x86_64.rpm.zip
vagrant@toan:/tmp$ cd /tmp/Disk1
vagrant@toan:/tmp/Disk1$ sudo alien --scripts --veryverbose oracle-xe-11.2.0-1.0.x86_64.rpm >> convert-oracle.log
vagrant@toan:/tmp/Disk1$ sudo cp /vagrant/toan/roles/oracle-server/templates/chkconfig.j2 /sbin/chkconfig
vagrant@toan:/tmp/Disk1$ sudo chmod 755 /sbin/chkconfig
vagrant@toan:/tmp/Disk1$ sudo dpkg --install ./oracle-xe_11.2.0-2_amd64.deb
vagrant@toan:~$ . /u01/app/oracle/product/11.2.0/xe/bin/oracle_env.sh
vagrant@toan:~$ sudo /etc/init.d/oracle-xe configure < /tmp/oracle-xe.conf >> /tmp/oracle-xe.log
vagrant@toan:~$ sudo /etc/init.d/oracle-xe start
* Xóa Oracle có thể tóm gọn như sau
$ sudo apt-get --remove oracle-xe
$ sudo apt-get --purge oracle-xe