ansible.builtin.postgresql_user (v2.9.24) — module

Add or remove a user (role) from a PostgreSQL server instance

| "added in version" 0.6 of ansible.builtin"

Authors: Ansible Core Team

stableinterface | supported by community

Install Ansible via pip

Install with pip install ansible==2.9.24

Description

Adds or removes a user (role) from a PostgreSQL server instance ("cluster" in PostgreSQL terminology) and, optionally, grants the user access to an existing database or tables.

A user is a role with login privilege.

The fundamental function of the module is to create, or delete, users from a PostgreSQL instances. Privilege assignment, or removal, is an optional step, which works on one database at a time. This allows for the module to be called several times in the same module to modify the permissions on different databases, or to grant permissions to already existing users.

A user cannot be removed until all the privileges have been stripped from the user. In such situation, if the module tries to remove the user it will fail. To avoid this from happening the fail_on_user option signals the module to try to remove the user, but if not possible keep going; the module will report if changes happened and separately if the user was removed or not.


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Connect to acme database, create django user, and grant access to database and products table
  postgresql_user:
    db: acme
    name: django
    password: ceec4eif7ya
    priv: "CONNECT/products:ALL"
    expires: "Jan 31 2020"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Connect to default database, create rails user, set its password (MD5-hashed),
# and grant privilege to create other databases and demote rails from super user status if user exists
- name: Create rails user, set MD5-hashed password, grant privs
  postgresql_user:
    name: rails
    password: md59543f1d82624df2b31672ec0f7050460
    role_attr_flags: CREATEDB,NOSUPERUSER
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Connect to acme database and remove test user privileges from there
  postgresql_user:
    db: acme
    name: test
    priv: "ALL/products:ALL"
    state: absent
    fail_on_user: no
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Connect to test database, remove test user from cluster
  postgresql_user:
    db: test
    name: test
    priv: ALL
    state: absent
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Connect to acme database and set user's password with no expire date
  postgresql_user:
    db: acme
    name: django
    password: mysupersecretword
    priv: "CONNECT/products:ALL"
    expires: infinity
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Example privileges string format
# INSERT,UPDATE/table:SELECT/anothertable:ALL

- name: Connect to test database and remove an existing user's password
  postgresql_user:
    db: test
    user: test
    password: ""
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create user test and grant group user_ro and user_rw to it
  postgresql_user:
    name: test
    groups:
    - user_ro
    - user_rw

Inputs

    
db:
    aliases:
    - login_db
    description:
    - Name of database to connect to and where user's permissions will be granted.
    type: str

name:
    aliases:
    - user
    description:
    - Name of the user (role) to add or remove.
    required: true
    type: str

port:
    aliases:
    - login_port
    default: 5432
    description:
    - Database port to connect to.
    type: int

priv:
    description:
    - 'Slash-separated PostgreSQL privileges string: C(priv1/priv2), where privileges
      can be defined for database ( allowed options - ''CREATE'', ''CONNECT'', ''TEMPORARY'',
      ''TEMP'', ''ALL''. For example C(CONNECT) ) or for table ( allowed options - ''SELECT'',
      ''INSERT'', ''UPDATE'', ''DELETE'', ''TRUNCATE'', ''REFERENCES'', ''TRIGGER'', ''ALL''.
      For example C(table:SELECT) ). Mixed example of this string: C(CONNECT/CREATE/table1:SELECT/table2:INSERT).'
    type: str

state:
    choices:
    - absent
    - present
    default: present
    description:
    - The user (role) state.
    type: str

groups:
    description:
    - The list of groups (roles) that need to be granted to the user.
    elements: str
    type: list
    version_added: '2.9'
    version_added_collection: ansible.builtin

ca_cert:
    aliases:
    - ssl_rootcert
    description:
    - Specifies the name of a file containing SSL certificate authority (CA) certificate(s).
    - If the file exists, the server's certificate will be verified to be signed by one
      of these authorities.
    type: str
    version_added: '2.3'
    version_added_collection: ansible.builtin

expires:
    description:
    - The date at which the user's password is to expire.
    - If set to C('infinity'), user's password never expire.
    - Note that this value should be a valid SQL date and time type.
    type: str
    version_added: '1.4'
    version_added_collection: ansible.builtin

password:
    description:
    - Set the user's password, before 1.4 this was required.
    - Password can be passed unhashed or hashed (MD5-hashed).
    - Unhashed password will automatically be hashed when saved into the database if C(encrypted)
      parameter is set, otherwise it will be save in plain text format.
    - When passing a hashed password it must be generated with the format C('str["md5"]
      + md5[ password + username ]'), resulting in a total of 35 characters. An easy way
      to do this is C(echo "md5$(echo -n 'verysecretpasswordJOE' | md5sum | awk '{print
      $1}')").
    - Note that if the provided password string is already in MD5-hashed format, then
      it is used as-is, regardless of C(encrypted) parameter.
    type: str

ssl_mode:
    choices:
    - allow
    - disable
    - prefer
    - require
    - verify-ca
    - verify-full
    default: prefer
    description:
    - Determines whether or with what priority a secure SSL TCP/IP connection will be
      negotiated with the server.
    - See https://www.postgresql.org/docs/current/static/libpq-ssl.html for more information
      on the modes.
    - Default of C(prefer) matches libpq default.
    type: str
    version_added: '2.3'
    version_added_collection: ansible.builtin

encrypted:
    default: 'yes'
    description:
    - Whether the password is stored hashed in the database.
    - Passwords can be passed already hashed or unhashed, and postgresql ensures the stored
      password is hashed when C(encrypted) is set.
    - 'Note: Postgresql 10 and newer doesn''t support unhashed passwords.'
    - Previous to Ansible 2.6, this was C(no) by default.
    type: bool
    version_added: '1.4'
    version_added_collection: ansible.builtin

conn_limit:
    description:
    - Specifies the user (role) connection limit.
    type: int
    version_added: '2.4'
    version_added_collection: ansible.builtin

login_host:
    default: ''
    description:
    - Host running the database.
    - If you have connection issues when using C(localhost), try to use C(127.0.0.1) instead.
    type: str

login_user:
    default: postgres
    description:
    - The username this module should use to establish its PostgreSQL session.
    type: str

fail_on_user:
    aliases:
    - fail_on_role
    default: 'yes'
    description:
    - If C(yes), fail when user (role) can't be removed. Otherwise just log and continue.
    type: bool

session_role:
    description:
    - Switch to session_role after connecting.
    - The specified session_role must be a role that the current login_user is a member
      of.
    - Permissions checking for SQL commands is carried out as though the session_role
      were the one that had logged in originally.
    type: str
    version_added: '2.8'
    version_added_collection: ansible.builtin

login_password:
    default: ''
    description:
    - The password this module should use to establish its PostgreSQL session.
    type: str

role_attr_flags:
    choices:
    - '[NO]SUPERUSER'
    - '[NO]CREATEROLE'
    - '[NO]CREATEDB'
    - '[NO]INHERIT'
    - '[NO]LOGIN'
    - '[NO]REPLICATION'
    - '[NO]BYPASSRLS'
    description:
    - 'PostgreSQL user attributes string in the format: CREATEDB,CREATEROLE,SUPERUSER.'
    - Note that '[NO]CREATEUSER' is deprecated.
    - To create a simple role for using it like a group, use C(NOLOGIN) flag.
    type: str

login_unix_socket:
    default: ''
    description:
    - Path to a Unix domain socket for local connections.
    type: str

no_password_changes:
    default: 'no'
    description:
    - If C(yes), don't inspect database for password changes. If the user already exists,
      skips all password related checks. Effective when C(pg_authid) is not accessible
      (such as AWS RDS). Otherwise, make password changes as necessary.
    type: bool
    version_added: '2.0'
    version_added_collection: ansible.builtin

Outputs

queries:
  description: List of executed queries.
  returned: always
  sample:
  - CREATE USER "alice"
  - GRANT CONNECT ON DATABASE "acme" TO "alice"
  type: list
  version_added: '2.8'
  version_added_collection: ansible.builtin

See also