diff --git a/hosts.ini b/hosts.ini new file mode 100644 index 0000000..fdc31b9 --- /dev/null +++ b/hosts.ini @@ -0,0 +1,2 @@ +[postgres_servers] +192.168.0.72 ansible_user=root ansible_password='XSW@1qaz' ansible_connection=ssh diff --git a/install_postgresql.yml b/install_postgresql.yml new file mode 100644 index 0000000..7c80612 --- /dev/null +++ b/install_postgresql.yml @@ -0,0 +1,89 @@ +--- +- name: Установка и настройка PostgreSQL + hosts: postgres_servers + become: yes + vars: + postgres_user: myuser + postgres_password: mypassword + postgres_db: mydatabase + tasks: + + - name: Обновление zypper + command: zypper refresh + register: zypper_refresh + changed_when: "'Refreshing' in zypper_refresh.stdout" + + - name: Обновление системы + zypper: + name: '*' + state: latest + when: zypper_refresh.changed + + - name: Установка пакетов PostgreSQL + zypper: + name: + - postgresql-server + - postgresql-contrib + state: present + + - name: Инициализация базы данных PostgreSQL + command: /usr/bin/postgresql-setup initdb + args: + creates: /var/lib/pgsql/data/PG_VERSION + when: ansible_facts['pkg_mgr'] == 'zypper' + + - name: Обеспечение запуска и автозапуска службы PostgreSQL + service: + name: postgresql + state: started + enabled: yes + + - name: Установка python + zypper: + name: python3-psycopg2 + state: present + + - name: Настройка listen_addresses в postgresql.conf + lineinfile: + path: /var/lib/pgsql/data/postgresql.conf + regexp: '^#?listen_addresses\s*=' + line: "listen_addresses = '*'" + notify: Перезапуск PostgreSQL + + - name: Настройка 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: Перезапуск PostgreSQL + + - name: Создание пользователя PostgreSQL + community.postgresql.postgresql_user: + name: "{{ postgres_user }}" + password: "{{ postgres_password }}" + state: present + + + - name: Создание базы данных PostgreSQL + community.postgresql.postgresql_db: + name: "{{ postgres_db }}" + owner: "{{ postgres_user }}" + encoding: UTF8 + state: present + + - name: Создание таблицы + 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: Перезапуск PostgreSQL + service: + name: postgresql + state: restarted