
    Vh6                         d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlmZ d dlmZ  G d de      Z G d	 d
e      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functiona  
module: cronvar
short_description: Manage variables in crontabs
description:
  - Use this module to manage crontab variables.
  - This module allows you to create, update, or delete cron variable definitions.
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
options:
  name:
    description:
      - Name of the crontab variable.
    type: str
    required: true
  value:
    description:
      - The value to set this variable to.
      - Required if O(state=present).
    type: str
  insertafter:
    description:
      - If specified, the variable will be inserted after the variable specified.
      - Used with O(state=present).
    type: str
  insertbefore:
    description:
      - Used with O(state=present). If specified, the variable will be inserted just before the variable specified.
    type: str
  state:
    description:
      - Whether to ensure that the variable is present or absent.
    type: str
    choices: [absent, present]
    default: present
  user:
    description:
      - The specific user whose crontab should be modified.
      - This parameter defaults to V(root) when unset.
    type: str
  cron_file:
    description:
      - If specified, uses this file instead of an individual user's crontab.
      - Without a leading V(/), this is assumed to be in C(/etc/cron.d).
      - With a leading V(/), this is taken as absolute.
    type: str
  backup:
    description:
      - If set, create a backup of the crontab before it is modified. The location of the backup is returned in the C(backup)
        variable by this module.
    type: bool
    default: false
requirements:
  - cron
author:
  - Doug Luce (@dougluce)
a  
- name: Ensure entry like "EMAIL=doug@ansibmod.con.com" exists
  community.general.cronvar:
    name: EMAIL
    value: doug@ansibmod.con.com

- name: Ensure a variable does not exist. This may remove any variable named "LEGACY"
  community.general.cronvar:
    name: LEGACY
    state: absent

- name: Add a variable to a file under /etc/cron.d
  community.general.cronvar:
    name: LOGFILE
    value: /var/log/yum-autoupdate.log
    user: root
    cron_file: ansible_yum-autoupdate
N)AnsibleModule)shlex_quotec                       e Zd Zy)CronVarErrorN)__name__
__module____qualname__     m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/cronvar.pyr	   r	   q   s    r   r	   c                   j    e Zd ZdZddZd Zd ZddZd Zd Z	d	 Z
d
 Zd Zd ZddZd Zd Zd Zy)CronVarz
        CronVar object to write variables to crontabs.

        user      - the user of the crontab (defaults to root)
        cron_file - a cron file under /etc/cron.d
    Nc                    || _         || _        d | _        dj                  d t	        d      D              | _        | j                   j                  dd      | _        |rTd| _        t        j                  j                  |      r|| _        n-t        j                  j                  d|      | _        nd | _        | j                          y )N c              3   N   K   | ]  }t        |      d vst        |        yw))='"N)chr).0xs     r   	<genexpr>z#CronVar.__init__.<locals>.<genexpr>   s       ^As1vM]?]Q ^s   %%   crontabT)requiredz/etc/cron.d)moduleuserlinesjoinrange	wordcharsget_bin_pathcron_cmd	cron_fileospathisabsread)selfr   r    r'   s       r   __init__zCronVar.__init__}   s    	
 ^s ^^00T0JDNww}}Y'!*!#mY!G!DN		r   c                    g | _         | j                  rE	 t        | j                  d      5 }|j                         j	                         | _         d d d        y y | j                  j                  | j                         d      \  }}}|dk7  r|dk7  rt        d      |j	                         }d}|D ]i  }|dkD  sBt        j                  d	|      sGt        j                  d
|      s1t        j                  d|      s| j                   j                  |       |dz  }k y # 1 sw Y   xY w# t
        $ r Y y t        $ r" t        dt        j                         d         w xY w)NrUnexpected error:r   Tuse_unsafe_shell   zUnable to read crontab   z8# DO NOT EDIT THIS FILE - edit the master and reinstall.z# \(/tmp/.*installed on.*\)z# \(.*version.*\))r!   r'   openr+   
splitlinesIOError	Exceptionr	   sysexc_infor   run_command_read_user_executerematchappend)r,   frcouterrr!   countls           r   r+   zCronVar.read   sO   
>>K$..#. 7!!"!4!4!6DJ7 7 "[[44T5L5L5Nae4fNRcQw27"#;<<NN$EE 19RXX.ikl &08:A_ab8clnltlt  vJ  LM  mNJJ%%a(
	!7 7  K"#6q8IJJKs(   D* $DD* D'#D* *	E5*Ec                 @    | j                   j                  d|z         y )Nzansible: "%s")r   debug)r,   messages     r   log_messagezCronVar.log_message   s    /G34r   c                    |rt        |d      }nT| j                  rt        | j                  d      }d}n/t        j                  d      \  }}t	        j
                  |d      }|j                  | j                                |j                          |ry| j                  sh| j                  j                  | j                        d      \  }}}t	        j                  |       |dk7  r| j                  j                  |       yyy)	zI
        Write the crontab to the system. Saves all information.
        wNr   prefixTr1   r   msg)r5   r'   tempfilemkstempr(   fdopenwriterendercloser   r;   _write_executeunlink	fail_json)r,   backup_filefilehr)   filedrA   rB   rC   s           r   rS   zCronVar.write   s     c*E^^-ED"**)<KE4IIeS)EDKKM"  ~~![[44T5H5H5Nae4fNRcIIdOQw%%#%.  r   c                     	 t        j                  | j                         y# t        $ r Y yt        $ r" t        dt        j                         d         w xY w)NTFr0   r   )r(   rW   r'   OSErrorr8   r	   r9   r:   )r,   s    r   remove_variable_filezCronVar.remove_variable_file   sP    	GIIdnn% 	 	G2CLLN14EFF	Gs   " 	A*Ac                     t        j                   |      }| j                  |_        |j                         }|j                         dk(  }dj                  |      }|r||fS t	        d      )Nr   r   zNot a variable.)shlexr$   	get_tokenr"   r	   )r,   linelexervarname
is_env_varvalues         r   parse_for_varzCronVar.parse_for_var   s_    D!..//#__&#-
U##,--r   c                     | j                   D ]   }	 | j                  |      \  }}||k(  r|c S " y # t        $ r Y /w xY wN)r!   rg   r	   )r,   namerE   rd   rf   s        r   find_variablezCronVar.find_variable   sY     	A#'#5#5a#8 %d? L #	    s   1	==c                     g }| j                   D ](  }	 | j                  |      \  }}|j                  |       * |S # t        $ r Y 8w xY wri   )r!   rg   r?   r	   )r,   	var_namesrE   var_namedummys        r   get_var_nameszCronVar.get_var_names   s]    	 	A"&"4"4Q"7%  *	    s   %<	AAc                    |$|"| j                   j                  d|d|       y g }| j                   D ]w  }	 | j                  |      \  }}||k(  r(|j                  |d|       |j                  |       n3||k(  r(|j                  |       |j                  |d|       nt        y || _         y # t        $ r |j                  |       Y w xY w)Nr   r   )r!   insertrg   r?   r	   )	r,   rj   rf   insertbeforeinsertafternewlinesrE   rd   ro   s	            r   add_variablezCronVar.add_variable   s    K$7JJaD%!89HZZ ''%)%7%7%:NGU,. 4(?@ * K/ * 4(?@**' "DJ $ 'OOA&'s   A4B77CCc                 ,    | j                  |d d       y )NT)remove)update_variable)r,   rj   s     r   remove_variablezCronVar.remove_variable	  s    T45r   c                     g }| j                   D ]:  }	 | j                  |      \  }}||k7  rt        |s|j                  |d|       < || _         y # t        $ r |j                  |       Y aw xY w)Nr   )r!   rg   r	   r?   )r,   rj   rf   rx   ru   rE   rd   ro   s           r   ry   zCronVar.update_variable  s{     	#A#!%!3!3A!6d?&&OOtU$;<	# 
   #"#s   7AA10A1c                 X    dj                  | j                        }|r|d   dvr|dz  }|S )z)
        Render a proper crontab
        
)r}   )r"   r!   )r,   results     r   rT   zCronVar.render  s2     4::&fRj4dNFr   c                 v   d}| j                   rt        j                         dk(  r/dt        | j                         dt        | j                        dS t        j                         dk(  r-t        | j                        dt        | j                         S t        j                         dk(  r'| j                  d	d
d	t        | j                         S t        j                  t        j                               d   | j                   k7  rdt        | j                         z  }| j                  d	|d	d
S )z@
        Returns the command line for reading a crontab
        r   SunOSzsu z -c 'z -l'AIXz -l HP-UX z-lr   -u %s	r    platformsystemr   r&   pwdgetpwuidr(   getuid)r,   r    s     r   r<   zCronVar._read_user_execute#  s     99 G+-8-C[QUQ^Q^E_``"e+%0%?TYYAWXX"g-%)]]D+dii:PQQbiik*1-:TYY!77!]]D$77r   c                    d}| j                   rt        j                         dv rTdt        | j                         dt        |      dt        | j                         d| j                  dt        |      dS t        j                  t        j                               d   | j                   k7  rd	t        | j                         z  }| j                  d|dt        |      S )
z?
        Return the command line for writing a crontab
        r   )r   r   r   zchown r   z ; su 'z' -c 'r   r   r   r   )r,   r)   r    s      r   rV   zCronVar._write_execute4  s     99 $==		*K,={499?UW[WdWdfqrvfwy ybiik*1-:TYY!77!]]D+d2CDDr   )NNri   )F)r
   r   r   __doc__r-   r+   rI   rS   r^   rg   rk   rp   rv   rz   ry   rT   r<   rV   r   r   r   r   r   u   sO    $85/:G.",68"Er   r   c                  :   t        t        t        dd      t        d      t        d      t        d      t        d      t        d      t        ddddg      t        dd	
            ddggd	      } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }|dk(  }	d	}
t               }t        j                  t        dd             t        | ||      }| j                  d|z         ||	r| j                  d       ||	r| j                  d       ||	s| j                  d       |r*t        j                  d      \  }}|j                  |       |j                  r(|s&|	s$|j                         }
| j                  |
||       |j                  |      }|	r3||j!                  ||||       d}
n/||k7  r*|j#                  ||       d}
n||j%                  |       d}
|j'                         |
d}|
r|j                          |r|
r|d <   nt        j(                         |r||d<    | j                  d!i | y )"NstrT)typer   )r   presentabsent)r   defaultchoicesboolF)r   r   )rj   rf   r    r'   rt   rs   statebackuprs   rt   )argument_specmutually_exclusivesupports_check_moderj   rf   r    r'   r   r   022   z!cronvar instantiated - name: "%s"z5You must specify 'name' to insert a new cron variablerN   z6You must specify 'value' to insert a new cron variablez1You must specify 'name' to remove a cron variablecronvarrL   )changedr'   r   )varsr   rY   r   )r   dictparamsr(   umaskintr   rG   rX   rP   rQ   rS   r'   remove_job_file	exit_jsonrk   rv   ry   rz   rp   rW   )r   rj   rf   r    r'   rt   rs   r   r   ensure_presentr   res_argsr   ro   rY   	old_values                   r   mainr   D  s    540E"5!&%(5)E9x>STVU3	
 ,];<!F == DMM'"E== Dk*I--.K==0LMM'"E]]8$Fi'NGvH HHS]fdI.G
LL4t;< |TU}UV|NPQ %--Y?{k"n))+IUK%%d+I  ulKHG%##D%0G ##D)G %%'H
  &1H]#IIk" )F x r   __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESr(   r   r   r=   r`   r9   rP   ansible.module_utils.basicr   ansible.module_utils.six.movesr   r8   r	   objectr   r   r
   r   r   r   <module>r      sw   " A @<|& 
  
 	  
  4 6	9 	JEf JE^e!P zF r   