2025-04-10 11:26:55 +08:00

147 lines
3.9 KiB
YAML

---
# 检查前置条件
- name: 检查前置条件
include_tasks: check_prerequisites.yml
# 安装依赖包
- name: 安装必要的系统工具 (RedHat系列)
package:
name:
- yum-utils
- device-mapper-persistent-data
- lvm2
state: present
when: ansible_facts['os_family'] == "RedHat"
- name: 安装必要的系统工具 (Debian系列)
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
state: present
when: ansible_facts['os_family'] == "Debian"
- name: 安装必要的系统工具 (Darwin)
homebrew:
name:
- docker
state: present
when: ansible_facts['os_family'] == "Darwin"
# 添加Docker仓库
- name: 添加Docker仓库
get_url:
url: "{{ docker_repository }}"
dest: /etc/yum.repos.d/docker-ce.repo
mode: '0644'
when: ansible_facts['os_family'] == "RedHat"
# 安装Docker
- name: 安装Docker
package:
name: "{{ docker_packages }}"
state: present
when: docker_check.rc != 0
# 启动Docker服务
- name: 启动并启用Docker服务
systemd:
name: docker
state: started
enabled: yes
# 创建OpenGauss数据目录
- name: 创建OpenGauss数据目录
file:
path: "{{ opengauss_data_dir }}"
state: directory
mode: '0777'
# 拉取官方OpenGauss镜像
- name: 拉取OpenGauss Docker镜像
docker_image:
name: "opengauss/opengauss-server:{{ opengauss_version }}"
source: pull
register: pull_image_result
# 停止并移除已存在的容器(如果存在)
- name: 检查容器是否存在
command: docker ps -a -f name={{ opengauss_container_name }}
register: container_exists
changed_when: false
ignore_errors: true
- name: 移除已存在的容器
command: docker rm -f {{ opengauss_container_name }}
when: opengauss_container_name in container_exists.stdout
ignore_errors: true
# 运行OpenGauss容器
- name: 运行OpenGauss Docker容器
docker_container:
name: "{{ opengauss_container_name }}"
image: "opengauss/opengauss-server:{{ opengauss_version }}"
state: started
privileged: yes
restart_policy: always
ports:
- "{{ opengauss_host_port }}:{{ opengauss_port }}"
env:
GS_PASSWORD: "{{ opengauss_password | string }}"
GS_NODENAME: "{{ opengauss_nodename | string }}"
GS_USERNAME: "{{ opengauss_username | string }}"
GS_USER_PASSWORD: "{{ opengauss_password | string }}"
GS_PORT: "{{ opengauss_port | string }}"
GS_DB: "{{ opengauss_dbname | string }}"
volumes:
- "{{ opengauss_data_dir }}:{{ opengauss_container_data_dir }}"
healthcheck:
test: ["CMD-SHELL", "gs_ctl query -D {{ opengauss_container_data_dir }}"]
interval: 10s
timeout: 5s
retries: 3
# 等待OpenGauss服务启动
- name: 等待OpenGauss服务启动
pause:
seconds: 60 # 增加等待时间确保服务完全启动
# 检查OpenGauss连接
- name: 检查OpenGauss容器状态
command: docker ps -f name={{ opengauss_container_name }} --format "{{ '{{' }}.Status{{ '}}' }}"
register: container_status
changed_when: false
retries: 5
delay: 10
until: container_status.stdout.find("healthy") != -1 or container_status.stdout.find("Up") != -1
- name: 显示OpenGauss容器状态
debug:
msg: "OpenGauss容器状态: {{ container_status.stdout }}"
# 验证数据库连接
- name: 等待数据库端口可用
wait_for:
host: localhost
port: "{{ opengauss_port }}"
timeout: 300
state: started
- name: 测试数据库连接
command: >
docker exec {{ opengauss_container_name }}
gsql -d {{ opengauss_dbname }}
-U {{ opengauss_username }}
-W {{ opengauss_password }}
-p {{ opengauss_port }}
-c "SELECT version();"
register: db_test
changed_when: false
ignore_errors: true
- name: 显示数据库连接测试结果
debug:
msg: "数据库连接测试结果: {{ db_test.stdout if db_test.rc == 0 else db_test.stderr }}"