courseworkrep/install_postgresql.yml

88 lines
2.4 KiB
YAML

---
- name: Install PostgreSQL on openSUSE Leap
hosts: postgres_servers
become: yes
vars:
postgres_user: "{{ postgres_user }}"
postgres_password: "{{ postgres_password }}"
postgres_db: "{{ postgres_db }}"
tasks:
- name: Update zypper
command: zypper refresh
register: zypper_refresh
changed_when: "'Refreshing' in zypper_refresh.stdout"
- name: Update System
zypper:
name: '*'
state: latest
when: zypper_refresh.changed
- name: Update PostgreSQL package
zypper:
name:
- postgresql-server
- postgresql-contrib
state: present
- name: PostgreSQL initdb
command: sudo -u postgres initdb -D /var/lib/pgsql/data
args:
creates: /var/lib/pgsql/data/PG_VERSION
- name: Systemctl start and enable PostgreSQL
service:
name: postgresql
state: started
enabled: yes
- name: python3-psycopg2 install
zypper:
name: python3-psycopg2
state: present
- 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
- 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
- name: Create User PostgreSQL
community.postgresql.postgresql_user:
name: "{{ postgres_user }}"
password: "{{ postgres_password }}"
state: present
- name: Create Base PostgreSQL
community.postgresql.postgresql_db:
name: "{{ postgres_db }}"
owner: "{{ postgres_user }}"
encoding: UTF8
state: present
- name: Create table "contacts"
community.postgresql.postgresql_query:
db: "{{ postgres_db }}"
query: |
CREATE TABLE IF NOT EXISTS contacts (
имя VARCHAR(100),
телефон VARCHAR(20)
);
login_user: "{{ postgres_user }}"
login_password: "{{ postgres_password }}"
handlers:
- name: Restart PostgreSQL
service:
name: postgresql
state: restarted