diff options
Diffstat (limited to 'roles/postgresql/tasks')
-rw-r--r-- | roles/postgresql/tasks/create_db.yml | 6 | ||||
-rw-r--r-- | roles/postgresql/tasks/create_user.yml | 6 | ||||
-rw-r--r-- | roles/postgresql/tasks/main.yml | 39 |
3 files changed, 51 insertions, 0 deletions
diff --git a/roles/postgresql/tasks/create_db.yml b/roles/postgresql/tasks/create_db.yml new file mode 100644 index 0000000..d2f3676 --- /dev/null +++ b/roles/postgresql/tasks/create_db.yml @@ -0,0 +1,6 @@ +--- + +- name: create database # noqa: no-changed-when + ansible.builtin.command: createdb -U postgres -O "{{ postgres_db_user }}" "{{ postgres_db_name }}" + register: result + failed_when: result.rc != 0 and "already exists" not in result.stderr diff --git a/roles/postgresql/tasks/create_user.yml b/roles/postgresql/tasks/create_user.yml new file mode 100644 index 0000000..793676e --- /dev/null +++ b/roles/postgresql/tasks/create_user.yml @@ -0,0 +1,6 @@ +--- + +- name: create database user # noqa: no-changed-when + ansible.builtin.command: createuser -U postgres "{{ postgres_db_user }}" + register: result + failed_when: result.rc != 0 and "already exists" not in result.stderr diff --git a/roles/postgresql/tasks/main.yml b/roles/postgresql/tasks/main.yml new file mode 100644 index 0000000..a59faa2 --- /dev/null +++ b/roles/postgresql/tasks/main.yml @@ -0,0 +1,39 @@ +--- + +- name: install postgresql + ansible.builtin.package: + name: + - postgresql-server + - postgresql-contrib + state: present + +- name: install python libraries for ansible + ansible.builtin.package: + name: py3-psycopg2 + state: present + +- name: create postgresql data directory + ansible.builtin.file: + path: "{{ postgresql__data_dir }}" + owner: "{{ postgresql__user }}" + group: "{{ postgresql__group }}" + mode: "0700" + state: directory + +- name: init postgresql database # noqa: no-changed-when + ansible.builtin.command: + argv: + - initdb + - "--pgdata={{ postgresql__data_dir }}" + - "--username={{ postgresql__db_user }}" + - --encoding=UTF-8 + - --locale=en_US.UTF-8 + creates: "{{ postgresql__data_dir }}/PG_VERSION" + become: true + become_user: "{{ postgresql__user }}" + +- name: enable and restart postgresql service + ansible.builtin.service: + name: postgresql + state: restarted + enabled: true |