@ -210,6 +210,7 @@ import sys
import tempfile
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . common . text . converters import to_bytes , to_native
from ansible . module_utils . six . moves import shlex_quote
@ -231,14 +232,16 @@ class CronTab(object):
self . root = ( os . getuid ( ) == 0 )
self . lines = None
self . ansible = " #Ansible: "
self . existing = ' '
self . n_ existing = ' '
self . cron_cmd = self . module . get_bin_path ( ' crontab ' , required = True )
if cron_file :
if os . path . isabs ( cron_file ) :
self . cron_file = cron_file
self . b_cron_file = to_bytes ( cron_file , errors = ' surrogate_or_strict ' )
else :
self . cron_file = os . path . join ( ' /etc/cron.d ' , cron_file )
self . b_cron_file = os . path . join ( b ' /etc/cron.d ' , to_bytes ( cron_file , errors = ' surrogate_or_strict ' ) )
else :
self . cron_file = None
@ -250,9 +253,8 @@ class CronTab(object):
if self . cron_file :
# read the cronfile
try :
f = open ( self . cron_file , ' r ' )
self . existing = f . read ( )
self . lines = self . existing . splitlines ( )
f = open ( self . b_cron_file , ' rb ' )
self . n_existing = to_native ( f . read ( ) , errors = ' surrogate_or_strict ' )
f . close ( )
except IOError :
# cron file does not exist
@ -266,7 +268,7 @@ class CronTab(object):
if rc != 0 and rc != 1 : # 1 can mean that there are no jobs.
raise CronTabError ( " Unable to read crontab " )
self . existing = out
self . n_ existing = out
lines = out . splitlines ( )
count = 0
@ -277,7 +279,7 @@ class CronTab(object):
self . lines . append ( l )
else :
pattern = re . escape ( l ) + ' [ \r \n ]? '
self . existing = re . sub ( pattern , ' ' , self . existing, 1 )
self . n_ existing = re . sub ( pattern , ' ' , self . n_ existing, 1 )
count + = 1
def is_empty ( self ) :
@ -291,15 +293,15 @@ class CronTab(object):
Write the crontab to the system . Saves all information .
"""
if backup_file :
fileh = open ( backup_file , ' w ' )
fileh = open ( backup_file , ' w b ' )
elif self . cron_file :
fileh = open ( self . cron_file, ' w ' )
fileh = open ( self . b_ cron_file, ' w b ' )
else :
filed , path = tempfile . mkstemp ( prefix = ' crontab ' )
os . chmod ( path , int ( ' 0644 ' , 8 ) )
fileh = os . fdopen ( filed , ' w ' )
fileh = os . fdopen ( filed , ' w b ' )
fileh . write ( self . render ( ) )
fileh . write ( to_bytes ( self . render ( ) ) )
fileh . close ( )
# return if making a backup
@ -628,7 +630,7 @@ def main():
if module . _diff :
diff = dict ( )
diff [ ' before ' ] = crontab . existing
diff [ ' before ' ] = crontab . n_ existing
if crontab . cron_file :
diff [ ' before_header ' ] = crontab . cron_file
else :
@ -724,8 +726,8 @@ def main():
changed = True
# no changes to env/job, but existing crontab needs a terminating newline
if not changed and crontab . existing != ' ' :
if not ( crontab . existing. endswith ( ' \r ' ) or crontab . existing. endswith ( ' \n ' ) ) :
if not changed and crontab . n_ existing != ' ' :
if not ( crontab . n_ existing. endswith ( ' \r ' ) or crontab . n_ existing. endswith ( ' \n ' ) ) :
changed = True
res_args = dict (