使用ansible 自动化同时多个节点安装oracle 11g 数据库基于rhel 7.9

1、找一台机器,其上已经安装完docker 并启动docker服务,这里控制节点ip:192.168.11.99
2、计划在2台已安装好rhel7.9的操作系统上安装oracle11g
ip 分别为192.168.11.153、192.168.11.154
3、在控制节点上运行如下脚本,修改v_host1、v_host2,有多台可以继续依次添加,这里host1,host2为主机名,配置好后会自动根据该名称为目标主机配置该主机名; 修改v_hostsum 为总共需要安装oracle数据库的节点数
#vi install_oracle11g.sh

#!/bin/bash
#vars 
v_host1='host1 ansible_ssh_host=192.168.11.154 ansible_ssh_user=root ansible_ssh_pass="123456"'
v_host2='host2 ansible_ssh_host=192.168.11.153 ansible_ssh_user=root ansible_ssh_pass="123456"'
v_hostsum=2
#defaults vars
v_image_path="registry.cn-shenzhen.aliyuncs.com/shine_fei/centos8/ansible"
v_image_version="2.9.1"
#get and run a ansible docker image  
v_1=`docker images  |grep $v_image_path |grep $v_image_version |wc -l`
if [[ $v_1 -eq 0 ]]; then
	docker pull $v_image_path:$v_image_version
fi
v_ipforward=`sysctl net.ipv4.ip_forward | awk '{print $3}'`
if [[ $v_ipforward -eq 0 ]];then
	sysctl -w net.ipv4.ip_forward=1
fi
v_2=`docker ps |grep $v_image_path |grep $v_image_version |wc -l`
if [[ $v_2 -eq 0 ]]; then
	if [ ! -d "/opt/ansible" ]; then
		mkdir /opt/ansible
	fi
        docker run -d --volume=/opt/ansible:/opt/ansible:ro $v_image_path:$v_image_version
fi  
if [ ! -f /opt/ansible/hosts.j2 ]; then
	echo "###download hosts.j2 file to /opt/ansible"
	curl -o /opt/ansible/hosts.j2  http://feilunshuai.tpddns.net:8446/repos/databases/scripts/hosts.j2
fi
if [ ! -f /opt/ansible/vars.yaml ]; then
        echo "###download vars.yaml file to /opt/ansible"
        curl -o /opt/ansible/vars.yaml  http://feilunshuai.tpddns.net:8446/repos/databases/scripts/vars.yaml
fi
if [ ! -f /opt/ansible/install_oracle11g_single_instance_fs.yml ]; then
        echo "###download install_oracle11g_single_instance_fs.yml file to /opt/ansible"
        curl -o /opt/ansible/install_oracle11g_single_instance_fs.yml  http://feilunshuai.tpddns.net:8446/repos/databases/scripts/install_oracle11g_single_instance_fs.yml
fi


#generate ansible inventory
v_3="/opt/ansible"
echo "###generate ansible inventory"
cat /dev/null >  $v_3/inventory
for i in $(seq 1 $v_hostsum)
do 
	echo "[host$i]" >> $v_3/inventory
	eval echo '$'v_host$i >> $v_3/inventory
#check number of ORACLE_SID for hosts
	v_hostname=`eval echo '$'v_host$i |awk '{print $1}'`
	echo "###check ORACLE_SID_$v_hostname variables exists in vars.yaml"
	v_countoraclesid=`cat $v_3/vars.yaml |grep -v '^#'|grep ORACLE_SID_$v_hostname |wc -l`
	if [[ $v_countoraclesid -eq 0 ]]; then
		echo "error: The ORACLE_SID_$v_hostname dose not exist"
		echo "error: Please modify $v_3/vars.yaml, add ORACLE_SID_$v_hostname variables"
		exit
	else
		echo "###check successful"
	fi
	
done
v_containerid=`docker ps |grep $v_image_path |grep $v_image_version |awk '{print $1}'`
echo "***************************************************"
echo "1. If you want to self-defining some parameter, please input n/N. Then modify /opt/ansible/vars.yaml parameter for you. Then use command as below to execute."
echo "docker exec -it $v_containerid /bin/bash"
echo "cd /opt/ansible ; ansible-playbook -i inventory install_oracle11g_single_instance_fs.yml"
echo ""
echo "2. If you just use default config, please input y/Y."
echo "***************************************************"

read -r -p "Are You Want To Continue? [Y/n] " input

case $input in
    [yY][eE][sS]|[yY])
		echo "Yes"
		docker exec $v_containerid ansible-playbook -i $v_3/inventory $v_3/install_oracle11g_single_instance_fs.yml 
		;;

    [nN][oO]|[nN])
		echo "No"
                exit 1
       	        ;;

    *)
		echo "Invalid input..."
		exit 1
		;;
esac

4、然后执行脚本
#chmod +x install_oracle11g.sh
#./install_oracle11g.sh 根据提示,如果使用默认配置,直接选y,继续,如果需要自定义相关参数,选n,然后根据1. 后面的提示命令继续执行,这里我使用默认配置继续执行,结果如下

[root@linuxidc1 work]# ./install_oracle11g.sh 
###download hosts.j2 file to /opt/ansible
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   337  100   337    0     0     90      0  0:00:03  0:00:03 --:--:--    90
###download vars.yaml file to /opt/ansible
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1378  100  1378    0     0    502      0  0:00:02  0:00:02 --:--:--   502
###download install_oracle11g_single_instance_fs.yml file to /opt/ansible
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 14243  100 14243    0     0   8026      0  0:00:01  0:00:01 --:--:--  8024
###generate ansible inventory
###check ORACLE_SID_host1 variables exists in vars.yaml
###check successful
###check ORACLE_SID_host2 variables exists in vars.yaml
###check successful
***************************************************
1. If you want to self-defining some parameter, please input n/N. Then modify /opt/ansible/vars.yaml parameter for you. Then use command as below to execute.
docker exec -it 87429c83533a /bin/bash
cd /opt/ansible ; ansible-playbook -i inventory install_oracle11g_single_instance_fs.yml

2. If you just use default config, please input y/Y.
***************************************************
Are You Want To Continue? [Y/n] y
Yes
[WARNING]: Found both group and host with same name: host1
[WARNING]: Found both group and host with same name: host2

PLAY [configure oracle environment and download software] **********************

TASK [Gathering Facts] *********************************************************
ok: [host1]
ok: [host2]
....
....

如果中途报错,可以修复后,继续按提示1.下的命令继续执行,示例如下

docker exec -it 87429c83533a /bin/bash
cd /opt/ansible ; ansible-playbook -i inventory install_oracle11g_single_instance_fs.yml

5、执行成功后最终结果类似如下

"Log messages written to /u01/app/grid/diag/tnslsnr/host2/listener/alert/log.xml",
            "Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=host2)(PORT=1521)))",
            "",
            "Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))",
            "STATUS of the LISTENER",
            "------------------------",
            "Alias                     LISTENER",
            "Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production",
            "Start Date                22-MAR-2021 21:11:19",
            "Uptime                    0 days 0 hr. 0 min. 0 sec",
            "Trace Level               off",
            "Security                  ON: Local OS Authentication",
            "SNMP                      OFF",
            "Listener Log File         /u01/app/grid/diag/tnslsnr/host2/listener/alert/log.xml",
            "Listening Endpoints Summary...",
            "  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=host2)(PORT=1521)))",
            "The listener supports no services",
            "The command completed successfully"
        ]
    }
}

PLAY RECAP *************************************************************************************************************************************************************************************
host1                      : ok=73   changed=53   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host2                      : ok=73   changed=55   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

6、/opt/ansible/vars.yaml参数解释

---
groups_lists: ["oinstall","dba","oper","asmadmin","asmoper","asmdba"]    ---需要创建的用户组
pw_os_oracle: oracle    ---新建oracle用户的密码
pw_os_grid: oracle		---新建grid用户的密码
software_url1: "http://feilunshuai.tpddns.net:8446/repos/databases/oracle11g/p13390677_112040_Linux-x86-64_1of7.zip"     ---数据库软件下载地址,其他类似
software_url2: "http://feilunshuai.tpddns.net:8446/repos/databases/oracle11g/p13390677_112040_Linux-x86-64_2of7.zip"
software_url3: "http://feilunshuai.tpddns.net:8446/repos/databases/oracle11g/p13390677_112040_Linux-x86-64_3of7.zip"
yum_repos: "http://feilunshuai.tpddns.net:8446/repos/rhel-7-server-rpms/"  	---yum仓库地址
url_checksum: "sha256:http://feilunshuai.tpddns.net:8446/repos/databases/oracle11g/checksum.txt"   	 ---软件包sha256校验,校验不通过,就重新下载
gridrsp_url: "http://feilunshuai.tpddns.net:8446/repos/databases/oracle11g/grid.rsp"  						---grid 软件安装的response模板
oracle_softonlyrsp_url: "http://feilunshuai.tpddns.net:8446/repos/databases/oracle11g/oracle_soft.rsp" 					---oracle软件安装的response模板
ORACLE_INSTALL: "/u01/app"    		---所有软件安装的基础目录
ORACLE_BASE: "/u01/app/oracle"		
ORACLE_HOME: "/u01/app/oracle/product/11.2.0/dbhome_1"  
ORACLE_SID_host1: "orcldb1"     	---host1的实例名称,这里的host1为主机名,必须与实际主机名一致,脚本会检查该变量是否合规
ORACLE_SID_host2: "orcldb2"		 	---host2的实例名称,这里的host2为主机名,必须与实际主机名一致,脚本会检查该变量是否合规
GRID_BASE: "/u01/app/grid"
GRID_HOME: "/u01/app/11.2.0/grid"
INVENTORY_LOCATION: "/u01/app/oraInventory"
firewalld_allow_ips: ["192.168.11.151","192.168.11.152"]  ---根据需要配置主机需要放通哪些ip访问主机
#database
sysPassword: "oracle123"    		---sys用户密码
systemPassword: "oracle123"			---system用户密码
redoLogFileSize: 50					---redo日志大小,单位Mb
storageType: "FS"     				---目前只支持FS类型
characterSet: "AL32UTF8"			---数据库字符集
nationalCharacterSet: "AL16UTF16"	---国家字符集
listeners: "LISTENER"				---侦听
totalMemory: "1000"					---sga占80%,pga占20%,单位Mb
databaseType: "OLTP"				---数据库类型

7、清理
如果不需要或重新安装,可以先清理一下

rm -rf /u01/app/
rm -rf /etc/oratab

8、其他参考

docker exec -it 87429c83533a /bin/bash
cd /opt/ansible ;

如果gird 软件已经安装,可以跳过grid 软件安装
ansible-playbook  -i inventory  --skip-tags "grid" install_oracle11g_single_instance_fs.yml
如果oracle 软件已经安装,可以跳过grid和oracle 软件安装
ansible-playbook  -i inventory  --skip-tags "grid,oracle" install_oracle11g_single_instance_fs.yml