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

Adds or removes a users (roles) from a PostgreSQL database.

| "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.5.15

Description

Add or remove PostgreSQL users (roles) from a remote host and, optionally, grant the users access to an existing database or tables.

The fundamental function of the module is to create, or delete, roles from a PostgreSQL cluster. 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.
# Create django user and grant access to database and products table
- postgresql_user:
    db: acme
    name: django
    password: ceec4eif7ya
    priv: "CONNECT/products:ALL"
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Create rails user, grant privilege to create other databases and demote rails from super user status
- postgresql_user:
    name: rails
    password: secret
    role_attr_flags: CREATEDB,NOSUPERUSER
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Remove test user privileges from acme
- 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.
# Remove test user from test database and the cluster
- postgresql_user:
    db: test
    name: test
    priv: ALL
    state: absent
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Set user's password with no expire date
- postgresql_user:
    db: acme
    name: django
    password: mysupersecretword
    priv: "CONNECT/products:ALL"
    expire: infinity
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Example privileges string format
# INSERT,UPDATE/table:SELECT/anothertable:ALL

# Remove an existing user's password
- postgresql_user:
    db: test
    user: test
    password: NULL

Inputs

    
db:
    default: null
    description:
    - name of database where permissions will be granted

name:
    default: null
    description:
    - name of the user (role) to add or remove
    required: true

port:
    default: 5432
    description:
    - Database port to connect to.

priv:
    default: null
    description:
    - 'PostgreSQL privileges string in the format: C(table:priv1,priv2)'

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

expires:
    default: null
    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.
    version_added: '1.4'
    version_added_collection: ansible.builtin

password:
    default: null
    description:
    - set the user's password, before 1.4 this was required.
    - 'When passing an encrypted password, the encrypted parameter must also be true,
      and 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\" | md5`\"). Note that if the provided password
      string is already in MD5-hashed format, then it is used as-is, regardless of encrypted
      parameter.

      '

ssl_mode:
    choices:
    - disable
    - allow
    - 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.
    version_added: '2.3'
    version_added_collection: ansible.builtin

encrypted:
    default: false
    description:
    - whether the password is stored hashed in the database. boolean. Passwords can be
      passed already hashed or unhashed, and postgresql ensures the stored password is
      hashed when encrypted is set.
    version_added: '1.4'
    version_added_collection: ansible.builtin

conn_limit:
    default: null
    description:
    - Specifies the user connection limit.
    version_added: '2.4'
    version_added_collection: ansible.builtin

login_host:
    default: localhost
    description:
    - Host running PostgreSQL.

login_user:
    default: postgres
    description:
    - User (role) used to authenticate with PostgreSQL

fail_on_user:
    choices:
    - true
    - false
    default: 'yes'
    description:
    - if C(yes), fail when user can't be removed. Otherwise just log and continue

ssl_rootcert:
    default: null
    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.
    version_added: '2.3'
    version_added_collection: ansible.builtin

login_password:
    default: null
    description:
    - Password used to authenticate with PostgreSQL

role_attr_flags:
    choices:
    - '[NO]SUPERUSER'
    - '[NO]CREATEROLE'
    - '[NO]CREATEDB'
    - '[NO]INHERIT'
    - '[NO]LOGIN'
    - '[NO]REPLICATION'
    - '[NO]BYPASSRLS'
    default: ''
    description:
    - 'PostgreSQL role attributes string in the format: CREATEDB,CREATEROLE,SUPERUSER'
    - Note that '[NO]CREATEUSER' is deprecated.

login_unix_socket:
    default: null
    description:
    - Path to a Unix domain socket for local connections

no_password_changes:
    choices:
    - true
    - false
    default: 'no'
    description:
    - if C(yes), don't inspect database for password changes. Effective when C(pg_authid)
      is not accessible (such as AWS RDS). Otherwise, make password changes as necessary.
    version_added: '2.0'
    version_added_collection: ansible.builtin