@ -128,12 +128,14 @@ import os
import shutil
import shutil
import time
import time
# import module snippets
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . basic import AnsibleModule
from ansible . module_utils . six import b
from ansible . module_utils . _text import to_bytes , to_native
from ansible . module_utils . _text import to_bytes , to_native
# There will only be a single AnsibleModule object per module
module = None
def get_state ( b_path ) :
def get_state ( b_path ) :
''' Find out current state '''
''' Find out current state '''
@ -183,34 +185,36 @@ def recursive_set_attributes(module, b_path, follow, file_args):
def main ( ) :
def main ( ) :
global module
module = AnsibleModule (
module = AnsibleModule (
argument_spec = dict (
argument_spec = dict (
state = dict ( choices = [ ' file ' , ' directory ' , ' link ' , ' hard ' , ' touch ' , ' absent ' ] , default = None ) ,
state = dict ( choices = [ ' file ' , ' directory ' , ' link ' , ' hard ' , ' touch ' , ' absent ' ] , default = None ) ,
path = dict ( aliases = [ ' dest ' , ' name ' ] , required = True , type = ' path ' ) ,
path = dict ( aliases = [ ' dest ' , ' name ' ] , required = True , type = ' path ' ) ,
original_basename = dict ( required = False ) , # Internal use only, for recursive ops
original_basename = dict ( required = False ) , # Internal use only, for recursive ops
recurse = dict ( default = False , type = ' bool ' ) ,
recurse = dict ( default = False , type = ' bool ' ) ,
force = dict ( required = False , default = False , type = ' bool ' ) ,
force = dict ( required = False , default = False , type = ' bool ' ) , # Note: Should not be in file_common_args in future
follow = dict ( required = False , default = True , type = ' bool ' ) ,
follow = dict ( required = False , default = True , type = ' bool ' ) , # Note: Different default than file_common_args
diff_peek = dict ( default = None ) , # Internal use only, for internal checks in the action plugins
_diff_peek = dict ( default = None ) , # Internal use only, for internal checks in the action plugins
validate = dict ( required = False , default = None ) , # Internal use only, for template and copy
src = dict ( required = False , default = None , type = ' path ' ) , # Note: Should not be in file_common_args in future
src = dict ( required = False , default = None , type = ' path ' ) ,
) ,
) ,
add_file_common_args = True ,
add_file_common_args = True ,
supports_check_mode = True
supports_check_mode = True
)
)
params = module . params
params = module . params
state = params [ ' state ' ]
state = params [ ' state ' ]
recurse = params [ ' recurse ' ]
recurse = params [ ' recurse ' ]
force = params [ ' force ' ]
force = params [ ' force ' ]
diff_peek = params [ ' diff_peek ' ]
diff_peek = params [ ' _diff_peek ' ]
src = params [ ' src ' ]
b_src = to_bytes ( src , errors = ' surrogate_or_strict ' )
follow = params [ ' follow ' ]
follow = params [ ' follow ' ]
# modify source as we later reload and pass, specially relevant when used by other modules.
# modify path s as we later reload and pass, specially relevant when used by other modules.
path = params [ ' path ' ]
path = params [ ' path ' ]
b_path = to_bytes ( path , errors = ' surrogate_or_strict ' )
b_path = to_bytes ( path , errors = ' surrogate_or_strict ' )
src = params [ ' src ' ]
b_src = to_bytes ( src , errors = ' surrogate_or_strict ' , nonstring = ' passthru ' )
# short-circuit for diff_peek
# short-circuit for diff_peek
if diff_peek is not None :
if diff_peek is not None :
@ -219,16 +223,16 @@ def main():
f = open ( b_path , ' rb ' )
f = open ( b_path , ' rb ' )
head = f . read ( 8192 )
head = f . read ( 8192 )
f . close ( )
f . close ( )
if b ( " \x00 " ) in head :
if b " \x00 " in head :
appears_binary = True
appears_binary = True
except :
except :
pass
pass
module . exit_json ( path = path , changed = False , appears_binary = appears_binary )
module . exit_json ( path = path , changed = False , appears_binary = appears_binary )
# state should default to file, but since that creates many conflicts,
# default state to 'current' when it exists.
prev_state = get_state ( b_path )
prev_state = get_state ( b_path )
# state should default to file, but since that creates many conflicts,
# default to 'current' when it exists.
if state is None :
if state is None :
if prev_state != ' absent ' :
if prev_state != ' absent ' :
state = prev_state
state = prev_state