diff --git a/roles/postgresql-for-zeitbild_backend/defaults/main.json b/roles/postgresql-for-zeitbild_backend/defaults/main.json new file mode 100644 index 0000000..372ccfe --- /dev/null +++ b/roles/postgresql-for-zeitbild_backend/defaults/main.json @@ -0,0 +1,7 @@ +{ + "var_postgresql_for_zeitbild_username": "zeitbild_user", + "var_postgresql_for_zeitbild_password": "REPLACE_ME", + "var_postgresql_for_zeitbild_schema": "zeitbild", + "var_postgresql_for_zeitbild_git_reference": "master", + "var_postgresql_for_zeitbild_revision": "." +} diff --git a/roles/postgresql-for-zeitbild_backend/tasks/main.json b/roles/postgresql-for-zeitbild_backend/tasks/main.json new file mode 100644 index 0000000..5a8c35b --- /dev/null +++ b/roles/postgresql-for-zeitbild_backend/tasks/main.json @@ -0,0 +1,85 @@ +[ + { + "name": "packages", + "become": true, + "ansible.builtin.apt": { + "update_cache": true, + "pkg": [ + "acl", + "python3-psycopg2" + ] + } + }, + { + "name": "user", + "become": true, + "become_user": "postgres", + "community.postgresql.postgresql_user": { + "state": "present", + "name": "{{var_postgresql_for_zeitbild_username}}", + "password": "{{var_postgresql_for_zeitbild_password}}" + }, + "environment": { + "PGOPTIONS": "-c password_encryption=scram-sha-256" + } + }, + { + "name": "schema", + "become": true, + "become_user": "postgres", + "community.postgresql.postgresql_db": { + "state": "present", + "name": "{{var_postgresql_for_zeitbild_schema}}", + "owner": "{{var_postgresql_for_zeitbild_username}}" + } + }, + { + "name": "rights", + "become": true, + "become_user": "postgres", + "community.postgresql.postgresql_privs": { + "state": "present", + "db": "{{var_postgresql_for_zeitbild_schema}}", + "objs": "ALL_IN_SCHEMA", + "roles": "{{var_postgresql_for_zeitbild_username}}", + "privs": "ALL", + "grant_option": true + } + }, + { + "name": "structure | fetch", + "delegate_to": "localhost", + "ansible.builtin.git": { + "repo": "https://forgejo.linke.sx/zeitbild/datamodel", + "version": "{{var_postgresql_for_zeitbild_git_reference}}", + "dest": "/tmp/zeitbild-datamodel-repo" + } + }, + { + "name": "structure | build", + "delegate_to": "localhost", + "ansible.builtin.shell": { + "chdir": "/tmp/zeitbild-datamodel-repo", + "cmd": "tools/build --revision='{{var_postgresql_for_zeitbild_revision}}' --format='postgresql' > /tmp/zeitbild.sql" + } + }, + { + "name": "structure | transfer", + "ansible.builtin.copy": { + "src": "/tmp/zeitbild.sql", + "dest": "/tmp/zeitbild.sql" + } + }, + { + "name": "structure | apply", + "become": true, + "become_user": "postgres", + "community.postgresql.postgresql_query": { + "db": "{{var_postgresql_for_zeitbild_schema}}", + "login_host": "localhost", + "login_user": "{{var_postgresql_for_zeitbild_username}}", + "login_password": "{{var_postgresql_for_zeitbild_password}}", + "query": "{{lookup('ansible.builtin.file','/tmp/zeitbild.sql')}}" + } + } +] diff --git a/roles/postgresql-for-zeitbild_backend/vardef.json b/roles/postgresql-for-zeitbild_backend/vardef.json new file mode 100644 index 0000000..ad09deb --- /dev/null +++ b/roles/postgresql-for-zeitbild_backend/vardef.json @@ -0,0 +1,18 @@ +{ + "username": { + "type": "string", + "mandatory": false + }, + "password": { + "type": "string", + "mandatory": true + }, + "schema": { + "type": "string", + "mandatory": false + }, + "git_reference": { + "type": "string", + "mandatory": false + } +}