@ -193,9 +193,8 @@ from ansible.module_utils._text import to_bytes, to_native
def write_changes ( module , b_lines , dest ) :
def write_changes ( module , b_lines , dest ) :
tmpfd , tmpfile = tempfile . mkstemp ( )
tmpfd , tmpfile = tempfile . mkstemp ( )
f = os . fdopen ( tmpfd , ' wb ' )
with open ( tmpfile , ' wb ' ) as f :
f . writelines ( b_lines )
f . writelines ( b_lines )
f . close ( )
validate = module . params . get ( ' validate ' , None )
validate = module . params . get ( ' validate ' , None )
valid = not validate
valid = not validate
@ -247,14 +246,13 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
b_lines = [ ]
b_lines = [ ]
else :
else :
f = open ( b_dest , ' rb ' )
with open ( b_dest , ' rb ' ) as f :
b_lines = f . readlines ( )
b_lines = f . readlines ( )
f . close ( )
if module . _diff :
if module . _diff :
diff [ ' before ' ] = to_native ( b ( ' ' ) . join ( b_lines ) )
diff [ ' before ' ] = to_native ( b ( ' ' ) . join ( b_lines ) )
if regexp is not None :
if regexp :
bre_m = re . compile ( to_bytes ( regexp , errors = ' surrogate_or_strict ' ) )
bre_m = re . compile ( to_bytes ( regexp , errors = ' surrogate_or_strict ' ) )
if insertafter not in ( None , ' BOF ' , ' EOF ' ) :
if insertafter not in ( None , ' BOF ' , ' EOF ' ) :
@ -270,7 +268,7 @@ def present(module, dest, regexp, line, insertafter, insertbefore, create,
m = None
m = None
b_line = to_bytes ( line , errors = ' surrogate_or_strict ' )
b_line = to_bytes ( line , errors = ' surrogate_or_strict ' )
for lineno , b_cur_line in enumerate ( b_lines ) :
for lineno , b_cur_line in enumerate ( b_lines ) :
if regexp is not None :
if regexp :
match_found = bre_m . search ( b_cur_line )
match_found = bre_m . search ( b_cur_line )
else :
else :
match_found = b_line == b_cur_line . rstrip ( b ( ' \r \n ' ) )
match_found = b_line == b_cur_line . rstrip ( b ( ' \r \n ' ) )
@ -406,21 +404,20 @@ def absent(module, dest, regexp, line, backup):
' before_header ' : ' %s (content) ' % dest ,
' before_header ' : ' %s (content) ' % dest ,
' after_header ' : ' %s (content) ' % dest }
' after_header ' : ' %s (content) ' % dest }
f = open ( b_dest , ' rb ' )
with open ( b_dest , ' rb ' ) as f :
b_lines = f . readlines ( )
b_lines = f . readlines ( )
f . close ( )
if module . _diff :
if module . _diff :
diff [ ' before ' ] = to_native ( b ( ' ' ) . join ( b_lines ) )
diff [ ' before ' ] = to_native ( b ( ' ' ) . join ( b_lines ) )
if regexp is not None :
if regexp :
bre_c = re . compile ( to_bytes ( regexp , errors = ' surrogate_or_strict ' ) )
bre_c = re . compile ( to_bytes ( regexp , errors = ' surrogate_or_strict ' ) )
found = [ ]
found = [ ]
b_line = to_bytes ( line , errors = ' surrogate_or_strict ' )
b_line = to_bytes ( line , errors = ' surrogate_or_strict ' )
def matcher ( b_cur_line ) :
def matcher ( b_cur_line ) :
if regexp is not None :
if regexp :
match_found = bre_c . search ( b_cur_line )
match_found = bre_c . search ( b_cur_line )
else :
else :
match_found = b_line == b_cur_line . rstrip ( b ( ' \r \n ' ) )
match_found = b_line == b_cur_line . rstrip ( b ( ' \r \n ' ) )
@ -480,13 +477,15 @@ def main():
backrefs = params [ ' backrefs ' ]
backrefs = params [ ' backrefs ' ]
path = params [ ' path ' ]
path = params [ ' path ' ]
firstmatch = params [ ' firstmatch ' ]
firstmatch = params [ ' firstmatch ' ]
regexp = params [ ' regexp ' ]
line = params . get ( ' line ' , None )
b_path = to_bytes ( path , errors = ' surrogate_or_strict ' )
b_path = to_bytes ( path , errors = ' surrogate_or_strict ' )
if os . path . isdir ( b_path ) :
if os . path . isdir ( b_path ) :
module . fail_json ( rc = 256 , msg = ' Path %s is a directory ! ' % path )
module . fail_json ( rc = 256 , msg = ' Path %s is a directory ! ' % path )
if params [ ' state ' ] == ' present ' :
if params [ ' state ' ] == ' present ' :
if backrefs and params [ ' regexp ' ] is None :
if backrefs and not regexp :
module . fail_json ( msg = ' regexp= is required with backrefs=true ' )
module . fail_json ( msg = ' regexp= is required with backrefs=true ' )
if params . get ( ' line ' , None ) is None :
if params . get ( ' line ' , None ) is None :
@ -500,13 +499,13 @@ def main():
line = params [ ' line ' ]
line = params [ ' line ' ]
present ( module , path , params[ ' regexp' ] , line ,
present ( module , path , regexp, line ,
ins_aft , ins_bef , create , backup , backrefs , firstmatch )
ins_aft , ins_bef , create , backup , backrefs , firstmatch )
else :
else :
if params [ ' regexp ' ] is None and params . get ( ' line ' , None ) is None :
if not regexp and line is None :
module . fail_json ( msg = ' one of line= or regexp= is required with state=absent ' )
module . fail_json ( msg = ' one of line= or regexp= is required with state=absent ' )
absent ( module , path , params[ ' regexp' ] , params. get ( ' line' , None ) , backup )
absent ( module , path , regexp, line, backup )
if __name__ == ' __main__ ' :
if __name__ == ' __main__ ' :