courseworkrep/install_postgresql.yml

99 lines
3.0 KiB
YAML
Raw Normal View History

2024-10-20 10:09:22 +00:00
---
- name: Install PostgreSQL on openSUSE Leap
hosts: postgres_servers
become: yes
vars:
postgres_user: "{{ postgres_user }}"
postgres_password: "{{ postgres_password }}"
postgres_db: "{{ postgres_db }}"
2024-10-20 10:35:21 +00:00
tasks:
2024-10-20 10:35:21 +00:00
- name: Update zypper
command: zypper refresh
register: zypper_refresh
changed_when: "'Refreshing' in zypper_refresh.stdout"
2024-10-20 10:35:21 +00:00
- name: Update System
zypper:
name: '*'
state: latest
when: zypper_refresh.changed
2024-10-20 10:35:21 +00:00
- name: Update PostgreSQL package
zypper:
name:
- postgresql-server
- postgresql-contrib
state: present
2024-10-20 10:35:21 +00:00
- name: PostgreSQL initdb
command: sudo -u postgres initdb -D /var/lib/pgsql/data
args:
creates: /var/lib/pgsql/data/PG_VERSION
2024-10-20 10:35:21 +00:00
- name: Systemctl start and enable PostgreSQL
service:
name: postgresql
state: started
enabled: yes
2024-10-20 10:35:21 +00:00
- name: python3-psycopg2 install
zypper:
name: python3-psycopg2
state: present
2024-10-20 10:35:21 +00:00
- name: Change listen_addresses in postgresql.conf
lineinfile:
path: /var/lib/pgsql/data/postgresql.conf
regexp: '^#?listen_addresses\s*='
line: "listen_addresses = '*'"
notify: Restart PostgreSQL
2024-10-20 10:35:21 +00:00
- name: Change pg_hba.conf
lineinfile:
path: /var/lib/pgsql/data/pg_hba.conf
regexp: '^host\s+all\s+all\s+0\.0\.0\.0/0\s+md5'
line: "host all all 0.0.0.0/0 md5"
notify: Restart PostgreSQL
2024-10-20 10:35:21 +00:00
- name: Create User PostgreSQL
community.postgresql.postgresql_user:
name: "{{ postgres_user }}"
password: "{{ postgres_password }}"
state: present
2024-10-20 10:35:21 +00:00
- name: Create Base PostgreSQL
community.postgresql.postgresql_db:
name: "{{ postgres_db }}"
owner: "{{ postgres_user }}"
encoding: UTF8
state: present
2024-10-20 10:35:21 +00:00
- name: Create a table for storing names and phone numbers
community.postgresql.postgresql_query:
db: "{{ postgres_db }}"
query: >
CREATE TABLE IF NOT EXISTS contacts (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
phone_number VARCHAR(15)
);
login_user: "{{ postgres_user }}"
login_password: "{{ postgres_password }}"
- name: Открыть порт PostgreSQL 5432 в файрволе
command: firewall-cmd --add-port=5432/tcp --permanent
become: yes
2024-10-20 10:35:21 +00:00
- name: Перезагрузить файрвол для применения изменений
command: firewall-cmd --reload
become: yes
2024-10-20 10:35:21 +00:00
handlers:
- name: Restart PostgreSQL
service:
name: postgresql
state: restarted