Ë
    ÇVh„   ã                   ó€   — d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ d dlmZmZmZmZmZmZ d„ Zed	k(  r e«        y
y
)é    )Úabsolute_importÚdivisionÚprint_functionau  
---
module: postgresql_membership
short_description: Add or remove PostgreSQL roles from groups
description:
- Adds or removes PostgreSQL roles from groups (other roles).
- Users are roles with login privilege.
- Groups are PostgreSQL roles usually without LOGIN privilege.
- "Common use case:"
- 1) add a new group (groups) by M(community.postgresql.postgresql_user) module with I(role_attr_flags=NOLOGIN)
- 2) grant them desired privileges by M(community.postgresql.postgresql_privs) module
- 3) add desired PostgreSQL users to the new group (groups) by this module
options:
  groups:
    description:
    - The list of groups (roles) that need to be granted to or revoked from I(target_roles).
    required: true
    type: list
    elements: str
    aliases:
    - group
    - source_role
    - source_roles
  target_roles:
    description:
    - The list of target roles (groups will be granted to them).
    required: true
    type: list
    elements: str
    aliases:
    - target_role
    - users
    - user
  fail_on_role:
    description:
      - If C(true), fail when group or target_role doesn't exist. If C(false), just warn and continue.
    default: true
    type: bool
  state:
    description:
    - Membership state.
    - I(state=present) implies the I(groups)must be granted to I(target_roles).
    - I(state=absent) implies the I(groups) must be revoked from I(target_roles).
    - I(state=exact) implies that I(target_roles) will be members of only the I(groups)
      (available since community.postgresql 2.2.0).
      Any other groups will be revoked from I(target_roles).
    type: str
    default: present
    choices: [ absent, exact, present ]
  login_db:
    description:
    - Name of database to connect to.
    - The V(db) alias is deprecated and will be removed in version 5.0.0.
    type: str
    aliases:
    - db
  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
  trust_input:
    description:
    - If C(false), check whether values of parameters I(groups),
      I(target_roles), I(session_role) are potentially dangerous.
    - It makes sense to use C(false) only when SQL injections via the parameters are possible.
    type: bool
    default: true
    version_added: '0.2.0'
seealso:
- module: community.postgresql.postgresql_user
- module: community.postgresql.postgresql_privs
- module: community.postgresql.postgresql_owner
- name: PostgreSQL role membership reference
  description: Complete reference of the PostgreSQL role membership documentation.
  link: https://www.postgresql.org/docs/current/role-membership.html
- name: PostgreSQL role attributes reference
  description: Complete reference of the PostgreSQL role attributes documentation.
  link: https://www.postgresql.org/docs/current/role-attributes.html

attributes:
  check_mode:
    support: full

author:
- Andrew Klychkov (@Andersson007)

extends_documentation_fragment:
- community.postgresql.postgres
a¿  
- name: Grant role read_only to alice and bob
  community.postgresql.postgresql_membership:
    group: read_only
    target_roles:
    - alice
    - bob
    state: present

# you can also use target_roles: alice,bob,etc to pass the role list

- name: Revoke role read_only and exec_func from bob. Ignore if roles don't exist
  community.postgresql.postgresql_membership:
    groups:
    - read_only
    - exec_func
    target_role: bob
    fail_on_role: false
    state: absent

- name: >
    Make sure alice and bob are members only of marketing and sales.
    If they are members of other groups, they will be removed from those groups
  community.postgresql.postgresql_membership:
    group:
    - marketing
    - sales
    target_roles:
    - alice
    - bob
    state: exact

- name: Make sure alice and bob do not belong to any groups
  community.postgresql.postgresql_membership:
    group: []
    target_roles:
    - alice
    - bob
    state: exact
a:  
queries:
    description: List of executed queries.
    returned: success
    type: str
    sample: [ "GRANT \"user_ro\" TO \"alice\"" ]
granted:
    description: Dict of granted groups and roles.
    returned: if I(state=present)
    type: dict
    sample: { "ro_group": [ "alice", "bob" ] }
revoked:
    description: Dict of revoked groups and roles.
    returned: if I(state=absent)
    type: dict
    sample: { "ro_group": [ "alice", "bob" ] }
state:
    description: Membership state that tried to be set.
    returned: success
    type: str
    sample: "present"
)ÚAnsibleModule)Úcheck_input)ÚPgMembershipÚconnect_to_dbÚensure_required_libsÚget_conn_paramsÚpg_cursor_argsÚpostgres_common_argument_specc                  óî  — t        «       } | j                  t        dddg d¢¬«      t        dddg d¢¬«      t        dd¬«      t        dd	g d
¢¬«      t        ddgddddœg¬«      t        d¬«      t        dd¬«      ¬«       t        | d¬«      }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }|st        ||||«       t        |«       t        ||j                  d¬«      }t        ||d¬«      \  }	}
 |	j                  d"i t        ¤Ž}t        |||||«      }|d	k(  r|j                  «        n+|dk(  r|j                  «        n|dk(  r|j                  «        |j                  r|	j!                  «        n|	j#                  «        |j%                  «        |	j%                  «        t        |j&                  ||j(                  |j*                  |j,                  ¬«      }|d	k(  r|j.                  |d <   n8|dk(  r|j0                  |d!<   n#|dk(  r|j.                  |d <   |j0                  |d!<    |j2                  d"i |¤Ž y )#NÚlistÚstrT)ÚgroupÚsource_roleÚsource_roles)ÚtypeÚelementsÚrequiredÚaliases)Útarget_roleÚuserÚusersÚbool)r   ÚdefaultÚpresent)ÚabsentÚexactr   )r   r   ÚchoicesÚdbz5.0.0zcommunity.postgresql)ÚnameÚversionÚcollection_name)r   r   Údeprecated_aliases)r   )ÚgroupsÚtarget_rolesÚfail_on_roleÚstateÚlogin_dbÚsession_roleÚtrust_input)Úargument_specÚsupports_check_moder&   r'   r(   r)   r+   r,   F)Úwarn_db_default)Ú
autocommitr   r   )Úchangedr)   r&   r'   ÚqueriesÚgrantedÚrevoked© )r   ÚupdateÚdictr   Úparamsr   r
   r   r	   Úcursorr   r   ÚgrantÚmatchÚrevokeÚ
check_modeÚrollbackÚcommitÚcloser1   r&   r'   Úexecuted_queriesr3   r4   Ú	exit_json)r-   Úmoduler&   r'   r(   r)   r+   r,   Úconn_paramsÚdb_connectionÚdummyr9   Úpg_membershipÚreturn_dicts                 ú~/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/postgresql/plugins/modules/postgresql_membership.pyÚmainrJ   ¹   sm  € Ü1Ó3€MØ×ÑÜ˜¨%¸$ÒHpÔqÜ˜v°ÀÒNnÔoÜ˜v¨tÔ4Ü˜ yÒ:XÔYÜ˜5¨4¨&àØ"Ø#9ñðFô 
ô ˜uÔ%Ü˜f¨dÔ3ð ô ô  Ø#Ø ô€Fð
 ]‰]˜8Ñ$€FØ—=‘= Ñ0€LØ—=‘= Ñ0€LØM‰M˜'Ñ"€EØ—=‘= Ñ0€LØ—-‘- Ñ.€KÙäF˜F L°,Ô?ô ˜Ô Ü! &¨&¯-©-ÈÔO€KÜ(¨°ÈÔOÑ€M5Ø!ˆ]×!Ñ!Ñ3¤NÑ3€Fô
 ! ¨°¸À|ÓT€Mà	ÒØ×ÑÕà	'Ò	Ø×ÑÕà	(Ò	Ø×ÑÔð ×ÒØ×ÑÕ à×ÑÔà
‡LL„NØ×ÑÔô Ø×%Ñ%ØØ×#Ñ#Ø"×/Ñ/Ø×.Ñ.ô€Kð 	ÒØ!.×!6Ñ!6ˆIÒØ	(Ò	Ø!.×!6Ñ!6ˆIÒØ	'Ò	Ø!.×!6Ñ!6ˆIÑØ!.×!6Ñ!6ˆIÑà€F×ÑÑ#{Ó#ó    Ú__main__N)Ú
__future__r   r   r   r   Ú__metaclass__ÚDOCUMENTATIONÚEXAMPLESÚRETURNÚansible.module_utils.basicr   ÚFansible_collections.community.postgresql.plugins.module_utils.databaser   ÚFansible_collections.community.postgresql.plugins.module_utils.postgresr   r	   r
   r   r   r   rJ   Ú__name__r5   rK   rI   ú<module>rV      sZ   ð÷ AÑ @à€ð[€ðz'€ðR
€õ. 5õ÷÷ òO$ðd ˆzÒÙ…Fð rK   