
    Vh\                    b   d dl mZ dZdZdZd dlZd dlZd dlmZ d dl	m
Z
 d dlmZ  G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Zed k(  r e        yy)!    )annotationsa1  
---
module: group
version_added: "0.0.2"
short_description: Add or remove groups
requirements:
- groupadd
- groupdel
- groupmod
description:
    - Manage presence of groups on a host.
    - For Windows targets, use the M(ansible.windows.win_group) module instead.
options:
    name:
        description:
            - Name of the group to manage.
        type: str
        required: true
    gid:
        description:
            - Optional I(GID) to set for the group.
        type: int
    state:
        description:
            - Whether the group should be present or not on the remote host.
        type: str
        choices: [ absent, present ]
        default: present
    force:
        description:
            - Whether to delete a group even if it is the primary group of a user.
            - Only applicable on platforms which implement a C(--force) flag on the group deletion command.
        type: bool
        default: false
        version_added: "2.15"
    system:
        description:
            - If V(yes), indicates that the group created is a system group.
        type: bool
        default: no
    local:
        description:
            - Forces the use of "local" command alternatives on platforms that implement it.
            - This is useful in environments that use centralized authentication when you want to manipulate the local groups.
              (for example, it uses C(lgroupadd) instead of C(groupadd)).
            - This requires that these commands exist on the targeted host, otherwise it will be a fatal error.
        type: bool
        default: no
        version_added: "2.6"
    non_unique:
        description:
            - This option allows to change the group ID to a non-unique value. Requires O(gid).
            - Not supported on macOS or BusyBox distributions.
        type: bool
        default: no
        version_added: "2.8"
    gid_min:
        description:
            - Sets the GID_MIN value for group creation.
            - Overwrites /etc/login.defs default value.
            - Currently supported on Linux. Does nothing when used with other platforms.
            - Requires O(local) is omitted or V(False).
        type: int
        version_added: "2.18"
    gid_max:
        description:
            - Sets the GID_MAX value for group creation.
            - Overwrites /etc/login.defs default value.
            - Currently supported on Linux. Does nothing when used with other platforms.
            - Requires O(local) is omitted or V(False).
        type: int
        version_added: "2.18"
extends_documentation_fragment: action_common_attributes
attributes:
    check_mode:
        support: full
    diff_mode:
        support: none
    platform:
        platforms: posix
seealso:
- module: ansible.builtin.user
- module: ansible.windows.win_group
author:
- Stephen Fromm (@sfromm)
z
- name: Ensure group "somegroup" exists
  ansible.builtin.group:
    name: somegroup
    state: present

- name: Ensure group "docker" exists with correct gid
  ansible.builtin.group:
    name: docker
    state: present
    gid: 1750
a  
gid:
  description: Group ID of the group.
  returned: When O(state) is C(present)
  type: int
  sample: 1001
name:
  description: Group name.
  returned: always
  type: str
  sample: users
state:
  description: Whether the group is present or not.
  returned: always
  type: str
  sample: 'absent'
system:
  description: Whether the group is a system group or not.
  returned: When O(state) is C(present)
  type: bool
  sample: False
N)to_bytes)AnsibleModule)get_platform_subclassc                  ^     e Zd ZdZdZdZdZ fdZd Zd Z	d Z
d	 Zd
 Zd Zd Zd Z xZS )Groupa-  
    This is a generic Group manipulation class that is subclassed
    based on platform.

    A subclass may wish to override the following action methods:-
      - group_del()
      - group_add()
      - group_mod()

    All subclasses MUST define platform and distribution (which may be None).
    GenericN
/etc/groupc                @    t        t              }t        | |  |      S N)r   r   super__new__)clsargskwargsnew_cls	__class__s       E/home/dcms/DCMS/lib/python3.12/site-packages/ansible/modules/group.pyr   zGroup.__new__   s    '.S'*733    c                   || _         |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _	        |j                  d	   | _
        | j                  r>| j                  |j                  d
       | j                  |j                  d       y y y )Nstatenameforcegidsystemlocal
non_uniquegid_mingid_maxz&'gid_min' can not be used with 'local'msgz&'gid_max' can not be used with 'local')moduleparamsr   r   r   r   r   r   r   r   r   	fail_json)selfr"   s     r   __init__zGroup.__init__   s    ]]7+
MM&)	]]7+
=='mmH-]]7+
 --5}}Y/}}Y/::||'  %M N||'  %M N ( r   c                8    | j                   j                  |      S r   )r"   run_commandr%   cmds     r   execute_commandzGroup.execute_command   s    {{&&s++r   c                    | j                   rd}nd}| j                  j                  |d      | j                  g}| j	                  |      S )N	lgroupdelgroupdelT)r   r"   get_bin_pathr   r+   r%   command_namer*   s      r   	group_delzGroup.group_del   sC    ::&L%L{{''d;TYYG##C((r   c                8   | j                   rt        j                         D ]v  }| j                   |j                  k(  s| j                  |j
                  k7  s7| j                  j                  dj                  | j                   |j
                               x y y )Nz)GID '{0}' already exists with group '{1}'r    )	r   grpgetgrallgr_gidr   gr_namer"   r$   format)r%   grs     r   _local_check_gid_existszGroup._local_check_gid_exists   su    88lln x88ryy(TYY"**-DKK)).Y.`.`aeaiaikmkuku.v)wx r   c                   | j                   rd}| j                          nd}| j                  j                  |d      g}|D ]x  }|dk(  rR||   M|j	                  d       |j	                  t        ||                | j                  sH|j	                  d       Z|dk(  s`||   du sh|j	                  d       z | j                  8|j	                  d	       |j	                  d
t        | j                        z          | j                  8|j	                  d	       |j	                  dt        | j                        z          |j	                  | j                         | j                  |      S )N	lgroupaddgroupaddTr   -g-or   z-r-KGID_MIN=GID_MAX=)r   r:   r"   r/   appendstrr   r   r   r   r+   )r%   r   r1   r*   keys        r   	group_addzGroup.group_add   s,   ::&L((*%L{{''d;< 	!Ce|s 7

4 

3vc{+,??JJt$VC[D%8

4 	! <<#JJtJJzC$556<<#JJtJJzC$556

499##C((r   c                *   | j                   rd}| j                          nd}| j                  j                  |d      g}| j	                         }|D ]o  }|dk(  s	||   |d   t        ||         k7  s$|j                  d       |j                  t        ||                | j                  s_|j                  d       q t        |      dk(  ry	| j                  j                  ry
|j                  | j                         | j                  |      S )N	lgroupmodgroupmodTr      r>   r?      N rM   r   rM   rM   )r   r:   r"   r/   
group_infointrC   rD   r   len
check_moder   r+   )r%   r   r1   r*   inforE   s         r   	group_modzGroup.group_mod   s    ::&L((*%L{{''d;<  	)Ce|#;*tAw#fSk:J/JJJt$JJs6#;/0

4(	) s8q=!;;!!

499##C((r   c                D   | j                   rt        j                  j                  | j                        s5| j
                  j                  dj                  | j                               d}dj                  | j                        }t        | j                  d      5 }|j                         d d d   }|D ]   }|j                  t        |            sd} n d d d        |S 	 t        j                  | j                        ryy # 1 sw Y   |S xY w# t        $ r Y yw xY w)NzI'local: true' specified but unable to find local group file {0} to parse.r    Fz{0}:rbT)r   ospathexists	GROUPFILEr"   r$   r8   r   open	readlines
startswithr   r4   getgrnamKeyError)r%   rZ   	name_testfreversed_lineslines         r   group_existszGroup.group_exists   s
   
 ::77>>$..1%%*u*|*|  ~B  ~L  ~L  +M%  NFdii0Idnnd+ q!"tt!4* Dx	':;!% M<<		* + M
  s$   6DD%D D	DDc                    | j                         sy	 t        t        j                  | j                              }|S # t
        $ r Y yw xY w)NF)re   listr4   r_   r   r`   )r%   rS   s     r   rO   zGroup.group_info  sH      "	TYY/0D   		s   (= 	A	A	)__name__
__module____qualname____doc__platformdistributionr[   r   r&   r+   r2   r:   rF   rT   re   rO   __classcell__)r   s   @r   r   r      sH    
 HLI4O$,)x)0),2r   r   c                      e Zd ZdZd ZdZd Zy)Linuxz
    This is a Linux Group manipulation class. This is to apply the '-f' parameter to the groupdel command

    This overrides the following methods from the generic class:-
        - group_del()
    Nc                    | j                   rd}nd}| j                  j                  |d      g}| j                  r|j	                  d       |j	                  | j
                         | j                  |      S )Nr-   r.   Tz-f)r   r"   r/   r   rC   r   r+   r0   s      r   r2   zLinux.group_del"  s`    ::&L%L{{''d;<::JJt

499##C((r   )rh   ri   rj   rk   rl   rm   r2    r   r   rp   rp     s     HL	)r   rp   c                  "    e Zd ZdZd ZdZdZd Zy)SunOSz
    This is a SunOS Group manipulation class. Solaris doesn't have
    the 'system' group concept.

    This overrides the following methods from the generic class:-
        - group_add()
    Nr
   c                b   | j                   j                  dd      g}|D ]Z  }|dk(  s	||   |j                  d       |j                  t        ||                | j                  sJ|j                  d       \ | j
                  8|j                  d       |j                  dt        | j
                        z          | j                  8|j                  d       |j                  dt        | j                        z          |j                  | j                         | j                  |      S )	Nr=   Tr   r>   r?   r@   rA   rB   )	r"   r/   rC   rD   r   r   r   r   r+   r%   r   r*   rE   s       r   rF   zSunOS.group_add=  s    {{''
D9: 	%Ce|s 7

4 

3vc{+,??JJt$	% <<#JJtJJzC$556<<#JJtJJzC$556

499##C((r   )rh   ri   rj   rk   rl   rm   r[   rF   rr   r   r   rt   rt   0  s     HLI)r   rt   c                  .    e Zd ZdZd ZdZdZd Zd Zd Z	y)AIXz
    This is a AIX Group manipulation class.

    This overrides the following methods from the generic class:-
      - group_del()
      - group_add()
      - group_mod()
    Nr
   c                t    | j                   j                  dd      | j                  g}| j                  |      S )NrmgroupTr"   r/   r   r+   r)   s     r   r2   zAIX.group_del_  s2    {{''	48$))D##C((r   c                F   | j                   j                  dd      g}|D ]L  }|dk(  r&||   !|j                  dt        ||         z          .|dk(  s4||   du s<|j                  d       N | j                  8|j                  d       |j                  dt        | j                        z          | j
                  8|j                  d       |j                  d	t        | j
                        z          |j                  | j                         | j                  |      S )
NmkgroupTr   id=r   z-ar@   rA   rB   )r"   r/   rC   rD   r   r   r   r+   rv   s       r   rF   zAIX.group_addc  s    {{''	489 	!Ce|s 7

53vc{#334VC[D%8

4 		!
 <<#JJtJJzC$556<<#JJtJJzC$556

499##C((r   c                   | j                   j                  dd      g}| j                         }|D ]C  }|dk(  s	||   |d   t        ||         k7  s$|j	                  dt        ||         z          E t        |      dk(  ry| j                   j                  ry|j	                  | j                         | j                  |      S )	NchgroupTr   rJ   r~   rK   rL   rN   )
r"   r/   rO   rP   rC   rD   rQ   rR   r   r+   )r%   r   r*   rS   rE   s        r   rT   zAIX.group_mods  s    {{''	489  	9Ce|#;*tAw#fSk:J/JJJus6#;'778	9 s8q=!;;!!

499##C((r   
rh   ri   rj   rk   rl   rm   r[   r2   rF   rT   rr   r   r   rx   rx   Q  s'     HLI)) )r   rx   c                  .    e Zd ZdZdZdZdZd Zd Zd Z	y)FreeBsdGroupz
    This is a FreeBSD Group manipulation class.

    This overrides the following methods from the generic class:-
      - group_del()
      - group_add()
      - group_mod()
    FreeBSDNr
   c                v    | j                   j                  dd      d| j                  g}| j                  |      S )NpwTr.   r{   r)   s     r   r2   zFreeBsdGroup.group_del  s4    {{''d3ZK##C((r   c                B   | j                   j                  dd      d| j                  g}| j                  R|j	                  d       |j	                  t        | j                               | j                  r|j	                  d       | j                  8|j	                  d       |j	                  dt        | j                        z          | j                  8|j	                  d       |j	                  dt        | j                        z          | j                  |      S )	Nr   Tr=   r>   r?   r@   rA   rB   )
r"   r/   r   r   rC   rD   r   r   r   r+   r%   r   r*   s      r   rF   zFreeBsdGroup.group_add  s    {{''d3ZK88JJtJJs488}%

4 <<#JJtJJzC$556<<#JJtJJzC$556##C((r   c                   | j                   j                  dd      d| j                  g}| j                         }t	        |      }| j
                  mt        | j
                        |d   k7  rR|j                  d       |j                  t        | j
                               | j                  r|j                  d       |t	        |      k7  r(| j                   j                  ry| j                  |      S y)	Nr   TrI   rJ   r>   r?   rN   rL   )r"   r/   r   rO   rQ   r   rP   rC   rD   r   rR   r+   )r%   r   r*   rS   cmd_lens        r   rT   zFreeBsdGroup.group_mod  s    {{''d3ZK c(88CMT!W$<JJtJJs488}%

4 c#h{{%%"'',,r   r   rr   r   r   r   r     s'     HLI))r   r   c                      e Zd ZdZdZy)DragonFlyBsdGroupzq
    This is a DragonFlyBSD Group manipulation class.
    It inherits all behaviors from FreeBsdGroup class.
    	DragonFlyN)rh   ri   rj   rk   rl   rr   r   r   r   r     s    
 Hr   r   c                  2    e Zd ZdZdZdZd Zd ZddZd Z	y)	DarwinGroupz
    This is a Mac macOS Darwin Group manipulation class.

    This overrides the following methods from the generic class:-
      - group_del()
      - group_add()
      - group_mod()

    group manipulation are done using dseditgroup(1).
    DarwinNc                   | j                   j                  dd      g}|ddgz  }| j                  |dt        | j                        gz  }nId|v rE|d   du r>| j	                         }|dur*t        |      | _        |dt        | j                        gz  }|d| j
                  gz  }| j                  |      \  }}}|||fS )	NdseditgroupTr?   create-ir   F-L)r"   r/   r   rD   get_lowest_available_system_gidr   r+   )r%   r   r*   r   rcouterrs          r   rF   zDarwinGroup.group_add  s    {{''t<=h88D#dhh-((CF8$4$<668C%s8c$((m,,dii  --c2S#C~r   c                    | j                   j                  dd      g}|ddgz  }|d| j                  gz  }| j                  |      \  }}}|||fS )Nr   Tr?   deleter   r{   )r%   r*   r   r   r   s        r   r2   zDarwinGroup.group_del  s_    {{''t<=hdii  --c2S#C~r   c                4   | j                         }| j                  |t        | j                        |d   k7  ra| j                  j	                  dd      g}|ddgz  }||dt        |      gz  }|d| j                  gz  }| j                  |      \  }}}|||fS y)	NrJ   r   Tr?   editr   r   rL   )rO   r   rP   r"   r/   rD   r   r+   )r%   r   rS   r*   r   r   r   s          r   rT   zDarwinGroup.group_mod  s     88CMT!W$<;;++M4@ACD&>!Cc#h''D$))$$C!11#6NRcS>!r   c                ^   	 | j                   j                  dd      g}|g dz  }| j                  |      \  }}}|j                         }d}|D ]>  }|j	                  d      }t        |      dkD  s#t        |d         }	|	|kD  s7|	dk  s=|	}@ |dk(  s|d	k(  ry
|dz   S # t        $ r Y y
w xY w)NdsclT)z/Local/Defaultz-listz/GroupsPrimaryGroupIDr    rK   rW   i  i  F)r"   r/   r+   
splitlinessplitrQ   rP   	Exception)
r%   r*   r   r   r   lineshighestrO   partsr   s
             r   r   z+DarwinGroup.get_lowest_available_system_gid  s    	;;++FD9:CKKC!11#6NRcNN$EG# &
"((-u:>eBi.CW}s"%& !|w#~aK  		s*   A/B  2B  B  B  B   	B,+B,r   )
rh   ri   rj   rk   rl   rm   rF   r2   rT   r   rr   r   r   r   r     s'    	 HL
r   r   c                  .    e Zd ZdZdZdZdZd Zd Zd Z	y)OpenBsdGroupz
    This is a OpenBSD Group manipulation class.

    This overrides the following methods from the generic class:-
      - group_del()
      - group_add()
      - group_mod()
    OpenBSDNr
   c                t    | j                   j                  dd      | j                  g}| j                  |      S Nr.   Tr{   r)   s     r   r2   zOpenBsdGroup.group_del  2    {{''
D9499E##C((r   c                `   | j                   j                  dd      g}| j                  R|j                  d       |j                  t	        | j                               | j
                  r|j                  d       | j                  8|j                  d       |j                  dt	        | j                        z          | j                  8|j                  d       |j                  dt	        | j                        z          |j                  | j                         | j                  |      S Nr=   Tr>   r?   r@   rA   rB   
r"   r/   r   rC   rD   r   r   r   r   r+   r   s      r   rF   zOpenBsdGroup.group_add      {{''
D9:88JJtJJs488}%

4 <<#JJtJJzC$556<<#JJtJJzC$556

499##C((r   c                   | j                   j                  dd      g}| j                         }| j                  mt	        | j                        |d   k7  rR|j                  d       |j                  t        | j                               | j                  r|j                  d       t        |      dk(  ry| j                   j                  ry|j                  | j                         | j                  |      S 	NrI   TrJ   r>   r?   rK   rL   rN   r"   r/   rO   r   rP   rC   rD   r   rQ   rR   r   r+   r%   r   r*   rS   s       r   rT   zOpenBsdGroup.group_mod(      {{''
D9: 88CMT!W$<JJtJJs488}%

4 s8q=!;;!!

499##C((r   r   rr   r   r   r   r     s'     HLI)) )r   r   c                  .    e Zd ZdZdZdZdZd Zd Zd Z	y)NetBsdGroupz
    This is a NetBSD Group manipulation class.

    This overrides the following methods from the generic class:-
      - group_del()
      - group_add()
      - group_mod()
    NetBSDNr
   c                t    | j                   j                  dd      | j                  g}| j                  |      S r   r{   r)   s     r   r2   zNetBsdGroup.group_delH  r   r   c                `   | j                   j                  dd      g}| j                  R|j                  d       |j                  t	        | j                               | j
                  r|j                  d       | j                  8|j                  d       |j                  dt	        | j                        z          | j                  8|j                  d       |j                  dt	        | j                        z          |j                  | j                         | j                  |      S r   r   r   s      r   rF   zNetBsdGroup.group_addL  r   r   c                   | j                   j                  dd      g}| j                         }| j                  mt	        | j                        |d   k7  rR|j                  d       |j                  t        | j                               | j                  r|j                  d       t        |      dk(  ry| j                   j                  ry|j                  | j                         | j                  |      S r   r   r   s       r   rT   zNetBsdGroup.group_mod\  r   r   r   rr   r   r   r   r   :  s'     HLI)) )r   r   c                  "    e Zd ZdZd Zd Zd Zy)BusyBoxGroupz
    BusyBox group manipulation class for systems that have addgroup and delgroup.

    It overrides the following methods:
        - group_add()
        - group_del()
        - group_mod()
    c                B   | j                   j                  dd      g}| j                  &|j                  dt	        | j                        g       | j
                  r|j                  d       | j                  8|j                  d       |j                  dt	        | j                        z          | j                  8|j                  d       |j                  dt	        | j                        z          |j                  | j                         | j                  |      S )NaddgroupTr>   z-Sr@   rA   rB   )r"   r/   r   extendrD   r   rC   r   r   r   r+   r   s      r   rF   zBusyBoxGroup.group_addy  s    {{''
D9:88JJc$((m,-;;JJt<<#JJtJJzC$556<<#JJtJJzC$556

499##C((r   c                t    | j                   j                  dd      | j                  g}| j                  |      S )NdelgroupTr{   r)   s     r   r2   zBusyBoxGroup.group_del  r   r   c                h   | j                         }| j                  | j                  |d   k7  rt        dd      5 }|j                         }d d d        t	        | j
                        }d||d   fz  }d|| j                  fz  }d| j                  z  v r6| j                  j                  dj                  | j                               | j                  j                  ry	|j                  ||      }t        dd
      5 }|j                  |       d d d        y	y# 1 sw Y   xY w# 1 sw Y   y	xY w)NrJ   r
   rV   s   %s:x:%d:s   :%d:zgid '{gid}' in user   r    rN   wbrL   )rO   r   r\   readr   r   r"   r$   r8   rR   replacewrite)	r%   r   rS   rb   b_groupsb_nameb_current_group_stringb_new_group_stringb_new_groupss	            r   rT   zBusyBoxGroup.group_mod  s    88DHHQ$7lD) $Q668$ dii(F%0FDG3D%D"!,/A!A!X-%%*>*E*E$((*E*S%T{{%% #++,BDVWLlD) &Q%&#$ $&s   D D(D%(D1N)rh   ri   rj   rk   rF   r2   rT   rr   r   r   r   r   o  s    )()r   r   c                      e Zd ZdZdZy)AlpineGrouprp   AlpineN)rh   ri   rj   rl   rm   rr   r   r   r   r     s    HLr   r   c                 r   t        t        t        ddddg      t        dd      t        dd	      t        d
      t        dd	      t        dd	      t        dd	      t        d
      t        d
      	      ddddggg      } | j                  d   r!| j                  d   r| j                  d       t	        |       }| j                  d|j                  z         |j                  r| j                  d|j                  z         d }d}d}i }|j                  |d<   |j                  |d<   |j                  dk(  rf|j                         r| j                  r| j                  d       |j                         \  }}}|dk7  r| j                  |j                  |       n|j                  dk(  r|j                         sJ| j                  r| j                  d       |j                  |j                  |j                         \  }}}n |j#                  |j                        \  }}}|"|dk7  r| j                  |j                  |       |d|d<   nd|d<   |r||d<   |r||d <   |j                         r'|j%                         }|j                   |d!<   |d"   |d<    | j                  d#i | y )$NrD   presentabsent)typedefaultchoicesT)r   requiredboolF)r   r   rP   )r   )	r   r   r   r   r   r   r   r   r   r   r   )argument_specsupports_check_moderequired_ifr   r   zWforce is not a valid option for local, force=True and local=True are mutually exclusiver    z Group instantiated - platform %sz$Group instantiated - distribution %srM   r   r   )changedr   )r   r!   )r   r   r   r   stdoutstderrr   rJ   rr   )r   dictr#   r$   r   debugrl   rm   r   r   re   rR   	exit_jsonr2   rF   r   r   rT   rO   )r"   groupr   r   r   resultrS   s          r   mainr     s   E9x>ST540FE2% VU3FE27e$e$

 !4%)
F$ }}W&--"8vw&ME
LL3ennDE;e>P>PPQ	B
C
CFZZF6NkkF7O{{h     ."__.NRcQw  ejjc :			!!!#     ."__5<<_PNRc"___;NRc>bAg%**#6	z!y y
x
x! <<xQuFvr   __main__)
__future__r   DOCUMENTATIONEXAMPLESRETURNr4   rX   +ansible.module_utils.common.text.convertersr   ansible.module_utils.basicr   $ansible.module_utils.common.sys_infor   objectr   rp   rt   rx   r   r   r   r   r   r   r   r   rh   rr   r   r   <module>r      s    #Un
.  	 @ 4 FFF FV)E )2)E )B.)% .)f/5 /d B% BJ/)5 /)h/)% /)j75 7t, FR zF r   