ansible_playbooks/roles/postgres_install/tasks/main.yml
2026-07-21 14:37:34 +03:00

120 lines
3.5 KiB
YAML
Executable file

---
- name: Update apt cache
ansible.builtin.apt:
update_cache: true
changed_when: false
- name: Install postgresql-common
ansible.builtin.apt:
name: postgresql-common
state: present
- name: Install PostgreSQL {{ postgres_install_postgresql_version }} and contrib
ansible.builtin.apt:
name:
- "postgresql-{{ postgres_install_postgresql_version }}"
- "postgresql-contrib-{{ postgres_install_postgresql_version }}"
- apache2
- python3-pip
- python3-psycopg2
state: present
- name: Ensure PostgreSQL service is running and enabled
ansible.builtin.systemd:
name: postgresql
state: started
enabled: true
ignore_errors: "{{ ansible_check_mode }}"
- name: Configure postgresql.conf (listen_addresses and port)
ansible.builtin.lineinfile:
path: "{{ postgres_install_postgresql_conf_path }}"
regexp: "^#?listen_addresses\\s*="
line: "listen_addresses = '{{ postgres_install_postgresql_listen_addresses }}'"
notify: Restart postgresql
ignore_errors: "{{ ansible_check_mode }}"
- name: Configure postgresql.conf (port)
ansible.builtin.lineinfile:
path: "{{ postgres_install_postgresql_conf_path }}"
regexp: "^#?port\\s*="
line: "port = {{ postgresql__install_postgresql_port }}"
notify: Restart postgresql
ignore_errors: "{{ ansible_check_mode }}"
- name: Deploy pg_hba.conf from template
ansible.builtin.template:
src: pg_hba.conf.j2
dest: "{{ postgres_install_pg_hba_conf_path }}"
owner: postgres
group: postgres
mode: '0640'
notify: Restart postgresql
ignore_errors: "{{ ansible_check_mode }}"
- name: Create database user
community.postgresql.postgresql_user:
name: "{{ db_user }}"
password: "{{ db_password }}"
role_attr_flags: CREATEDB,LOGIN
become_user: postgres
ignore_errors: "{{ ansible_check_mode }}"
- name: Create database
community.postgresql.postgresql_db:
name: "{{ db_name }}"
encoding: UTF8
lc_ctype: "{{ postgres_install_postgresql_locale }}"
lc_collate: "{{ postgres_install_postgresql_locale }}"
become_user: postgres
ignore_errors: "{{ ansible_check_mode }}"
- name: Disable angie
ansible.builtin.systemd:
name: angie
state: stopped
enabled: false
- name: Download and install PGAdmin GPG key
ansible.builtin.get_url:
url: https://www.pgadmin.org/static/packages_pgadmin_org.pub
dest: /tmp/packages_pgadmin_org.pub
mode: '0644'
register: gpg_key_download
ignore_errors: "{{ ansible_check_mode }}"
- name: Import PGAdmin GPG key into APT keyring
ansible.builtin.copy:
src: /tmp/packages_pgadmin_org.pub
dest: /tmp/pgadmin_key_for_import.pub
remote_src: true
changed_when: false
check_mode: false
- name: Add key from copied file
ansible.builtin.apt_key:
file: /tmp/pgadmin_key_for_import.pub
keyring: /usr/share/keyrings/packages-pgadmin-org.gpg
ignore_errors: "{{ ansible_check_mode }}"
- name: Add PGAdmin APT repository
ansible.builtin.apt_repository:
repo: >
deb [signed-by=/usr/share/keyrings/packages-pgadmin-org.gpg]
https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/{{ ansible_lsb.codename }}
pgadmin4 main
state: present
filename: pgadmin4
ignore_errors: "{{ ansible_check_mode }}"
- name: Update APT cache after adding repository
ansible.builtin.apt:
update_cache: true
- name: Install pgadmin packages
ansible.builtin.apt:
name:
- pgadmin4
- pgadmin4-web
ignore_errors: "{{ ansible_check_mode }}"
...