Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ postgresql_user_privileges:
db: foobar # database
priv: "ALL" # privilege string format: example: INSERT,UPDATE/table:SELECT/anothertable:ALL
role_attr_flags: "CREATEDB" # role attribute flags

# List of object privileges to be applied (optional)
postgresql_privileges:
- db: foobar
obj: table1
role: baz
priv: SELECT,INSERT,UPDATE
schema: public
- db: foobar
obj: public
role: baz
state: absent # revoke privilege
type: schema # on all objects in schema
priv: INSERT,UPDATE
```

There's a lot more knobs and bolts to set, which you can find in the defaults/main.yml
Expand Down
3 changes: 3 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ postgresql_users: []
# List of user privileges to be applied (optional)
postgresql_user_privileges: []

# List of object privileges to be applied (optional)
postgresql_privileges: []

# pg_hba.conf
postgresql_pg_hba_default:
- { type: local, database: all, user: '{{ postgresql_admin_user }}', address: '', method: '{{ postgresql_default_auth_method }}', comment: '' }
Expand Down
28 changes: 28 additions & 0 deletions tasks/users_privileges.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# file: postgresql/tasks/users_privileges.yml

- name: PostgreSQL | Ensure PostgreSQL is running
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you want to check postgresql status here? It's not a task about privileges.

service:
name: "{{ postgresql_service_name }}"
state: started

- name: PostgreSQL | Update the user privileges
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this task should be removed, because postgresql_privs can do all needed actions.

postgresql_user:
name: "{{item.name}}"
Expand All @@ -14,3 +19,26 @@
become_user: "{{postgresql_admin_user}}"
with_items: "{{postgresql_user_privileges}}"
when: postgresql_users|length > 0

# Iterate over postgresql_privileges to grant and revoke privileges
# on objects using the built in module
# http://docs.ansible.com/ansible/postgresql_privs_module.html
- name: PostgreSQL | Update the privileges
postgresql_privs:
db: "{{item.db}}"
login_host: "{{item.host | default(omit)}}"
login_user: "{{postgresql_admin_user}}"
port: "{{postgresql_port}}"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove empty lines.

grant_option: "{{item.grant_option | default(omit)}}"
obj: "{{item.obj | default(omit)}}"
priv: "{{item.priv | default(omit)}}"
role: "{{item.role}}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use roles name, because it can be comma-separated list of Roles.

schema: "{{item.schema | default(omit)}}"

state: "{{item.state | default(omit)}}"
type: "{{item.type | default(omit)}}"
become: yes
become_user: "{{postgresql_admin_user}}"
with_items: "{{postgresql_privileges}}"
when: "{{postgresql_privileges|length > 0}}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right way: drop when condition and use
with_items: "{{ postgresql_user_privileges | default([]) }}"

8 changes: 8 additions & 0 deletions tests/vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,11 @@ postgresql_users:
postgresql_user_privileges:
- name: baz
db: foobar

postgresql_privileges:
- db: foobar
obj: public
role: baz
state: present
type: schema
priv: CREATE,USAGE