summaryrefslogtreecommitdiff
path: root/test/integration/targets/setup_postgresql_db/tasks/main.yml
blob: 651f6b39460621c2ab9c60be431f13c6fadfdbce (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
- name: python 2
  set_fact:
    python_suffix: ""
  when: ansible_python_version is version('3', '<')

- name: python 3
  set_fact:
    python_suffix: "-py3"
  when: ansible_python_version is version('3', '>=')

- name: Include distribution and Python version specific variables
  include_vars: "{{ lookup('first_found', params) }}"
  vars:
    params:
      files:
        - '{{ ansible_distribution }}-{{ ansible_distribution_major_version }}{{ python_suffix }}.yml'
        - '{{ ansible_distribution }}-{{ ansible_distribution_version }}{{ python_suffix }}.yml'
        - '{{ ansible_os_family }}{{ python_suffix }}.yml'
        - 'default{{ python_suffix }}.yml'
      paths:
        - "{{ role_path }}/vars"

- name: make sure the dbus service is started under systemd
  systemd:
    name: dbus
    state: started
  when: ansible_service_mgr == 'systemd' and ansible_distribution == 'Fedora'

# Make sure we start fresh
- name: stop postgresql service
  service: name={{ postgresql_service }} state=stopped
  ignore_errors: True

- name: remove old db (RedHat or Suse)
  file:
    path: "{{ pg_dir }}"
    state: absent
  ignore_errors: True
  when: ansible_os_family == "RedHat" or ansible_os_family == "Suse"

- name: remove old db (FreeBSD)
  file:
    path: "{{ pg_dir }}"
    state: absent
  ignore_errors: True
  when: ansible_os_family == "FreeBSD"

# Theoretically, pg_dropcluster should work but it doesn't so remove files
- name: remove old db config and files (debian)
  file:
    path: '{{ loop_item }}'
    state: absent
  ignore_errors: True
  when: ansible_os_family == "Debian"
  loop:
  - /etc/postgresql
  - /var/lib/postgresql
  loop_control:
    loop_var: loop_item

- name: install dependencies for postgresql test
  package:
    name: "{{ postgresql_package_item }}"
    state: present
  with_items: "{{ postgresql_packages }}"
  loop_control:
    loop_var: postgresql_package_item

- name: initialize postgres (FreeBSD)
  command: /usr/local/etc/rc.d/postgresql oneinitdb
  when: ansible_os_family == "FreeBSD"

- name: Initialize postgres (RedHat systemd)
  command: postgresql-setup initdb
  when: ansible_os_family == "RedHat" and ansible_service_mgr == "systemd"

- name: Initialize postgres (RedHat sysv)
  command: /sbin/service postgresql initdb
  when: ansible_os_family == "RedHat" and ansible_service_mgr != "systemd"

- name: Initialize postgres (Debian)
  shell: '. /usr/share/postgresql-common/maintscripts-functions && set_system_locale && /usr/bin/pg_createcluster -u postgres {{ pg_verĀ }} main'
  args:
    creates: "/etc/postgresql/{{ pg_ver }}/"
  when: ansible_os_family == 'Debian'

- name: Initialize postgres (Suse)
  service: name=postgresql state=restarted
  when: ansible_os_family == 'Suse'

- name: Copy pg_hba into place
  template:
    src: files/pg_hba.conf
    dest: "{{ pg_hba_location }}"
    owner: "{{ pg_user }}"
    group: "{{ pg_group }}"
    mode: "0644"

- name: Generate locales (Debian)
  locale_gen:
    name: '{{ item }}'
    state: present
  with_items:
    - pt_BR
    - es_ES
  when: ansible_os_family == 'Debian'

# Suse: locales are installed by default (glibc-locale package).
# Fedora 23: locales are installed by default (glibc-common package)
# CentOS: all locales are installed by default (glibc-common package) but some
# RPM macros could prevent their installation (for example when using anaconda
# instLangs parameter).

- block:
  - name: Install langpacks (RHEL8)
    yum:
      name:
        - glibc-langpack-es
        - glibc-langpack-pt
        - glibc-all-langpacks
      state: present
    when: ansible_distribution_major_version is version('8', '>=')

  - name: Check if locales need to be generated (RedHat)
    shell: "localedef --list-archive | grep -a -q '^{{ locale }}$'"
    register: locale_present
    ignore_errors: True
    with_items:
      - es_ES
      - pt_BR
    loop_control:
      loop_var: locale

  - name: Reinstall internationalization files
    shell: 'yum -y reinstall glibc-common || yum -y install glibc-common'
    args:
      warn: no
    when: locale_present is failed

  - name: Generate locale (RedHat)
    command: 'localedef -f ISO-8859-1 -i {{ item.locale }} {{ item.locale }}'
    when: item is failed
    with_items: '{{ locale_present.results }}'
  when: ansible_os_family == 'RedHat' and ansible_distribution != 'Fedora'

- name: Install glibc langpacks (Fedora >= 24)
  package:
    name: '{{ item }}'
    state: 'latest'
  with_items:
    - glibc-langpack-es
    - glibc-langpack-pt
  when: ansible_distribution == 'Fedora' and ansible_distribution_major_version is version('24', '>=')

- name: enable postgresql service (FreeBSD)
  lineinfile:
    path: /etc/rc.conf
    line: 'postgresql_enable="YES"'
  when: ansible_os_family == "FreeBSD"

- name: start postgresql service
  # work-around for issue on FreeBSD where service won't restart if currently stopped
  service: name={{ postgresql_service }} state=started

- name: restart postgresql service
  service: name={{ postgresql_service }} state=restarted

########################
# Setup dummy extension:
- name: copy control file for dummy ext
  copy:
    src: dummy.control
    dest: "/usr/share/postgresql/{{ pg_ver }}/extension/dummy.control"
    mode: 0444
  when: ansible_os_family == 'Debian'

- name: copy version files for dummy ext
  copy:
    src: "{{ item }}"
    dest: "/usr/share/postgresql/{{ pg_ver }}/extension/{{ item }}"
    mode: 0444
  with_items:
  - dummy--1.0.sql
  - dummy--2.0.sql
  - dummy--3.0.sql
  when: ansible_os_family == 'Debian'

- name: add update paths
  file:
    path: "/usr/share/postgresql/{{ pg_ver }}/extension/{{ item }}"
    mode: 0444
    state: touch
  with_items:
  - dummy--1.0--2.0.sql
  - dummy--2.0--3.0.sql
  when: ansible_os_family == 'Debian'

- name: Get PostgreSQL version
  become_user: "{{ pg_user }}"
  become: yes
  shell: "echo 'SHOW SERVER_VERSION' | psql --tuples-only --no-align --dbname postgres"
  register: postgres_version_resp

- name: Print PostgreSQL server version
  debug:
    msg: "{{ postgres_version_resp.stdout }}"

# SSL configuration.
# Restricted using Debian family because of there are errors on other distributions
# that not related with PostgreSQL or psycopg2 SSL support.
# The tests key point is to be sure that ssl options work in general
- import_tasks: ssl.yml
  when:
  - ansible_os_family == 'Debian'
  - postgres_version_resp.stdout is version('9.4', '>=')