postgresql_privs - Grant or revoke privileges on PostgreSQL database objects.

Author:Bernhard Weitzhofer

Synopsis

New in version 1.2.

Grant or revoke privileges on PostgreSQL database objects. This module is basically a wrapper around most of the functionality of PostgreSQL’s GRANT and REVOKE statements with detection of changes (GRANT/REVOKE privs ON type objs TO/FROM roles)

Options

parameter required default choices comments
database yes
    Name of database to connect to.Alias: db
    grant_option no
    • yes
    • no
    Whether role may grant/revoke the specified privileges/group memberships to others.Set to no to revoke GRANT OPTION, leave unspecified to make no changes.grant_option only has an effect if state is present.Alias: admin_option
    host no
      Database host address. If unspecified, connect via Unix socket.Alias: login_host
      login no postgres
        The username to authenticate with.Alias: login_user
        objs no
          Comma separated list of database objects to set privileges on.If type is table or sequence, the special value ALL_IN_SCHEMA can be provided instead to specify all database objects of type type in the schema specified via schema. (This also works with PostgreSQL < 9.0.)If type is database, this parameter can be omitted, in which case privileges are set for the database specified via database.If type is function, colons (":") in object names will be replaced with commas (needed to specify function signatures, see examples)Alias: obj
          password no
            The password to authenticate with.Alias: login_password)
            port no 5432
              Database port to connect to.
              privs no
                Comma separated list of privileges to grant/revoke.Alias: priv
                roles yes
                  Comma separated list of role (user/group) names to set permissions for.The special value PUBLIC can be provided instead to set permissions for the implicitly defined PUBLIC group.Alias: role
                  schema no
                    Schema that contains the database objects specified via objs.May only be provided if type is table, sequence or function. Defaults to public in these cases.
                    state no present
                    • present
                    • absent
                    If present, the specified privileges are granted, if absent they are revoked.
                    type no table
                    • table
                    • sequence
                    • function
                    • database
                    • schema
                    • language
                    • tablespace
                    • group
                    Type of database object to set privileges on.

                    Note

                    Requires psycopg2

                    Examples


                    # On database "library":
                    # GRANT SELECT, INSERT, UPDATE ON TABLE public.books, public.authors
                    # TO librarian, reader WITH GRANT OPTION
                    - postgresql_privs: >
                        database=library
                        state=present
                        privs=SELECT,INSERT,UPDATE
                        type=table
                        objs=books,authors
                        schema=public
                        roles=librarian,reader
                        grant_option=yes
                    
                    # Same as above leveraging default values:
                    - postgresql_privs: >
                        db=library
                        privs=SELECT,INSERT,UPDATE
                        objs=books,authors
                        roles=librarian,reader
                        grant_option=yes
                    
                    # REVOKE GRANT OPTION FOR INSERT ON TABLE books FROM reader
                    # Note that role "reader" will be *granted* INSERT privilege itself if this
                    # isn't already the case (since state=present).
                    - postgresql_privs: >
                        db=library
                        state=present
                        priv=INSERT
                        obj=books
                        role=reader
                        grant_option=no
                    
                    # REVOKE INSERT, UPDATE ON ALL TABLES IN SCHEMA public FROM reader
                    # "public" is the default schema. This also works for PostgreSQL 8.x.
                    - postgresql_privs: >
                        db=library
                        state=absent
                        privs=INSERT,UPDATE
                        objs=ALL_IN_SCHEMA
                        role=reader
                    
                    # GRANT ALL PRIVILEGES ON SCHEMA public, math TO librarian
                    - postgresql_privs: >
                        db=library
                        privs=ALL
                        type=schema
                        objs=public,math
                        role=librarian
                    
                    # GRANT ALL PRIVILEGES ON FUNCTION math.add(int, int) TO librarian, reader
                    # Note the separation of arguments with colons.
                    - postgresql_privs: >
                        db=library
                        privs=ALL
                        type=function
                        obj=add(int:int)
                        schema=math
                        roles=librarian,reader
                    
                    # GRANT librarian, reader TO alice, bob WITH ADMIN OPTION
                    # Note that group role memberships apply cluster-wide and therefore are not
                    # restricted to database "library" here.
                    - postgresql_privs: >
                        db=library
                        type=group
                        objs=librarian,reader
                        roles=alice,bob
                        admin_option=yes
                    
                    # GRANT ALL PRIVILEGES ON DATABASE library TO librarian
                    # Note that here "db=postgres" specifies the database to connect to, not the
                    # database to grant privileges on (which is specified via the "objs" param)
                    - postgresql_privs: >
                        db=postgres
                        privs=ALL
                        type=database
                        obj=library
                        role=librarian
                    
                    # GRANT ALL PRIVILEGES ON DATABASE library TO librarian
                    # If objs is omitted for type "database", it defaults to the database
                    # to which the connection is established
                    - postgresql_privs: >
                        db=library
                        privs=ALL
                        type=database
                        role=librarian
                    

                    Note

                    Default authentication assumes that postgresql_privs is run by the postgres user on the remote host. (Ansible’s user or sudo-user).

                    Note

                    This module requires Python package psycopg2 to be installed on the remote host. In the default case of the remote host also being the PostgreSQL server, PostgreSQL has to be installed there as well, obviously. For Debian/Ubuntu-based systems, install packages postgresql and python-psycopg2.

                    Note

                    Parameters that accept comma separated lists (privs, objs, roles) have singular alias names (priv, obj, role).

                    Note

                    To revoke only GRANT OPTION for a specific object, set state to present and grant_option to no (see examples).

                    Note

                    Note that when revoking privileges from a role R, this role may still have access via privileges granted to any role R is a member of including PUBLIC.

                    Note

                    Note that when revoking privileges from a role R, you do so as the user specified via login. If R has been granted the same privileges by another user also, R can still access database objects via these privileges.

                    Note

                    When revoking privileges, RESTRICT is assumed (see PostgreSQL docs).