
    Vhf                        d dl mZ d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mZ d dlmZ d dlmZ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)    )annotationsaO  
---
module: cron
short_description: Manage cron.d and crontab entries
description:
  - Use this module to manage crontab and environment variables entries. This module allows
    you to create environment variables and named crontab entries, update, or delete them.
  - 'When crontab jobs are managed: the module includes one line with the description of the
    crontab entry C("#Ansible: <name>") corresponding to the O(name) passed to the module,
    which is used by future ansible/module calls to find/check the state. The O(name)
    parameter should be unique, and changing the O(name) value will result in a new cron
    task being created (or a different one being removed).'
  - When environment variables are managed, no comment line is added, but, when the module
    needs to find/check the state, it uses the O(name) parameter to find the environment
    variable definition line.
  - When using symbols such as C(%), they must be properly escaped.
version_added: "0.9"
options:
  name:
    description:
      - Description of a crontab entry or, if O(env) is set, the name of environment variable.
      - This parameter is always required as of ansible-core 2.12.
    type: str
    required: yes
  user:
    description:
      - The specific user whose crontab should be modified.
      - When unset, this parameter defaults to the current user.
    type: str
  job:
    description:
      - The command to execute or, if O(env) is set, the value of environment variable.
      - The command should not contain line breaks.
      - Required if O(state=present).
    type: str
    aliases: [ value ]
  state:
    description:
      - Whether to ensure the job or environment variable is present or absent.
    type: str
    choices: [ absent, present ]
    default: present
  cron_file:
    description:
      - If specified, uses this file instead of an individual user's crontab.
        The assumption is that this file is exclusively managed by the module,
        do not use if the file contains multiple entries, NEVER use for /etc/crontab.
      - If this is a relative path, it is interpreted with respect to C(/etc/cron.d).
      - Many Linux distros expect (and some require) the filename portion to consist solely
        of upper- and lower-case letters, digits, underscores, and hyphens.
      - Using this parameter requires you to specify the O(user) as well, unless O(state=absent).
      - Either this parameter or O(name) is required.
    type: path
  backup:
    description:
      - If set, create a backup of the crontab before it is modified.
        The location of the backup is returned in the RV(ignore:backup_file) variable by this module.
    type: bool
    default: no
  minute:
    description:
      - Minute when the job should run (V(0-59), V(*), V(*/2), and so on).
    type: str
    default: "*"
  hour:
    description:
      - Hour when the job should run (V(0-23), V(*), V(*/2), and so on).
    type: str
    default: "*"
  day:
    description:
      - Day of the month the job should run (V(1-31), V(*), V(*/2), and so on).
    type: str
    default: "*"
    aliases: [ dom ]
  month:
    description:
      - Month of the year the job should run (V(1-12), V(*), V(*/2), and so on).
    type: str
    default: "*"
  weekday:
    description:
      - Day of the week that the job should run (V(0-6) for Sunday-Saturday, V(*), and so on).
    type: str
    default: "*"
    aliases: [ dow ]
  special_time:
    description:
      - Special time specification nickname.
    type: str
    choices: [ annually, daily, hourly, monthly, reboot, weekly, yearly ]
    version_added: "1.3"
  disabled:
    description:
      - If the job should be disabled (commented out) in the crontab.
      - Only has effect if O(state=present).
    type: bool
    default: no
    version_added: "2.0"
  env:
    description:
      - If set, manages a crontab's environment variable.
      - New variables are added on top of crontab.
      - O(name) and O(value) parameters are the name and the value of environment variable.
    type: bool
    default: false
    version_added: "2.1"
  insertafter:
    description:
      - Used with O(state=present) and O(env).
      - If specified, the environment variable will be inserted after the declaration of specified environment variable.
    type: str
    version_added: "2.1"
  insertbefore:
    description:
      - Used with O(state=present) and O(env).
      - If specified, the environment variable will be inserted before the declaration of specified environment variable.
    type: str
    version_added: "2.1"
requirements:
  - cron (any 'vixie cron' conformant variant, like cronie)
notes:
  - If you are experiencing permissions issues with cron and MacOS,
    you should see the official MacOS documentation for further information.
author:
  - Dane Summers (@dsummersl)
  - Mike Grozak (@rhaido)
  - Patrick Callahan (@dirtyharrycallahan)
  - Evan Kaufman (@EvanK)
  - Luca Berruti (@lberruti)
extends_documentation_fragment:
    - action_common_attributes
attributes:
    check_mode:
        support: full
    diff_mode:
        support: full
    platform:
        support: full
        platforms: posix
a  
- name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /dev/null"
  ansible.builtin.cron:
    name: "check dirs"
    minute: "0"
    hour: "5,2"
    job: "ls -alh > /dev/null"

- name: 'Ensure an old job is no longer present. Removes any job that is prefixed by "#Ansible: an old job" from the crontab'
  ansible.builtin.cron:
    name: "an old job"
    state: absent

- name: Creates an entry like "@reboot /some/job.sh"
  ansible.builtin.cron:
    name: "a job for reboot"
    special_time: reboot
    job: "/some/job.sh"

- name: Creates an entry like "PATH=/opt/bin" on top of crontab
  ansible.builtin.cron:
    name: PATH
    env: yes
    job: /opt/bin

- name: Creates an entry like "APP_HOME=/srv/app" and insert it after PATH declaration
  ansible.builtin.cron:
    name: APP_HOME
    env: yes
    job: /srv/app
    insertafter: PATH

- name: Creates a cron file under /etc/cron.d
  ansible.builtin.cron:
    name: yum autoupdate
    weekday: "2"
    minute: "0"
    hour: "12"
    user: root
    job: "YUMINTERACTIVE=0 /usr/sbin/yum-autoupdate"
    cron_file: ansible_yum-autoupdate

- name: Removes a cron file from under /etc/cron.d
  ansible.builtin.cron:
    name: "yum autoupdate"
    cron_file: ansible_yum-autoupdate
    state: absent

- name: Removes "APP_HOME" environment variable from crontab
  ansible.builtin.cron:
    name: APP_HOME
    env: yes
    state: absent
#N)AnsibleModule)S_IRWU_RWG_RWO)to_bytes	to_native)shlex_quotec                      e Zd Zy)CronTabErrorN)__name__
__module____qualname__     D/home/dcms/DCMS/lib/python3.12/site-packages/ansible/modules/cron.pyr   r      s    r   r   c                      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d Zd ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Zy)CronTabz
        CronTab object to write time based crontab file

        user      - the user of the crontab (defaults to current user)
        cron_file - a cron file under /etc/cron.d, or an absolute path
    Nc                   || _         || _        t        j                         dk(  | _        d | _        d| _        d| _        | j                   j                  dd      | _	        |rt        j                  j                  |      r|| _        t        |d      | _        n]t        j                  j                  d	|      | _        t        j                  j                  d
t        |d            | _        nd | _        | j!                          y )Nr   z
#Ansible:  crontabT)requiredsurrogate_or_stricterrorsz/etc/cron.ds   /etc/cron.d)moduleuserosgetuidrootlinesansible
n_existingget_bin_pathcron_cmdpathisabs	cron_filer   b_cron_filejoinread)selfr   r   r'   s       r   __init__zCronTab.__init__   s    	YY[A%	
#00T0Jww}}Y'!*#+I>S#T !#mY!G#%77<<[p@q#r !DN		r   c                ~   g | _         | j                  rg	 t        | j                  d      }t	        |j                         d      | _        | j                  j                         | _         |j                          y | j                  j                  | j!                         d      \  }}}|dk7  r|dk7  rt        d	      || _        |j                         }d}|D ]  }|d
kD  sBt#        j$                  d|      sHt#        j$                  d|      s2t#        j$                  d|      s| j                   j'                  |       n?t#        j(                  |      dz   }t#        j*                  |d| j                  d      | _        |dz  } y # t        $ r Y y t        $ r" t        dt        j                         d         w xY w)Nrbr   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.*\)z[
]?r   )r    r'   openr(   r   r*   r"   
splitlinescloseIOError	Exceptionr   sysexc_infor   run_command_read_user_executerematchappendescapesub)	r+   frcouterrr    countlpatterns	            r   r*   zCronTab.read  sw   
>>	K))40"+AFFH=R"S!__779
	 "[[44T5L5L5Nae4fNRcQw27"#;<<!DONN$EE 19RXX.ikl%m%'XX.La%P%'XX.BA%FJJ%%a( iilY6G&(ffWb$//1&MDO
!   K"#6q8IJJKs   A%F 	F<*F<c                |    t        | j                        dk(  ry| j                  D ]  }|j                         s y yNr   TF)lenr    strip)r+   lines     r   is_emptyzCronTab.is_empty'  s9    tzz?a

 !::< ! r   c                   |rt        |d      }nl| j                  rt        | j                  d      }nIt        j                  d      \  }}t        j                  |t               t        j                  |d      }|j                  t        | j                                      |j                          |ry| j                  sf| j                  j                  | j                        d      \  }}}t        j                   |       |dk7  r| j                  j#                  |       | j                  j%                         r4| j                  r'| j                  j'                  | j                  d	       yyy)
zI
        Write the crontab to the system. Saves all information.
        wbr   prefixNTr0   r   msgF)r4   r'   r(   tempfilemkstempr   chmodr   fdopenwriter   renderr6   r   r;   _write_executeunlink	fail_jsonselinux_enabledset_default_selinux_context)r+   backup_filefilehfiledr%   rC   rD   rE   s           r   rY   zCronTab.write0  s    d+E^^))40E"**)<KE4HHT>*IIeT*EHT[[]+,  ~~![[44T5H5H5Nae4fNRcIIdOQw%%#%. ;;&&(T^^KK33DNNEJ .<(r   c                "    | j                   |S N)r!   r+   names     r   
do_commentzCronTab.do_commentQ  s    t,,r   c                    | j                   j                  | j                  |             | j                   j                  d|z         y N%s)r    r?   rg   r+   rf   jobs      r   add_jobzCronTab.add_jobT  s5    

$//$/0 	

$#,'r   c                <    | j                  ||| j                        S rd   )_update_job
do_add_jobrk   s      r   
update_jobzCronTab.update_job[  s    c4??;;r   c                N    |j                  |       |j                  d|z         y ri   r?   r+   r    commentrl   s       r   rp   zCronTab.do_add_job^  s    WTS\"r   c                <    | j                  |d| j                        S Nr   )ro   do_remove_jobre   s     r   
remove_jobzCronTab.remove_jobc      b$*<*<==r   c                     y rd   r   rt   s       r   rx   zCronTab.do_remove_jobf      r   c                2   |s|s| j                   j                  d|       y |r|}n|r|}| j                        }t        |      dkD  r/|r	|d   dz   }n|r|d   }| j                   j                  |       y | j                  j                  d|z         y )Nr   r2   zVariable named '%s' not found.rS   )r    insertfind_envrK   r   r]   )r+   declinsertafterinsertbefore
other_name
other_declindexs          r   add_envzCronTab.add_envi  s    |JJa&$J%J]]:.
z?Q"1)"1JJeT*"BZ"OPr   c                <    | j                  ||| j                        S rd   )_update_env
do_add_env)r+   rf   r   s      r   
update_envzCronTab.update_env}  s    dDOO<<r   c                &    |j                  |       y rd   rs   r+   r    r   s      r   r   zCronTab.do_add_env  s    Tr   c                <    | j                  |d| j                        S rw   )r   do_remove_envre   s     r   
remove_envzCronTab.remove_env  rz   r   c                     y rd   r   r   s      r   r   zCronTab.do_remove_env  r|   r   c                    	 t        j                  | j                         y# t        $ r Y yt        $ r" t        dt        j                         d         w xY w)NTFr/   r   )r   r\   r'   OSErrorr8   r   r9   r:   )r+   s    r   remove_job_filezCronTab.remove_job_file  sP    	GIIdnn% 	 	G2CLLN14EFF	Gs   " 	A*Ac                   d }| j                   D ]Z  }|||k(  r||gc S d }t        j                  d| j                  z  |      s7t        j                  d| j                  z  d|      }\ |rt        | j                         D ]  \  }}||k(  st        j                  d| j                  z  | j                   |dz
           s?| j                   j                  || j                  |             | j                   |   |dgc S |s| j                   |dz
     | j                  d       k(  s| j                  |      | j                   |dz
  <   | j                   |dz
     |dgc S  g S )Nrj   r   r2   T)r    r=   r>   r!   rA   	enumerater~   rg   )r+   rf   rl   ru   rG   is         r   find_jobzCronTab.find_job  sB    	>A"d?#Q<'"G%$,,.2&&!5r1=	> !$**- 	<1888EDLL$8$**QU:KL

))!T__T-BC $

1q$77$**QU"3tt7L"L,0OOD,A

1q5) $

1q5 11d;;	< 	r   c                ~    t        | j                        D ]$  \  }}t        j                  d|z  |      s ||gc S  g S Nz^%s=)r   r    r=   r>   )r+   rf   r   rG   s       r   r   zCronTab.find_env  sA    !$**- 	"HE1xx$*qz!	" 	r   c	                   |j                  d      }|rd}	nd}	|r-| j                  r|	d|d| j                  d|S |	d|d|S | j                  r"|	|d|d|d|d|d| j                  d|S |	|d|d|d|d|d|S )N
r   r   @ )rL   r'   r   )
r+   minutehourdaymonthweekdayrl   specialdisableddisable_prefixs
             r   get_cron_jobzCronTab.get_cron_job  s    ii NN~~(6CPP%3WcBB~~3A64QTV[]dfjfofoqtuu0>cSXZacfggr   c                    g }| j                   D ]Y  }t        j                  d| j                  z  |      s'|j	                  t        j
                  d| j                  z  d|             [ |S )Nrj   r   )r    r=   r>   r!   r?   rA   )r+   jobnamesrG   s      r   get_jobnameszCronTab.get_jobnames  s\     	EAxx,a0ut||';R CD	E r   c                    g }| j                   D ]<  }t        j                  d|      s|j                  |j	                  d      d          > |S )Nz^\S+==r   )r    r=   r>   r?   split)r+   envnamesrG   s      r   get_envnameszCronTab.get_envnames  sG     	1Axx!$Q0	1 r   c                    | j                  |      }g }d }| j                  D ]*  }| ||||       d }||k(  r|}|j                  |       , || _        t        |      dk(  ryyrJ   )rg   r    r?   rK   )r+   rf   rl   addlinesfunctionansiblenamenewlinesru   rG   s           r   ro   zCronTab._update_job  sx    ood+ 	#A" 7C8k!"	# 
x=Ar   c                    g }| j                   D ]6  }t        j                  d|z  |      r
 |||       &|j                  |       8 || _         y r   )r    r=   r>   r?   )r+   rf   r   addenvfunctionr   rG   s         r   r   zCronTab._update_env  sK     	#Axx$*x."		# 
r   c                    g }| j                   D ]  }|j                  |        dj                  |      }|r|j                  d      dz   }|S )zD
        Render this crontab as it would be in the crontab.
        
r   )r    r?   r)   rstrip)r+   cronscronresults       r   rZ   zCronTab.render  sS     JJ 	DLL	 5!]]6*T1F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-UXr   z-lr   -u %s	r   platformsystemr	   r$   pwdgetpwuidr   r   )r+   r   s     r   r<   zCronTab._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   s      r   r[   zCronTab._write_execute  s     99 $==		*K,={499?UW[WdWdfqrvfwy ybiik*1-:TYY!77!]]D+d2CDDr   )NNrd   )r   r   r   __doc__r,   r*   rN   rY   rg   rm   rq   rp   ry   rx   r   r   r   r   r   r   r   r   r   r   r   ro   r   rZ   r<   r[   r   r   r   r   r      s    ,"HKB-(<#
>Q(=>G6h(*	8 Er   r   c                    t        t        dNi dt        dd      dt        d      dt        ddg	      d
t        d      dt        ddddg      dt        dd      dt        dd      dt        dd      dt        dddg      dt        dd      dt        dddg      dt        dg d      d t        dd      d!t        dd      d"t        d      d#t        d      dd"d#gg$      } | 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    }| j                  d!   }| j                  d"   }| j                  d#   }|dk(  }d}t               }t               }|rr|d%k(  r| j	                  d&'       t
        j                  j                  |      }t        j                  d(|t        j                        s|j                  d)|z  d*z          t        j                  t        d+d,             t        | ||      }| j                  d-|z         | j                   rYt               }|j"                  |d.<   |j$                  r|j$                  |d/<   n$|j&                  rd0|j&                  z  |d/<   nd1|d/<   |r,d|||	|
|fD cg c]  }|dk7  	 c}v r| j	                  d2'       |r)t)        j*                         d3k(  r| j	                  d4'       |rB|r|s| j	                  d5'       || j	                  d6'       |s|r|s| j	                  d7'       |r6| j,                  s*t/        j0                  d18      \  }}|j3                  |       |rd9|v r| j	                  d:'       |d;|d<}|j5                  |      }|rQt7        |      d=k(  r|j9                  |||       d}t7        |      d=kD  r|d>   |k7  r|j;                  ||       d}nt7        |      d=kD  r|j=                  |       d}n|rd?D ](  }||j?                  d@      v s|j                  dA        n |jA                  |||	|
||||      }|jC                  ||      }t7        |      d=k(  r|jE                  ||       d}t7        |      d=kD  r|d>   |k7  r|jG                  ||       d}t7        |      dBkD  r|jG                  ||       d}n|jC                  |      }t7        |      d=kD  r|jI                  |       d}|j$                  r|jK                         r|| j                   rdCdD<   dE|dF<   n
t               }| j,                  r*t
        j                  jM                  |j$                        }n|jO                         }| jQ                  ||||G       |sG|j"                  dCk7  r8|j"                  jS                  dH      s|j"                  jS                  dI      sd}t        |jU                         |jW                         ||J      }|r| j,                  s|j3                          | j                   rX|jY                         dD<   |j$                  r|j$                  |dF<   n$|j&                  rd0|j&                  z  |dF<   nd1|dF<   ||dK<   |r)| j,                  s|r|dL<   nt        jZ                         |r||d
<    | jP                  dNi | | jQ                  dM'       y c c}w )ONrf   strT)typer   r   )r   rl   value)r   aliasesr'   r%   statepresentabsent)r   defaultchoicesbackupboolF)r   r   r   *r   r   dom)r   r   r   r   r   dowspecial_time)rebootyearlyannuallymonthlyweeklydailyhourly)r   r   r   envr   r   )argument_specsupports_check_modemutually_exclusivez/etc/crontabz>Will not manage /etc/crontab via cron_file, see documentation.rS   z^[A-Z0-9_-]+$z3Filename portion of cron_file ("%s") should consistzJ solely of upper- and lower-case letters, digits, underscores, and hyphens022   zcron instantiated - name: "%s"beforebefore_headerzcrontab for user "%s"r   z6You must specify time and date fields or special time.r   z4Solaris does not support special_time=... or @rebootz@To use cron_file=... parameter you must specify user=... as wellz<You must specify 'job' to install a new cron job or variablezCInsertafter and insertbefore parameters are valid only with env=yesrQ   r   z%Invalid name for environment variablez=""r   r2   )r   r   z"Job should not contain line breaksr3   r   afterz	/dev/nullafter_header)changedr'   r   diffr   r   )jobsenvswarningsr   r   r`   zUnable to execute cron task.r   ).r   dictparamslistr]   r   r%   basenamer=   searchIr?   umaskintr   debug_diffr"   r'   r   r   r   
check_moderU   rV   rY   r   rK   r   r   r   rL   r   r   rm   rq   ry   rN   isfiler   	exit_jsonendswithr   r   rZ   r\   )r   rf   r   rl   r'   r   r   r   r   r   r   r   r   r   r   r   r   
do_installr   res_argsr   cron_file_basenamer   r   xbackuphr`   r   old_declcharold_jobs                                  r   mainr  &  sh   *  
540
5!
 %'3
 '	

 E9y(>ST
 VU3
 UC0
 5#.
 %ug>
 E3/
 eS5'B
 52z{
 vu5
 &%0
 %(
  5)!
$ !N+
)F2 == D== D
--
Ck*IMM'"E]]8$F]]8$F== D
--
CMM'"EmmI&G==0L}}Z(H
--
C--.K==0L)#JGvHvH&!abWW--i8yy)+=rttDOOQTffhi j HHS]fdI.G
LL1D89||v ++X$+$5$5D!||(?',,(N_%(1_% fdC%HI!s(IIUV )W4STT!cd;!_`<!fg f''!)!1!1!C+k"
$;!HI #&##D)8}!k<@8}q Xa[D%8""4.8}q ""4($ 399V,,OO$HI
 &&vtS%#|]efC&&tS1G7|q c*7|aGAJ#$5""4-7|a""4-&&t,G7|a""4($$)9)9);||(*W/:^,#v(("$''..1B1B"C")"9"9";$$W	QV]a$b w))R/""++D1W5G5G5P5PQU5VG!!#!!#	H   MMO<<#NN,DM  '.'8'8^$<<+BW\\+QD(+4D(#HV f''&1H]#IIk" )F x  78i Js   /]?__main__)
__future__r   DOCUMENTATIONEXAMPLESRETURNr   r   r   r=   r9   rU   ansible.module_utils.basicr    ansible.module_utils.common.filer   +ansible.module_utils.common.text.convertersr   r   ansible.module_utils.six.movesr	   r8   r   objectr   r  r   r   r   r   <module>r     sx    #L\5n 
 	  
 	 
  4 ; K 6	9 	~Ef ~EB
V9r zF r   