Initial cleanup of file module

* Remove use of six.b as Python-2.6+ have byte literals.
* Make AnsibleModule a global object so we'll have access to it in all
  the functions we're going to break this up into.
* Rework the parameters so things that are in file_common_args are used
  from file_common_args or the reason for deviation is documented.
* Remove validate as a parameter: this should be taken care of by
  removing it from params before the copy and template action plugin
  invoke file.
* Rename diff_peek to _diff_peek as it is an internal parameter.
* add module_name execute_module call to assemble so that it is more greppable
This commit is contained in:
Toshio Kuratomi 2018-04-23 19:38:42 -07:00
parent d994595660
commit 6b6c4914d2
4 changed files with 21 additions and 17 deletions

View file

@ -229,7 +229,7 @@ FILE_COMMON_ARGUMENTS = dict(
# The following are not about perms and should not be in a rewritten file_common_args # The following are not about perms and should not be in a rewritten file_common_args
src=dict(), # Maybe dest or path would be appropriate but src is not src=dict(), # Maybe dest or path would be appropriate but src is not
follow=dict(type='bool', default=False), # Maybe follow is appropriate because it determines whether to follow symlinks for permission purposes too follow=dict(type='bool', default=False), # Maybe follow is appropriate because it determines whether to follow symlinks for permission purposes too
force=dict(type='bool'), force=dict(type='bool'),
# not taken by the file module, but other action plugins call the file module so this ignores # not taken by the file module, but other action plugins call the file module so this ignores

View file

@ -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 paths 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

View file

@ -937,7 +937,7 @@ class ActionBase(with_metaclass(ABCMeta, object)):
diff = {} diff = {}
display.debug("Going to peek to see if file has changed permissions") display.debug("Going to peek to see if file has changed permissions")
peek_result = self._execute_module(module_name='file', module_args=dict(path=destination, diff_peek=True), task_vars=task_vars, persist_files=True) peek_result = self._execute_module(module_name='file', module_args=dict(path=destination, _diff_peek=True), task_vars=task_vars, persist_files=True)
if not peek_result.get('failed', False) or peek_result.get('rc', 0) == 0: if not peek_result.get('failed', False) or peek_result.get('rc', 0) == 0:

View file

@ -103,7 +103,7 @@ class ActionModule(ActionBase):
raise AnsibleActionFail("src and dest are required") raise AnsibleActionFail("src and dest are required")
if boolean(remote_src, strict=False): if boolean(remote_src, strict=False):
result.update(self._execute_module(task_vars=task_vars)) result.update(self._execute_module(module_name='assemble', task_vars=task_vars))
raise _AnsibleActionDone() raise _AnsibleActionDone()
else: else:
try: try: