-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_postgresql.yml
118 lines (100 loc) · 3.44 KB
/
install_postgresql.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 剧本作用: 用于在远程主机上安装postgresql
# 安装命令: ansible-playbook -i hosts install_postgresql.yml
- hosts: all
vars:
# mirrors.cloud.tencent.com/postgresql repo.huaweicloud.com/postgresql mirrors.tuna.tsinghua.edu.cn/postgresql
mirror: mirrors.aliyun.com/postgresql
target: /opt/soft
data_dir: /opt/pgdata
major: 15
version: 15.2-1PGDG.rhel7
arch: x86_64
rhel: rhel-7.9
tasks:
- name: create installation directory
file:
path: "{{ target }}"
state: directory
- name: Install Package
yum:
name:
- vim
- wget
- net-tools
- epel-release
- name: open port
firewalld:
port: 5432/tcp
permanent: yes
state: enabled
immediate: yes
- name: Install libs
yum:
name:
- libicu
- libzstd
- name: Download package
get_url:
url: https://{{ mirror }}/repos/yum/{{ major }}/redhat/{{ rhel }}-{{ arch }}/postgresql{{ major }}-{{ version }}.{{ arch }}.rpm
dest: /opt/soft/
- name: Download package2
get_url:
url: https://{{ mirror }}/repos/yum/{{ major }}/redhat/{{ rhel }}-{{ arch }}/postgresql{{ major }}-contrib-{{ version }}.{{ arch }}.rpm
dest: /opt/soft/
- name: Download package3
get_url:
url: https://{{ mirror }}/repos/yum/{{ major }}/redhat/{{ rhel }}-{{ arch }}/postgresql{{ major }}-libs-{{ version }}.{{ arch }}.rpm
dest: /opt/soft/
- name: Download package4
get_url:
url: https://{{ mirror }}/repos/yum/{{ major }}/redhat/{{ rhel }}-{{ arch }}/postgresql{{ major }}-server-{{ version }}.{{ arch }}.rpm
dest: /opt/soft/
- name: Install
shell: chdir={{ target }} /usr/bin/rpm -ivh {{ item }} --force --nodeps
with_items:
- "postgresql{{ major }}-{{ version }}.{{ arch }}.rpm"
- "postgresql{{ major }}-contrib-{{ version }}.{{ arch }}.rpm"
- "postgresql{{ major }}-libs-{{ version }}.{{ arch }}.rpm"
- "postgresql{{ major }}-server-{{ version }}.{{ arch }}.rpm"
- name: Create directory data dir
file:
path: "{{ data_dir }}"
state: directory
mode: '0755'
- name: Set owner and group for data dir
file:
path: "{{ data_dir }}"
state: directory
owner: postgres
group: postgres
- name: Initialize PostgreSQL database cluster
become: yes
become_user: postgres
command: /usr/pgsql-15/bin/initdb -D {{ data_dir }}
- name: Start PostgreSQL database
become: yes
become_user: postgres
command: /usr/pgsql-15/bin/pg_ctl -D {{ data_dir }} -l {{ data_dir }}/pgsql.log start
- name: Update pg_hba.conf file
become: yes
become_user: postgres
lineinfile:
path: "{{ data_dir }}/pg_hba.conf"
line: "host all all 0.0.0.0/0 md5"
- name: Update postgresql.conf file
become: yes
become_user: postgres
lineinfile:
path: "{{ data_dir }}/postgresql.conf"
line: "listen_addresses = '*'"
- name: Restart postgresql database
become: yes
become_user: postgres
command: /usr/pgsql-15/bin/pg_ctl restart -w -D {{ data_dir }}
# Password needs to be changed manually
# su postgres
# /usr/pgsql-15/bin/psql -h 127.0.0.1
# \password
# mysecretpassword
# mysecretpassword
# Clean up