安装服务器、客户端和工具 —— 使用 ansible.builtin.apt

初始化数据库 —— 使用 ansible.builtin.stat 和 ansible.builtin.shell

启动并设置开机启动 —— 使用 ansible.builtin.service

在 Debian 类系统中安装 PostgreSQL 需要完成以下三个步骤:

  1. 安装软件包:安装服务器、客户端和工具所需的包。我们将使用 Ansible 的 ansible.builtin.apt 模块。这些包包括与发行版相关的二进制文件、库和文档,适用于 Debian 类系统(包括 Ubuntu)。
  2. 初始化 PostgreSQL 数据库:使用命令行工具执行初始化操作,可以通过 Ansible 的 ansible.builtin.shell 模块完成。实际执行的命令是 /usr/lib/postgresql/14/bin/initdb -D /var/lib/postgresql/14/main(假设您使用的是版本 14)。此命令仅在需要时执行,条件检查由 ansible.builtin.stat 模块完成。
  3. 启动服务并设置开机启动:使用 ansible.builtin.service 模块启动 PostgreSQL 服务,并确保其在开机时自动启动。

实际操作:通过 Ansible Playbook 安装 PostgreSQL

接下来,我们将通过一个实际的 Ansible Playbook 来展示如何在 Debian 类系统中安装 PostgreSQL。我将向您展示如何安装 PostgreSQL 服务器、客户端工具,以及用于管理数据库的 Python 库。初始化 PostgreSQL 数据库的步骤仅在数据不存在时执行。完成这些步骤后,您可以启动服务并设置开机启动。

Ansible Playbook 示例代码:

yaml复制

---
- name: PostgreSQL 示例
  hosts: all
  become: true
  vars:
    postgresql_version: "14"
    postgresql_bin_path: "/usr/lib/postgresql/{{ postgresql_version }}/bin"
    postgresql_data_dir: "/var/lib/postgresql/{{ postgresql_version }}/main"
    ansible_ssh_pipelining: true
  tasks:
    - name: 安装必要的软件包
      ansible.builtin.apt:
        name:
          - postgresql
          - postgresql-contrib
          - libpq-dev
          - python3-psycopg2
        state: present

    - name: 检查 PostgreSQL 是否已初始化
      ansible.builtin.stat:
        path: "{{ postgresql_data_dir }}/pg_hba.conf"
      register: postgres_data

    - name: 清空数据目录
      ansible.builtin.file:
        path: "{{ postgresql_data_dir }}"
        state: absent
      when: not postgres_data.stat.exists

    - name: 初始化 PostgreSQL 数据库
      ansible.builtin.shell: "{{ postgresql_bin_path }}/initdb -D {{ postgresql_data_dir }}"
      become: true
      become_user: postgres
      when: not postgres_data.stat.exists

    - name: 启动并启用 PostgreSQL 服务
      ansible.builtin.service:
        name: postgresql
        state: started
        enabled: true

执行过程:

bash复制

$ ansible-playbook -i virtualmachines/ubuntu2204/inventory postgresql/install_debian.yml
PLAY [postgresql 示例] ******************************************************
TASK [收集主机信息] ******************************************************
ok: [ubuntu.example.com]
TASK [安装必要的软件包] **************************************************************
changed: [ubuntu.example.com]
TASK [检查 PostgreSQL 是否已初始化] *******************************************
ok: [ubuntu.example.com]
TASK [清空数据目录] ******************************************************
changed: [ubuntu.example.com]
TASK [初始化 PostgreSQL 数据库] ******************************************************
changed: [ubuntu.example.com]
TASK [启动并启用 PostgreSQL 服务] ********************************************************
ok: [ubuntu.example.com]
PLAY RECAP *******************************************************************
ubuntu.example.com         : ok=6    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

幂等性测试:

bash复制

$ ansible-playbook -i virtualmachines/ubuntu2204/inventory postgresql/install_debian.yml
PLAY [postgresql 示例] ******************************************************
TASK [收集主机信息] ******************************************************
ok: [ubuntu.example.com]
TASK [安装必要的软件包] **************************************************************
ok: [ubuntu.example.com]
TASK [检查 PostgreSQL 是否已初始化] *******************************************
ok: [ubuntu.example.com]
TASK [清空数据目录] ******************************************************
skipping: [ubuntu.example.com]
TASK [初始化 PostgreSQL 数据库] ******************************************************
skipping: [ubuntu.example.com]
TASK [启动并启用 PostgreSQL 服务] ********************************************************
ok: [ubuntu.example.com]
PLAY RECAP *******************************************************************
ubuntu.example.com         : ok=4    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0

执行前的环境状态:

bash复制

$ ssh devops@ubuntu.example.com
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-25-generic x86_64)
* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage
System information as of Wed Jun  8 10:28:25 UTC 2022
System load:  0.4248046875      Processes:               100
Usage of /:   3.5% of 38.71GB   Users logged in:         0
Memory usage: 19%               IPv4 address for enp0s3: 10.0.2.15
Swap usage:   0%                IPv4 address for enp0s8: 192.168.178.10
0 updates can be applied immediately.
The list of available updates is more than a week old.
To check for new updates run: sudo apt update
Last login: Wed Jun  8 10:28:25 2022 from 192.168.178.26
$ sudo su
root@ubuntu:/home/devops# apt list postgresql
Listing... Done
postgresql/jammy 14+238 all
root@ubuntu:/home/devops# apt list python3-psycopg2
Listing... Done
python3-psycopg2/jammy 2.9.2-1build2 amd64
root@ubuntu:/home/devops# ls -al /var/lib/postgresql/14/main/pg_hba.conf
ls: cannot access '/var/lib/postgresql/14/main/pg_hba.conf': No such file or directory
root@ubuntu:/home/devops# systemctl status postgresql
Unit postgresql.service could not be found.
root@ubuntu:/home/devops#

执行后的环境状态:

bash复制

$ ssh devops@ubuntu.example.com
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-25-generic x86_64)
* Documentation:  https://help.ubuntu.com
* Management:     https://landscape.canonical.com
* Support:        https://ubuntu.com/advantage
System information as of Wed Jun  8 10:31:53 UTC 2022
System load:  0.15771484375     Processes:               99
Usage of /:   4.1% of 38.71GB   Users logged in:         0
Memory usage: 24%               IPv4 address for enp0s3: 10.0.2.15
Swap usage:   0%                IPv4 address for enp0s8: 192.168.178.10
0 updates can be applied immediately.
Last login: Wed Jun  8 10:28:55 2022 from 192.168.178.26
$ sudo su
root@ubuntu:/home/devops# apt list postgresql
Listing... Done
postgresql/jammy,now 14+238 all [installed]
root@ubuntu:/home/devops# apt list python3-psycopg2
Listing... Done
python3-psycopg2/jammy,now 2.9.2-1build2 amd64 [installed]
root@ubuntu:/home/devops# ls -al /var/lib/postgresql/14/main/pg_hba.conf 
-rw------- 1 postgres postgres 4789 Jun  8 10:31 /var/lib/postgresql/14/main/pg_hba.conf
root@ubuntu:/home/devops# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Wed 2022-06-08 10:31:23 UTC; 1min 18s ago
    Process: 4506 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 4506 (code=exited, status=0/SUCCESS)
        CPU: 1ms
Jun 08 10:31:23 ubuntu systemd[1]: Starting PostgreSQL RDBMS...
Jun 08 10:31:23 ubuntu systemd[1]: Finished PostgreSQL RDBMS.
root@ubuntu:/home/devops#