From c056c76090d2b98951e0c168c9739b4a0841fb0a Mon Sep 17 00:00:00 2001 From: Andrea Dell'Amico Date: Fri, 29 May 2020 16:54:22 +0200 Subject: [PATCH] Management of Postgresql databases and users. --- README.md | 35 +++++++++++------------ defaults/main.yml | 18 +++++++++++- handlers/main.yml | 7 ++++- meta/main.yml | 58 ++++++-------------------------------- tasks/configure-access.yml | 38 +++++++++++++++++++++++++ tasks/db_extensions.yml | 15 ++++++++++ tasks/db_schemas.yml | 14 +++++++++ tasks/main.yml | 10 ++++++- tasks/manage_dbs.yml | 35 +++++++++++++++++++++++ 9 files changed, 160 insertions(+), 70 deletions(-) create mode 100644 tasks/configure-access.yml create mode 100644 tasks/db_extensions.yml create mode 100644 tasks/db_schemas.yml create mode 100644 tasks/manage_dbs.yml diff --git a/README.md b/README.md index 3637db8..8f5338d 100644 --- a/README.md +++ b/README.md @@ -1,31 +1,30 @@ Role Name ========= -A brief description of the role goes here. - -Requirements ------------- - -Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required. +A role that manages PostgreSQL databases and their ACLs. extensions can be installed too. Role Variables -------------- -A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well. +The list of databases and database host. The role is meant to be run using a delegation to the database remote host. + +``` yaml +psql_db_data: + # Example of line needed to create a db, create the user that owns the db, manage the db accesses (used by iptables too). All the fields are mandatory. + - { db_host: '{{ ansible_fqdn }}', pgsql_version: '{{ psql_version }}', name: '{{ psql_db_name }}', encoding: 'UTF8', user: '{{ psql_db_user }}', pwd: '{{ psql_db_pwd }}', roles: 'NOCREATEDB,NOSUPERUSER', extensions: [ 'postgis', 'pgpool_regclass', 'pgpool_recovery' ], allowed_hosts: [ 'xxx.xxx.xxx.xxx/32', 'yyy.yyy.yyy.yyy/32' ], managedb: True } + # Example of line needed to manage the db accesses (used by iptables too), without creating the db and the user. Useful, for example, to give someone access to the postgresql db + - { db_host: '{{ ansible_fqdn }}', pgsql_version: '{{ psql_version }}', name: '{{ psql_db_name }}', user: '{{ psql_db_user }}', allowed_hosts: [ 'xxx.xxx.xxx.xxx/32', 'yyy.yyy.yyy.yyy/32' ], managedb: False } + # Example of line needed to remove a db, create the user that owns the db, manage the db accesses (used by iptables too). All the fields are mandatory. + - { db_host: '{{ ansible_fqdn }}', pgsql_version: '{{ psql_version }}', name: '{{ psql_db_name }}', encoding: 'UTF8', user: '{{ psql_db_user }}', pwd: '{{ psql_db_pwd }}', managedb: True, roles: 'NOCREATEDB,NOSUPERUSER', extensions: [ 'postgis', 'pgpool_regclass', 'pgpool_recovery' ], allowed_hosts: [ 'xxx.xxx.xxx.xxx/32', 'yyy.yyy.yyy.yyy/32' ], state=absent } + +psql_db_extensions: + - { db_host: '{{ ansible_fqdn }}', name: '{{ psql_db_name }}', extensions: [ 'postgis', 'pgpool_regclass', 'pgpool_recovery' ] } +``` Dependencies ------------ -A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles. - -Example Playbook ----------------- - -Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too: - - - hosts: servers - roles: - - { role: username.rolename, x: 42 } +None License ------- @@ -35,4 +34,4 @@ EUPL-1.2 Author Information ------------------ -An optional section for the role authors to include contact information, or a website (HTML is not allowed). +Andrea Dell'Amico, diff --git a/defaults/main.yml b/defaults/main.yml index 95d3c70..53b180e 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,2 +1,18 @@ --- -# defaults file for ansible-role-template \ No newline at end of file +psql_db_port: 5432 +psql_version: 11 +psql_conf_base_dir: '/etc/postgresql' +psql_conf_default_dir: '{{ psql_conf_base_dir }}/{{ psql_version }}/main' +psql_force_ssl_client_connection: False + +#psql_db_data: + # Example of line needed to create a db, create the user that owns the db, manage the db accesses (used by iptables too). All the fields are mandatory. + #- { db_host: '{{ ansible_fqdn }}', pgsql_version: '{{ psql_version }}', name: '{{ psql_db_name }}', encoding: 'UTF8', user: '{{ psql_db_user }}', pwd: '{{ psql_db_pwd }}', roles: 'NOCREATEDB,NOSUPERUSER', extensions: [ 'postgis', 'pgpool_regclass', 'pgpool_recovery' ], allowed_hosts: [ 'xxx.xxx.xxx.xxx/32', 'yyy.yyy.yyy.yyy/32' ], managedb: True } + # Example of line needed to manage the db accesses (used by iptables too), without creating the db and the user. Useful, for example, to give someone access to the postgresql db + #- { db_host: '{{ ansible_fqdn }}', pgsql_version: '{{ psql_version }}', name: '{{ psql_db_name }}', user: '{{ psql_db_user }}', allowed_hosts: [ 'xxx.xxx.xxx.xxx/32', 'yyy.yyy.yyy.yyy/32' ], managedb: False } + # Example of line needed to remove a db, create the user that owns the db, manage the db accesses (used by iptables too). All the fields are mandatory. + #- { db_host: '{{ ansible_fqdn }}', pgsql_version: '{{ psql_version }}', name: '{{ psql_db_name }}', encoding: 'UTF8', user: '{{ psql_db_user }}', pwd: '{{ psql_db_pwd }}', managedb: True, roles: 'NOCREATEDB,NOSUPERUSER', extensions: [ 'postgis', 'pgpool_regclass', 'pgpool_recovery' ], allowed_hosts: [ 'xxx.xxx.xxx.xxx/32', 'yyy.yyy.yyy.yyy/32' ], state=absent } + +#psql_db_extensions: + #- { db_host: '{{ ansible_fqdn }}', name: '{{ psql_db_name }}', extensions: [ 'postgis', 'pgpool_regclass', 'pgpool_recovery' ] } + diff --git a/handlers/main.yml b/handlers/main.yml index 27474e0..810530d 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,2 +1,7 @@ --- -# handlers file for ansible-role-template \ No newline at end of file +- name: Reload postgresql + service: name=postgresql state=reloaded + with_items: '{{ psql_db_data | default([]) }}' + delegate_to: "{{ item.db_host }}" + + diff --git a/meta/main.yml b/meta/main.yml index 1126a5e..79d7126 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,61 +1,21 @@ galaxy_info: - author: your name - description: your description + author: Andrea Dell'Amico + description: Systems Architect company: ISTI-CNR - # If the issue tracker for your role is not on github, uncomment the - # next line and provide a value issue_tracker_url: https://redmine-s2i2s.isti.cnr.it/projects/provisioning - # Some suggested licenses: - # - BSD (default) - # - MIT - # - GPLv2 - # - GPLv3 - # - Apache - # - CC-BY - license: EUPL-1.2 + license: EUPL 1.2+ min_ansible_version: 2.8 - # If this a Container Enabled role, provide the minimum Ansible Container version. - # min_ansible_container_version: - - # Optionally specify the branch Galaxy will use when accessing the GitHub - # repo for this role. During role install, if no tags are available, - # Galaxy will use this branch. During import Galaxy will access files on - # this branch. If Travis integration is configured, only notifications for this - # branch will be accepted. Otherwise, in all cases, the repo's default branch - # (usually master) will be used. - #github_branch: - - # - # Provide a list of supported platforms, and for each platform a list of versions. - # If you don't wish to enumerate all versions for a particular platform, use 'all'. # To view available platforms and versions (or releases), visit: # https://galaxy.ansible.com/api/v1/platforms/ # - # platforms: - # - name: Fedora - # versions: - # - all - # - 25 - # - name: SomePlatform - # versions: - # - all - # - 1.0 - # - 7 - # - 99.99 - - galaxy_tags: [] - # List tags for your role here, one per line. A tag is a keyword that describes - # and categorizes the role. Users find roles by searching for tags. Be sure to - # remove the '[]' above, if you add tags to this list. - # - # NOTE: A tag is limited to a single word comprised of alphanumeric characters. - # Maximum 20 tags per role. - -dependencies: [] - # List your role dependencies here, one per line. Be sure to remove the '[]' above, - # if you add dependencies to this list. + platforms: + - name: Ubuntu + versions: + - bionic + galaxy_tags: + - users diff --git a/tasks/configure-access.yml b/tasks/configure-access.yml new file mode 100644 index 0000000..d1d3ae1 --- /dev/null +++ b/tasks/configure-access.yml @@ -0,0 +1,38 @@ +--- +- name: Manage the pg_hba.conf file + block: + - name: Give access to the remote postgresql client + lineinfile: name={{ psql_conf_base_dir }}/{{ item.0.pgsql_version }}/main/pg_hba.conf regexp="^host.* {{ item.0.name }} {{ item.0.user }} {{ item.1 }}.*$" line="host {{ item.0.name }} {{ item.0.user }} {{ item.1 }} md5" + with_subelements: + - '{{ psql_db_data | default([]) }}' + - allowed_hosts + when: + - psql_db_data is defined + - item.1 is defined + - not psql_force_ssl_client_connection + notify: Reload postgresql + + - name: Give access to the remote postgresql client, enforce ssl + lineinfile: name={{ psql_conf_base_dir }}/{{ item.0.pgsql_version }}/main/pg_hba.conf regexp="^host.* {{ item.0.name }} {{ item.0.user }} {{ item.1 }}.*$" line="hostssl {{ item.0.name }} {{ item.0.user }} {{ item.1 }} md5" + with_subelements: + - '{{ psql_db_data | default([]) }}' + - allowed_hosts + when: + - psql_db_data is defined + - item.1 is defined + - psql_force_ssl_client_connection + notify: Reload postgresql + + delegate_to: "{{ item.0.db_host }}" + when: item.0.db_host is defined + tags: [ 'postgresql', 'postgres', 'pg_hba', 'pg_db' ] + +- name: Manage the permissions of the pg_hba.conf file + block: + - name: Set the correct permissions to the pg_hba.conf file + file: dest={{ psql_conf_base_dir }}/{{ item.pgsql_version }}/main/pg_hba.conf owner=root group=postgres mode=0640 + with_items: '{{ psql_db_data | default([]) }}' + + delegate_to: "{{ item.db_host }}" + when: item.db_host is defined + tags: [ 'postgresql', 'postgres', 'pg_hba', 'pg_conf', 'pg_db' ] diff --git a/tasks/db_extensions.yml b/tasks/db_extensions.yml new file mode 100644 index 0000000..e18fa51 --- /dev/null +++ b/tasks/db_extensions.yml @@ -0,0 +1,15 @@ +--- +- block: + - name: Add postgres extensions to the databases, if needed + become: True + become_user: postgres + postgresql_ext: name={{ item.1 | default(omit) }} db={{ item.0.name }} port={{ psql_db_port }} + with_subelements: + - '{{ psql_db_extensions | default([]) }}' + - extensions + when: psql_db_extensions is defined + + delegate_to: "{{ item.0.db_host }}" + when: item.0.db_host is defined + tags: [ 'postgresql', 'postgres', 'pg_extensions', 'pg_db' ] + diff --git a/tasks/db_schemas.yml b/tasks/db_schemas.yml new file mode 100644 index 0000000..d97cb3a --- /dev/null +++ b/tasks/db_schemas.yml @@ -0,0 +1,14 @@ +--- +- block: + - name: Add schemas to a database. + become: True + become_user: postgres + postgresql_schema: database={{ item.0.name }} port={{ psql_db_port }} name={{ item.1 }} owner={{ item.0.user }} state={{ item.0.schemastate | default('present') }} + with_subelements: + - '{{ psql_db_schemas | default([]) }}' + - schema + when: psql_db_schemas is defined + + delegate_to: "{{ item.0.db_host }}" + when: item.0.db_host is defined + tags: [ 'postgresql', 'postgres', 'pg_db', 'pg_schema' ] diff --git a/tasks/main.yml b/tasks/main.yml index 53c6cae..f300e04 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,2 +1,10 @@ --- -# tasks file for ansible-role-template \ No newline at end of file +- import_tasks: configure-access.yml + when: psql_db_data is defined +- import_tasks: manage_dbs.yml + when: psql_db_data is defined +- import_tasks: db_schemas.yml + when: psql_db_schemas is defined +- import_tasks: db_extensions.yml + when: psql_db_extensions is defined + diff --git a/tasks/manage_dbs.yml b/tasks/manage_dbs.yml new file mode 100644 index 0000000..a4b5351 --- /dev/null +++ b/tasks/manage_dbs.yml @@ -0,0 +1,35 @@ +--- +- block: + - name: Add a user for the postgresql DBs + become: True + become_user: postgres + postgresql_user: user={{ item.user }} password={{ item.pwd }} role_attr_flags={{ item.roles }} port={{ psql_db_port }} state={{ item.userstate | default('present') }} + with_items: '{{ psql_db_data | default([]) }}' + when: item.roles is defined + + - name: Add the databases with the correct owner. Or remove them, if not used anymore + become: True + become_user: postgres + postgresql_db: db={{ item.name }} port={{ psql_db_port }} encoding={{ item.encoding }} owner={{ item.user }} template=template0 state={{ item.state | default('present') }} + with_items: '{{ psql_db_data | default([]) }}' + when: item.managedb | default(True) + + - name: Manage users privileges + become: True + become_user: postgres + postgresql_privs: db={{ item.name }} privs={{ item.privs }} type=database roles={{ item.roles }} port={{ psql_db_port }} state={{ item.userstate | default('present') }} + with_items: '{{ psql_db_privs | default([]) }}' + when: psql_db_privs is defined + + - name: Define a user with password, with no associated DBs + become: True + become_user: postgres + postgresql_user: user={{ item.user }} password={{ item.pwd }} port={{ psql_db_port }} + with_items: '{{ psql_db_data | default([]) }}' + when: + - item.pwd is defined + - item.roles is not defined + + delegate_to: "{{ item.db_host }}" + when: item.db_host is defined + tags: [ 'postgresql', 'postgres', 'pg_db', 'pg_user' ]