Commit 05ad35f0 authored by Louis VINCHON's avatar Louis VINCHON
Browse files

Role postgresql: copy 'centos' tasks to 'ubuntu' and 'debian' folders

Duplicate the tasks so that we can, in next commits, eliminate the unrelated
tasks for each OS.
parent e83a8d53
---
- name: Collect and print PostgreSQL version on Ubuntu
block:
- name: Collect PostgreSQL version
become: yes
become_user: postgres
postgresql_info:
filter: ver*
register: postgresql_version
- name: Print PostgreSQL version
debug:
var: postgresql_version.version.major
when: (ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] == "18")
...
---
- name: Check that the database doesn't exist yet on CentOS
stat:
path: "{{path_to_icat}}/postgresql.conf"
register: stat_result_centos
when: ansible_facts['distribution'] == "CentOS"
- name: Check that the database doesn't exist yet on Debian
stat:
path: "/etc/postgresql/{{ postgresql_version.version.major }}/{{ cluster_name }}/postgresql.conf"
register: stat_result_debian
when: ansible_facts['distribution'] == "Debian"
- name: Check that the database doesn't exist yet on Ubuntu
stat:
path: "/etc/postgresql/{{ postgresql_version.version.major }}/{{ cluster_name }}/postgresql.conf"
register: stat_result_ubuntu
when: ansible_facts['distribution'] == "Ubuntu"
- name: Initialization of ICAT database on CentOS 7
command: /usr/pgsql-{{ external_postgresql_version }}/bin/initdb -D {{path_to_icat}}
become: true
become_user: postgres
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7" and stat_result_centos.stat.exists == False and not use_distribution_postgresql)
- name: Initialization of ICAT database on CentOS 7
command: /usr/bin/initdb -D {{path_to_icat}}
become: true
become_user: postgres
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7" and stat_result_centos.stat.exists == False and use_distribution_postgresql)
- name: Initialization of ICAT database on CentOS 8
command: /usr/pgsql-{{ external_postgresql_version }}/bin/initdb -D {{path_to_icat}}
become: true
become_user: postgres
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "8" and stat_result_centos.stat.exists == False and not use_distribution_postgresql)
- name: Initialization of ICAT database on CentOS 8
command: /usr/bin/initdb -D {{path_to_icat}}
become: true
become_user: postgres
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "8" and stat_result_centos.stat.exists == False and use_distribution_postgresql)
- name: Initialization of ICAT database on Debian 10
command: /usr/bin/pg_createcluster -d {{ path_to_icat }} {{ postgresql_version.version.major }} {{ cluster_name }}
become: true
become_user: postgres
when: (ansible_facts['distribution'] == "Debian" and ansible_facts['distribution_major_version'] == "10" and stat_result_debian.stat.exists == False)
- name: Initialization of ICAT database on Ubuntu 18.04
command: /usr/bin/pg_createcluster -d {{ path_to_icat }} {{ postgresql_version.version.major }} {{ cluster_name }}
become: true
become_user: postgres
when: (ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] == "18" and stat_result_ubuntu.stat.exists == False)
...
---
- name: Create PostgreSQL iRODS user
postgresql_user:
name: "{{ database_user }}"
state: present
password: "{{ database_password }}"
login_user: postgres
become: true
become_user: postgres
- name: Create PostgreSQL iRODS database
postgresql_db:
name: "{{ database_name }}"
state: present
login_user: postgres
become: true
become_user: postgres
- name: Set database privileges
postgresql_privs:
database: "{{ database_name }}"
state: present
privs: ALL
type: database
roles: "{{ database_user }}"
login_user: postgres
become: true
become_user: postgres
...
---
- name: Create database directory
file:
state: directory
path: "{{path_to_icat}}"
owner: postgres
group: postgres
mode: 0700
recurse: yes
...
---
# tasks file for postgresql
- import_tasks: repos.yml
- import_tasks: packages.yml
- import_tasks: pip.yml
- import_tasks: users.yml
- import_tasks: files.yml
- import_tasks: db_info.yml
- import_tasks: db_init.yml
- import_tasks: patches.yml
- import_tasks: security.yml
- import_tasks: systemd.yml
- import_tasks: db_privs.yml
...
---
- name: Install EPEL repository
yum:
conf_file: '/etc/yum.conf'
name:
- epel-release
state: present
update_cache: yes
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7" and use_external_epel)
- name: Install ancillary packages for CentOS 7
yum:
conf_file: '/etc/yum.conf'
name:
- libselinux-python
- python-pip
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7")
- name: Install PostgreSQL {{ external_postgresql_version }} packages for CentOS 7
yum:
conf_file: '/etc/yum.conf'
name:
- 'postgresql{{ external_postgresql_version | replace(".", "") }}'
- 'postgresql{{ external_postgresql_version | replace(".", "") }}-server'
state: present
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7" and not use_distribution_postgresql)
- name: Install PostgreSQL 9.2 packages for CentOS 7
yum:
conf_file: '/etc/yum.conf'
name:
- postgresql
- postgresql-server
state: present
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7" and use_distribution_postgresql)
- name: Install ancillary packages for CentOS 8
dnf:
conf_file: '/etc/dnf/dnf.conf'
name:
- python3-libselinux
- python3-pip
state: present
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "8")
- name: Disable the built-in PostgreSQL module
command: dnf -qy module disable postgresql
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "8" and not use_distribution_postgresql)
- name: Install PostgreSQL {{ external_postgresql_version }} packages for CentOS 8
dnf:
conf_file: '/etc/dnf/dnf.conf'
name:
- 'postgresql{{ external_postgresql_version | replace(".", "") }}'
- 'postgresql{{ external_postgresql_version | replace(".", "") }}-server'
state: present
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "8" and not use_distribution_postgresql)
- name: Install PostgreSQL 10 packages for CentOS 8
dnf:
conf_file: '/etc/dnf/dnf.conf'
name:
- postgresql
- postgresql-server
state: present
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "8" and use_distribution_postgresql)
- name: Install ancillary packages for Ubuntu 18.04
apt:
name:
- python-apt
- python-pip
state: present
when: (ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] == "18")
- name: Install PostgreSQL 10 packages for Ubuntu 18.04
apt:
name: postgresql
state: present
when: (ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] == "18" and use_distribution_postgresql)
...
---
- name: Configure PostgreSQL listen address and authorization in pg_hba.conf on CentOS
block:
- name: Configure PostgreSQL listen address on CentOS
lineinfile:
path: "{{ path_to_icat }}/postgresql.conf"
regexp: '^#listen_addresses ='
line: 'listen_addresses = {{ ansible_nodename }}'
backrefs: yes
backup: yes
- name: Set authorization in pg_hba.conf on CentOS
postgresql_pg_hba:
dest: "{{ path_to_icat }}/pg_hba.conf"
databases: "{{ database_name }}"
users: "{{ database_user }}"
address: "{{ provider_ip }}/24"
contype: host
method: trust
when: (ansible_facts['distribution'] == "CentOS")
- name: Configure PostgreSQL listen address and authorization in pg_hba.conf on Ubuntu
block:
# - name: Configure hostname on Ubuntu
# hostname:
# name: "{{ ansible_hostname }}.{{ domain_name }}"
# use: systemd
# - name: Configure /etc/hosts on Ubuntu
# lineinfile:
# path: /etc/hosts
# insertafter: EOF
# line: "{{ ansible_default_ipv4.address }}\t{{ ansible_nodename }}\t{{ ansible_hostname }}"
# backup: yes
# state: present
- name: Configure PostgreSQL listen address on Ubuntu
lineinfile:
path: "/etc/postgresql/{{ postgresql_version.version.major }}/{{ cluster_name }}/postgresql.conf"
regexp: '^#listen_addresses ='
line: 'listen_addresses = {{ ansible_nodename }}'
backrefs: yes
backup: yes
- name: Set autorization in pg_hba.conf on Ubuntu
postgresql_pg_hba:
dest: /etc/postgresql/{{ postgresql_version.version.major }}/{{ cluster_name }}/pg_hba.conf
databases: "{{ database_name }}"
users: "{{ database_user }}"
address: "{{ provider_ip }}/24"
contype: host
method: trust
when: (ansible_facts['distribution'] == "Ubuntu")
...
---
- name: Install Python modules
pip:
name:
- pip
- psycopg2-binary
state: latest
...
---
- name: Install external PostgreSQL repository for CentOS 7
yum:
conf_file: '/etc/yum.conf'
name: https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
state: present
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7" and not use_distribution_postgresql)
- name: Install external PostgreSQL repository for CentOS 8
dnf:
conf_file: '/etc/dnf/dnf.conf'
name: https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm
state: present
disable_gpg_check: yes
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "8" and not use_distribution_postgresql)
...
---
- name: Set SELinux mode on CentOS
selinux:
policy: targeted
state: "{{ selinux_mode }}"
when: ansible_facts['distribution'] == "CentOS"
- name: Allow PostgreSQL to modify files in "{{ path_to_icat }}" on CentOS
sefcontext:
target: '{{ path_to_icat }}(/.*)?'
setype: postgresql_db_t
state: present
when: ansible_facts['distribution'] == "CentOS"
- name: Apply new SELinux file context to filesystem
command: restorecon -irv "{{ path_to_icat }}"
when: ansible_facts['distribution'] == "CentOS"
...
---
# Dynamically set some variables commonly used by this task group.
- name: Setting facts for systemd operations.
vars:
external_postgres_suffix: "-{{ external_postgresql_version }}"
# Suffix will be '-version', e.g. '-13' for postgresql v13.x.x, or '' (nothing) if we do use the version distributed by the standard repository.
suffix: "{{ ( use_distribution_postgresql == false ) | ternary(external_postgres_suffix, '') }}"
set_fact:
postgres_unit_file_name: "postgresql{{ suffix }}.service"
# This task will place an 'override' for the systemd unit file distributed by postgresql.
# Modifying the original unit file is not recomended as it is replaced with each update of postgresql.
#
# Its current purpose is to define the path to the data directory where the database's actual data will be on the system.
# cf. The template file.
- name: "Define the {{ postgres_unit_file_name }} override file."
template:
src: templates/postgresql.service.j2
dest: "/etc/systemd/system/{{ postgres_unit_file_name }}"
owner: root
group: wheel
mode: '0644'
when:
- path_to_icat is defined
- path_to_icat != None
- path_to_icat | length > 0
- name: "Start {{ postgres_unit_file_name }}"
systemd:
name: "{{ postgres_unit_file_name }}"
state: started
daemon_reload: yes
enabled: yes
...
---
- name: Create UNIX iRODS group
group:
name: "{{ irods_unix_group_name }}"
gid: "{{ irods_unix_group_id }}"
state: present
- name: Create UNIX iRODS user
user:
name: "{{ irods_unix_user_name }}"
uid: "{{ irods_unix_user_id }}"
group: irods
state: present
password: "{{irods_unix_password}}"
#password: see docs.ansible.com/ansible/latest/user_module.html
- name: Create postgres user and group on CentOS 7
block:
- name: Create UNIX postgres group
group:
name: "{{ postgresql_unix_group_name }}"
state: present
system: yes
- name: Create UNIX postgres user
user:
name: "{{ postgresql_unix_user_name }}"
group: "{{ postgresql_unix_group_name }}"
state: present
system: yes
shell: /usr/bin/false
create_home: no
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7")
...
---
- name: Collect and print PostgreSQL version on Ubuntu
block:
- name: Collect PostgreSQL version
become: yes
become_user: postgres
postgresql_info:
filter: ver*
register: postgresql_version
- name: Print PostgreSQL version
debug:
var: postgresql_version.version.major
when: (ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] == "18")
...
---
- name: Check that the database doesn't exist yet on CentOS
stat:
path: "{{path_to_icat}}/postgresql.conf"
register: stat_result_centos
when: ansible_facts['distribution'] == "CentOS"
- name: Check that the database doesn't exist yet on Debian
stat:
path: "/etc/postgresql/{{ postgresql_version.version.major }}/{{ cluster_name }}/postgresql.conf"
register: stat_result_debian
when: ansible_facts['distribution'] == "Debian"
- name: Check that the database doesn't exist yet on Ubuntu
stat:
path: "/etc/postgresql/{{ postgresql_version.version.major }}/{{ cluster_name }}/postgresql.conf"
register: stat_result_ubuntu
when: ansible_facts['distribution'] == "Ubuntu"
- name: Initialization of ICAT database on CentOS 7
command: /usr/pgsql-{{ external_postgresql_version }}/bin/initdb -D {{path_to_icat}}
become: true
become_user: postgres
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7" and stat_result_centos.stat.exists == False and not use_distribution_postgresql)
- name: Initialization of ICAT database on CentOS 7
command: /usr/bin/initdb -D {{path_to_icat}}
become: true
become_user: postgres
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7" and stat_result_centos.stat.exists == False and use_distribution_postgresql)
- name: Initialization of ICAT database on CentOS 8
command: /usr/pgsql-{{ external_postgresql_version }}/bin/initdb -D {{path_to_icat}}
become: true
become_user: postgres
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "8" and stat_result_centos.stat.exists == False and not use_distribution_postgresql)
- name: Initialization of ICAT database on CentOS 8
command: /usr/bin/initdb -D {{path_to_icat}}
become: true
become_user: postgres
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "8" and stat_result_centos.stat.exists == False and use_distribution_postgresql)
- name: Initialization of ICAT database on Debian 10
command: /usr/bin/pg_createcluster -d {{ path_to_icat }} {{ postgresql_version.version.major }} {{ cluster_name }}
become: true
become_user: postgres
when: (ansible_facts['distribution'] == "Debian" and ansible_facts['distribution_major_version'] == "10" and stat_result_debian.stat.exists == False)
- name: Initialization of ICAT database on Ubuntu 18.04
command: /usr/bin/pg_createcluster -d {{ path_to_icat }} {{ postgresql_version.version.major }} {{ cluster_name }}
become: true
become_user: postgres
when: (ansible_facts['distribution'] == "Ubuntu" and ansible_facts['distribution_major_version'] == "18" and stat_result_ubuntu.stat.exists == False)
...
---
- name: Create PostgreSQL iRODS user
postgresql_user:
name: "{{ database_user }}"
state: present
password: "{{ database_password }}"
login_user: postgres
become: true
become_user: postgres
- name: Create PostgreSQL iRODS database
postgresql_db:
name: "{{ database_name }}"
state: present
login_user: postgres
become: true
become_user: postgres
- name: Set database privileges
postgresql_privs:
database: "{{ database_name }}"
state: present
privs: ALL
type: database
roles: "{{ database_user }}"
login_user: postgres
become: true
become_user: postgres
...
---
- name: Create database directory
file:
state: directory
path: "{{path_to_icat}}"
owner: postgres
group: postgres
mode: 0700
recurse: yes
...
---
# tasks file for postgresql
- import_tasks: repos.yml
- import_tasks: packages.yml
- import_tasks: pip.yml
- import_tasks: users.yml
- import_tasks: files.yml
- import_tasks: db_info.yml
- import_tasks: db_init.yml
- import_tasks: patches.yml
- import_tasks: security.yml
- import_tasks: systemd.yml
- import_tasks: db_privs.yml
...
---
- name: Install EPEL repository
yum:
conf_file: '/etc/yum.conf'
name:
- epel-release
state: present
update_cache: yes
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7" and use_external_epel)
- name: Install ancillary packages for CentOS 7
yum:
conf_file: '/etc/yum.conf'
name:
- libselinux-python
- python-pip
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7")
- name: Install PostgreSQL {{ external_postgresql_version }} packages for CentOS 7
yum:
conf_file: '/etc/yum.conf'
name:
- 'postgresql{{ external_postgresql_version | replace(".", "") }}'
- 'postgresql{{ external_postgresql_version | replace(".", "") }}-server'
state: present
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7" and not use_distribution_postgresql)
- name: Install PostgreSQL 9.2 packages for CentOS 7
yum:
conf_file: '/etc/yum.conf'
name:
- postgresql
- postgresql-server
state: present
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7" and use_distribution_postgresql)
- name: Install ancillary packages for CentOS 8
dnf:
conf_file: '/etc/dnf/dnf.conf'
name:
- python3-libselinux
- python3-pip
state: present
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "8")
- name: Disable the built-in PostgreSQL module
command: dnf -qy module disable postgresql
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "8" and not use_distribution_postgresql)
- name: Install PostgreSQL {{ external_postgresql_version }} packages for CentOS 8
dnf:
conf_file: '/etc/dnf/dnf.conf'
name:
- 'postgresql{{ external_postgresql_version | replace(".", "") }}'
- 'postgresql{{ external_postgresql_version | replace(".", "") }}-server'
state: present
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "8" and not use_distribution_postgresql)
- name: Install PostgreSQL 10 packages for CentOS 8
dnf:<