告别繁琐命令!用Ansible自动化部署CentOS7上的Oracle 19c数据库
2026/5/8 12:36:53 网站建设 项目流程

告别繁琐命令!用Ansible自动化部署CentOS7上的Oracle 19c数据库

在传统运维工作中,Oracle数据库的安装部署往往意味着长达数小时的手动操作:从系统配置、依赖安装到环境变量设置,每一步都可能隐藏着意想不到的"坑"。特别是在CentOS7这样的生产环境中,稍有不慎就可能导致安装失败或配置错误。而Ansible作为当下最流行的自动化运维工具,能够将这些繁琐的步骤转化为可重复执行的Playbook,实现真正的"基础设施即代码"。

本文将带您从零开始构建一个完整的Ansible Playbook,实现Oracle 19c在CentOS7上的一键式部署。不同于网上常见的简单示例,这个方案充分考虑了生产环境中的实际需求,包括:

  • 系统环境的自动化配置(防火墙、SELinux、tmp清理)
  • 智能化的Oracle用户管理(UID/GID一致性处理)
  • 多阶段验证机制确保各环节正确执行
  • 完整的回滚方案应对部署失败情况

1. 环境准备与Ansible基础配置

在开始编写Playbook前,我们需要确保控制机(运行Ansible的机器)和目标主机(安装Oracle的CentOS7服务器)满足基本要求。以下是推荐的准备工作:

控制机要求

  • 已安装Python 3.6+
  • 已安装Ansible 2.9+
  • 能够通过SSH密钥免密登录目标主机

目标主机检查清单

  • CentOS 7.6+(推荐7.9)
  • 至少4GB内存(Oracle 19c最低要求)
  • 磁盘空间:/opt目录至少10GB可用空间
  • 已配置正确的时区和主机名

提示:可以使用ansible -m ping all测试基础连接性,确保所有目标主机都能正常响应。

创建专用的项目目录结构:

oracle19c-ansible/ ├── inventories/ │ └── production ├── roles/ │ └── oracle19c/ │ ├── tasks/ │ ├── handlers/ │ ├── templates/ │ └── vars/ ├── playbooks/ │ └── deploy-oracle.yml └── files/ ├── oracle-database-ee-19c-1.0-1.x86_64.rpm └── oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm

2. 系统基础配置自动化

Oracle数据库对操作系统环境有严格要求,传统安装文档中需要手动执行的系统配置,我们可以通过Ansible模块高效完成。

2.1 安全策略配置

创建roles/oracle19c/tasks/security.yml文件处理防火墙和SELinux:

- name: 停止并禁用firewalld service: name: firewalld state: stopped enabled: no - name: 永久禁用SELinux selinux: state: disabled register: selinux_result changed_when: false - name: 检查是否需要重启以应用SELinux变更 meta: flush_handlers when: selinux_result.reboot_required

2.2 系统资源优化

Oracle安装需要特定的内核参数和用户限制,通过模板文件动态生成配置:

roles/oracle19c/templates/sysctl-oracle.conf.j2:

# Oracle 19c recommended settings kernel.shmall = {{ shmall }} kernel.shmmax = {{ shmmax }} kernel.shmmni = 4096 fs.file-max = 6815744 net.ipv4.ip_local_port_range = 9000 65500 net.core.rmem_default = 262144 net.core.rmem_max = 4194304 net.core.wmem_default = 262144 net.core.wmem_max = 1048576

对应的任务文件roles/oracle19c/tasks/system_tuning.yml:

- name: 安装tmpwatch工具 yum: name: tmpwatch state: present - name: 清理/tmp目录 command: tmpwatch 24 /tmp args: creates: /tmp/.cleanflag - name: 应用内核参数优化 template: src: sysctl-oracle.conf.j2 dest: /etc/sysctl.d/98-oracle.conf notify: apply sysctl settings - name: 设置用户限制 lineinfile: path: /etc/security/limits.conf line: "{{ item }}" with_items: - "oracle soft nofile 1024" - "oracle hard nofile 65536" - "oracle soft nproc 16384" - "oracle hard nproc 16384"

3. Oracle软件安装自动化

3.1 智能化的依赖管理

传统安装方式需要手动下载和传输rpm包,我们可以通过Ansible优化这一过程:

- name: 确保下载目录存在 file: path: /tmp/oracle_pkgs state: directory mode: 0755 - name: 下载Oracle预安装包 get_url: url: "{{ oracle_preinstall_url }}" dest: /tmp/oracle_pkgs/oracle-database-preinstall-19c.rpm checksum: "sha256:{{ preinstall_checksum }}" timeout: 300 - name: 安装预安装包 yum: name: /tmp/oracle_pkgs/oracle-database-preinstall-19c.rpm state: present - name: 清理临时文件 file: path: /tmp/oracle_pkgs state: absent

3.2 原子化的Oracle用户管理

处理Oracle用户时特别需要注意UID/GID一致性,以下是健壮的用户管理方案:

roles/oracle19c/tasks/user_management.yml:

- name: 检查现有oracle用户 command: grep oracle /etc/passwd register: oracle_user_check ignore_errors: yes changed_when: false - name: 删除现有oracle用户(如果存在) user: name: oracle state: absent remove: yes when: oracle_user_check.rc == 0 - name: 创建oinstall和dba组 group: name: "{{ item.name }}" gid: "{{ item.gid }}" state: present with_items: - { name: "oinstall", gid: 54321 } - { name: "dba", gid: 54322 } - name: 创建oracle用户 user: name: oracle uid: 54321 group: oinstall groups: dba,oper shell: /bin/bash password: "{{ oracle_password | password_hash('sha512') }}" create_home: yes home: /home/oracle

4. 数据库配置与初始化

4.1 环境变量配置

使用模板动态生成oracle用户的.bash_profile:

roles/oracle19c/templates/bash_profile.j2:

# Oracle 19c Environment Settings export ORACLE_BASE=/opt/oracle export ORACLE_HOME=/opt/oracle/product/19c/dbhome_1 export ORACLE_SID=ORCLCDB export PATH=$PATH:$ORACLE_HOME/bin export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/usr/lib export NLS_LANG=AMERICAN_AMERICA.AL32UTF8

对应的任务:

- name: 配置Oracle环境变量 template: src: bash_profile.j2 dest: /home/oracle/.bash_profile owner: oracle group: oinstall mode: 0644 - name: 应用环境变量 shell: | source /home/oracle/.bash_profile args: executable: /bin/bash chdir: /home/oracle become: yes become_user: oracle

4.2 数据库初始化与验证

- name: 初始化Oracle数据库 command: /etc/init.d/oracledb_ORCLCDB-19c configure args: creates: /opt/oracle/product/19c/dbhome_1/install/.configured register: db_init changed_when: "'Database configuration completed successfully' in db_init.stdout" notify: - start oracle listener - set sys password - name: 验证数据库状态 shell: | . /home/oracle/.bash_profile sqlplus -S / as sysdba <<EOF set heading off set feedback off select status from v\$instance; exit EOF args: executable: /bin/bash register: db_status changed_when: false become: yes become_user: oracle - name: 显示数据库状态 debug: msg: "Oracle数据库当前状态: {{ db_status.stdout }}"

5. 高级功能与生产实践

5.1 多阶段验证机制

为确保部署质量,我们可以在Playbook中添加验证步骤:

- name: 验证监听器状态 shell: | . /home/oracle/.bash_profile lsnrctl status | grep -q "STATUS of the LISTENER" args: executable: /bin/bash register: listener_check changed_when: false become: yes become_user: oracle ignore_errors: yes - name: 检查表空间使用情况 shell: | . /home/oracle/.bash_profile sqlplus -S / as sysdba <<EOF set pagesize 0 feedback off verify off heading off echo off select tablespace_name, round(used_percent) from dba_tablespace_usage_metrics; exit EOF args: executable: /bin/bash register: tablespace_usage changed_when: false become: yes become_user: oracle

5.2 完整的回滚方案

部署失败时能够安全回退至关重要,以下是回滚Playbook示例:

playbooks/rollback-oracle.yml:

- name: 停止Oracle服务 shell: | . /home/oracle/.bash_profile sqlplus -S / as sysdba <<EOF shutdown immediate; exit EOF args: executable: /bin/bash ignore_errors: yes become: yes become_user: oracle - name: 停止监听器 shell: | . /home/oracle/.bash_profile lsnrctl stop args: executable: /bin/bash ignore_errors: yes become: yes become_user: oracle - name: 移除Oracle软件 yum: name: - oracle-database-ee-19c - oracle-database-preinstall-19c state: absent - name: 清理残留文件 file: path: "{{ item }}" state: absent with_items: - /opt/oracle - /etc/oratab - /usr/local/bin/dbhome - /usr/local/bin/oraenv - /usr/local/bin/coraenv

6. 生产环境优化建议

在实际企业环境中部署Oracle 19c时,还需要考虑以下优化点:

存储配置最佳实践

  • 为Oracle数据文件使用独立的存储设备
  • 合理配置ASM磁盘组(如果使用ASM)
  • 设置适当的REDO日志大小和组数

内存调优参数

参数名推荐值说明
sga_target物理内存的60%系统全局区大小
pga_aggregate_target物理内存的20%程序全局区总量
db_cache_sizesga_target的70%数据库缓存大小
shared_pool_sizesga_target的20%共享池大小

监控与维护

  • 配置定期的AWR报告生成
  • 设置自动的统计信息收集
  • 实现备份自动化(RMAN)
  • 监控关键指标(等待事件、锁争用等)

在长期运维中,可以进一步扩展这个Ansible方案,实现:

  • 多节点Oracle RAC集群部署
  • Data Guard配置自动化
  • 定期的补丁应用工作流
  • 与CI/CD管道集成实现数据库变更管理

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询