
    VhAJ                         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Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
author:
  - Kairo Araujo (@kairoaraujo)
module: aix_filesystem
short_description: Configure LVM and NFS file systems for AIX
description:
  - This module creates, removes, mount and unmount LVM and NFS file system for AIX using C(/etc/filesystems).
  - For LVM file systems is possible to resize a file system.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  account_subsystem:
    description:
      - Specifies whether the file system is to be processed by the accounting subsystem.
    type: bool
    default: false
  attributes:
    description:
      - Specifies attributes for files system separated by comma.
    type: list
    elements: str
    default:
      - agblksize=4096
      - isnapshot=no
  auto_mount:
    description:
      - File system is automatically mounted at system restart.
    type: bool
    default: true
  device:
    description:
      - Logical volume (LV) device name or remote export device to create a NFS file system.
      - It is used to create a file system on an already existing logical volume or the exported NFS file system.
      - If not mentioned a new logical volume name will be created following AIX standards (LVM).
    type: str
  fs_type:
    description:
      - Specifies the virtual file system type.
    type: str
    default: jfs2
  permissions:
    description:
      - Set file system permissions. V(rw) (read-write) or V(ro) (read-only).
    type: str
    choices: [ro, rw]
    default: rw
  mount_group:
    description:
      - Specifies the mount group.
    type: str
  filesystem:
    description:
      - Specifies the mount point, which is the directory where the file system will be mounted.
    type: str
    required: true
  nfs_server:
    description:
      - Specifies a Network File System (NFS) server.
    type: str
  rm_mount_point:
    description:
      - Removes the mount point directory when used with state V(absent).
    type: bool
    default: false
  size:
    description:
      - Specifies the file system size.
      - For already V(present) it will be resized.
      - 512-byte blocks, Megabytes or Gigabytes. If the value has M specified it will be in Megabytes. If the value has G
        specified it will be in Gigabytes.
      - If no M or G the value will be 512-byte blocks.
      - If "+" is specified in begin of value, the value will be added.
      - If "-" is specified in begin of value, the value will be removed.
      - If "+" or "-" is not specified, the total value will be the specified.
      - Size will respects the LVM AIX standards.
    type: str
  state:
    description:
      - Controls the file system state.
      - V(present) check if file system exists, creates or resize.
      - V(absent) removes existing file system if already V(unmounted).
      - V(mounted) checks if the file system is mounted or mount the file system.
      - V(unmounted) check if the file system is unmounted or unmount the file system.
    type: str
    choices: [absent, mounted, present, unmounted]
    default: present
  vg:
    description:
      - Specifies an existing volume group (VG).
    type: str
notes:
  - For more O(attributes), please check "crfs" AIX manual.
a  
- name: Create filesystem in a previously defined logical volume.
  community.general.aix_filesystem:
    device: testlv
    filesystem: /testfs
    state: present

- name: Creating NFS filesystem from nfshost.
  community.general.aix_filesystem:
    device: /home/ftp
    nfs_server: nfshost
    filesystem: /home/ftp
    state: present

- name: Creating a new file system without a previously logical volume.
  community.general.aix_filesystem:
    filesystem: /newfs
    size: 1G
    state: present
    vg: datavg

- name: Unmounting /testfs.
  community.general.aix_filesystem:
    filesystem: /testfs
    state: unmounted

- name: Resizing /mksysb to +512M.
  community.general.aix_filesystem:
    filesystem: /mksysb
    size: +512M
    state: present

- name: Resizing /mksysb to 11G.
  community.general.aix_filesystem:
    filesystem: /mksysb
    size: 11G
    state: present

- name: Resizing /mksysb to -2G.
  community.general.aix_filesystem:
    filesystem: /mksysb
    size: -2G
    state: present

- name: Remove NFS filesystem /home/ftp.
  community.general.aix_filesystem:
    filesystem: /home/ftp
    rm_mount_point: true
    state: absent

- name: Remove /newfs.
  community.general.aix_filesystem:
    filesystem: /newfs
    rm_mount_point: true
    state: absent
z
changed:
  description: Return changed for aix_filesystems actions as true or false.
  returned: always
  type: bool
msg:
  description: Return message regarding the action.
  returned: always
  type: str
)AnsibleModule)ismountNc                     | j                  dd      }| j                  |d|g      \  }}}|dk(  r-t        j                  d|      ry| j	                  d|z         y	y)
z
    Check if file system already exists on /etc/filesystems.

    :param module: Ansible module.
    :param community.general.filesystem: filesystem name.
    :return: True or False.
    lsfsTz-l   zNo record matchingFz%Failed to run lsfs. Error message: %smsgN)get_bin_pathrun_commandrefindall	fail_json)module
filesystemlsfs_cmdrclsfs_outerrs         t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/aix_filesystem.py
_fs_existsr      sk     ""640H**HdJ+GHB#	Qw::*C0 !H3!NO     c                     | j                  dd      }| j                  |d|g      \  }}}|dk7  r| j                  d|z         y
|j                         }|D ]  }|j	                  d      d   |k(  s y y	)z
    Validate if NFS server is exporting the device (remote export).

    :param module: Ansible module.
    :param nfs_host: nfs_host parameter, NFS server.
    :param device: device parameter, remote export.
    :return: True or False.
    	showmountT-ar   z*Failed to run showmount. Error message: %sr   :r
   FN)r   r   r   
splitlinessplit)	r   nfs_hostdeviceshowmount_cmdr   showmount_outr   showmount_datalines	            r   _check_nfs_devicer'      s     ''T:M#//h0OPBs	QwICOP&113" 	Dzz#q!V+	 r   c                 6   | j                  dd      }| j                  |dg      \  }}}|dk7  r| j                  d|z         | j                  |g      \  }}}|dk7  r| j                  d|z         ||v r||vr	d|z  }d|fS ||v r	d	|z  }d|fS d
|z  }d|fS )z
    Check the current state of volume group.

    :param module: Ansible module argument spec.
    :param vg: Volume Group name.
    :return: True (VG in varyon state) or False (VG in varyoff state) or
             None (VG does not exist), message.
    lsvgTz-or   zFailed executing %s command.r   z$Volume group %s is in varyoff state.Fz#Volume group %s is in varyon state.zVolume group %s does not exist.N)r   r   r   )r   vglsvg_cmdr   current_active_vgsr   current_all_vgsr   s           r   _validate_vgr.      s     ""640H"("4"4h5E"FBC	Qw;hFG%118*=B	Qw;hFG	_+=!=4r9cz	!	!3b8Sy/"4Syr   c                 N   | j                  dd      }| j                  s| j                  |dd|z  |g      \  }}}|dk(  rd}||fS |dk7  r2t        j                  d|      rd}||fS | j                  d	|z  
       yt        j                  d|      rd}||fS d}||fS d}d}||fS )z Resize LVM file system. chfsTr   zsize=%s   Fr   zMaximum allocation for logicalz%Failed to run chfs. Error message: %sr   zThe filesystem size is already N)r   
check_moder   r   r   r   )	r   r   sizechfs_cmdr   chfs_outr   changedr   s	            r   	resize_fsr8     s     ""640H"..$	D@PR\/]^Hc8GH$$1Wzz:C@|#  %Ls%R S zz:HE H$$ H$$|r   c                    dj                  |      }ddd}|
ddd}ndd	d}|d
}nd|z  }|d
}nd|z  }|d
}nt        | |      \  }}|rd|z  }nd}||fS |d
}nd|z  }||   }||   }|
l| j                  dd      }| j                  sF| j	                  |d||d|
d|	|ddg      \  }}}|dk7  r| j                  d|z         yd}d|z  }||fS d}d
}||fS | j                  dd      }| j                  sB|g}|j                  d       |j                  |       |r5|j                         \  }}|j                  |       |j                  |       |r5|j                         \  }}|j                  |       |j                  |       |j                  d       |j                  |       |r5|j                         \  }}|j                  |       |j                  |       |r5|j                         \  }}|j                  |       |j                  |       |r5|j                         \  }}|j                  |       |j                  |       |j                  d       |j                  |	       |r5|j                         \  }}|j                  |       |j                  |       |r9|j                         }|j                  d       |D ]  }|j                  |        | j	                  |      \  }}}|dk(  r| j                  d |z         y|dk7  r| j                  d!|d"|       yd}||fS d}d
}||fS )#z3 Create LVM file system or NFS remote mount point. z -a z-t yesz-t noTFNz-Ar   z-A yesz-A nor2   z
-a size=%sz-d %sz-g %sFz-u %smknfsmntTz-fz-hz-tz-wbgr   z)Failed to run mknfsmnt. Error message: %sr   zNFS file system %s created.crfsz-vz-mz-p
   zVUsing a existent previously defined logical volume, volume group needs to be empty. %sFailed to run . Error message: )	joinr.   r   r3   r   r   appendr    	exit_json)r   fs_typer   r*   r"   r4   mount_group
auto_mountaccount_subsystempermissions
nfs_server
attributesaccount_subsys_optauto_mount_optvg_stater   r7   mknfsmnt_cmdr   mknfsmnt_outr   crfs_cmdcmdflagvaluesplitted_attributescrfs_outs                              r   	create_fsrV      s   
 Z(J 
 
 

 |d"~6!	z$VR0#2BGC< +
+J*+<=**:t<  $*$6$6dJX^`dfprv  yD  FP  RV  X\  8]  %^!BcQw  %PSV%V W3j@|#GCC< &&vt4  *CJJtJJw "
u

4 

5! &u

4 

5!JJtJJz" + 1 1 3u

4 

5! * 0 0 2u

4 

5!  1 7 7 9u

4 

5!JJtJJ{# $

u

4 

5!&0&6&6&8#

4 0 &EJJu%& !' 2 23 7B#Rx  =?BC ! D q  cSV%W X ((GCC<r   c                     ddd}||   }| j                  dd      }| j                  sH|d||g}| j                  |      \  }}}|dk7  r| j                  d|d|	       yd}	|}
|sd
|z  }
|	|
fS d}	d}
|	|
fS )z) Remove an LVM file system or NFS entry. z-rr2   r:   rmfsTr   r?   r@   r   zFile system %s removed.Nr   r3   r   r   )r   r   rm_mount_pointrm_mount_point_optrmfs_cmdrQ   r   rmfs_outr   r7   r   s              r   	remove_fsr^     s    
 
 (7N""640H~z:"..s3Hc7#s!STGC/*<C<|r   c                     | j                  dd      }| j                  s=| j                  ||g      \  }}}|dk7  r| j                  d|z         yd}d|z  }||fS d}d}||fS )	z Mount a file system. mountTr   z&Failed to run mount. Error message: %sr   File system %s mounted.r2   NrY   )r   r   	mount_cmdr   	mount_outr   r7   r   s           r   mount_fsrd     s    ##GT2I#//J0GHIs7!IC!OPG+j8CC<|r   c                     | j                  dd      }| j                  s=| j                  ||g      \  }}}|dk7  r| j                  d|z         yd}d|z  }||fS d}d}||fS )	z Unmount a file system.unmountTr   z(Failed to run unmount. Error message: %sr   zFile system %s unmounted.r2   NrY   )r   r   unmount_cmdr   unmount_outr   r7   r   s           r   
unmount_fsri     s    %%i6K%11;
2KLK7!Kc!QRG-
:CC<|r   c                     t        t        t        dd      t        ddddg      t        dd	      t        d
      t        dd	      t        dd      t        ddddg      t        d
      t        d
      t        dd      t        d
      t        ddg d      t        d
            d	      } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }	| j                  d   }
| j                  d   }| j                  d   }| j                  d    }t        dd!"      }|dk(  rt        |      }t	        | |      }|s|r(d#|z  |d$<   d|d%<   |t        | ||      \  |d%<   |d$<   np|	F|d&|d$<    | j                  d0i | n,t        | |	|      rt        | ||||||||||	|      \  |d%<   |d$<   |9|d'|d$<    | j                  d0i | nt        | ||||||||||	|      \  |d%<   |d$<   ||	t        | ||||||||||	|      \  |d%<   |d$<   n|d(k(  rBt        |      r	d)|z  |d$<   nt	        | |      }|s	d*|z  |d$<   nt        | ||
      \  |d%<   |d$<   n|d+k(  r/t        |      rd|d%<   d,|z  |d$<   ndt        | |      \  |d%<   |d$<   nN|d-k(  r/t        |      sd|d%<   d.|z  |d$<   n0t        | |      \  |d%<   |d$<   nd/|z  |d$<    | j                  d0i |  | j                  d0i | y )1NboolF)typedefaultliststrzagblksize=4096zisnapshot=no)rl   elementsrm   T)rl   )rl   requiredjfs2rwro)rl   rm   choicespresent)absentmountedrv   	unmounted)rG   rJ   rF   r"   r   rD   rH   rE   rI   rZ   r4   stater*   )argument_specsupports_check_moderG   rJ   rF   r"   rD   rH   rE   r   rI   rZ   r4   rz   r*   r2   )r7   r   zFile system %s already exists.r   r7   z<Parameter "device" is required when "nfs_server" is defined.zKRequired parameter "device" and/or "vg" is missing for filesystem creation.rw   ra   zFile system %s does not exist.rx   zFile system %s already mounted.ry   z!File system %s already unmounted.zUnexpected state %s. )r   dictparamsr   r   r8   r   r'   rV   r^   rd   ri   rC   )r   rG   rJ   rF   r"   rD   rH   rE   r   rI   rZ   r4   rz   r*   result
fs_mounted	fs_exists	fs_statuss                     r   mainr     s   ">%BRTbAcd6U#6eV4%d|L%('VU;5!E9>kl
 !!F& &9:|,J|,J]]8$FmmI&G--.K--.K|,J|,J]]#34N== DMM'"E	t	BF
 	Z(
vz2	 <zIF5M %F9 3<VZQU3V0y!6%=
 %>$bF5M$F$$.v. )VD;D"GZVT;Xbdu  xC  EO  Q[<\8y)6%= ~:$qF5M$F$$.v. 8AR{T^`qs~  AK  MW8X4F9%ve} !j&83<GZVT;PZ\moz  }G  IS4T0y!6%= 
(	:5
BF5M #6:6I @: Mu3<VZQ_3`0y!6%=	)	: %F9=
JF5M/7
/K,F9ve}	+	z" %F9?*LF5M/9&*/M,F9ve} /6u"6"Fvr   __main__)
__future__r   r   r   rl   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   Aansible_collections.community.general.plugins.module_utils._mountr   r   r   r'   r.   r8   rV   r^   rd   ri   r   __name__r}   r   r   <module>r      s{    A @aF7r	
 5 U 	,,:>M `>((m` zF r   