From 6b6c4914d2704c8a944500a3800c03b5457537fd Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Mon, 23 Apr 2018 19:38:42 -0700 Subject: [PATCH] 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 --- lib/ansible/module_utils/basic.py | 2 +- lib/ansible/modules/files/file.py | 32 +++++++++++++++----------- lib/ansible/plugins/action/__init__.py | 2 +- lib/ansible/plugins/action/assemble.py | 2 +- 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 80067daaeb..6f3b171e90 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -229,7 +229,7 @@ FILE_COMMON_ARGUMENTS = dict( # 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 - 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'), # not taken by the file module, but other action plugins call the file module so this ignores diff --git a/lib/ansible/modules/files/file.py b/lib/ansible/modules/files/file.py index 25d612405e..7976ac4bb9 100644 --- a/lib/ansible/modules/files/file.py +++ b/lib/ansible/modules/files/file.py @@ -128,12 +128,14 @@ import os import shutil import time -# import module snippets from ansible.module_utils.basic import AnsibleModule -from ansible.module_utils.six import b 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): ''' Find out current state ''' @@ -183,34 +185,36 @@ def recursive_set_attributes(module, b_path, follow, file_args): def main(): + global module + module = AnsibleModule( argument_spec=dict( state=dict(choices=['file', 'directory', 'link', 'hard', 'touch', 'absent'], default=None), path=dict(aliases=['dest', 'name'], required=True, type='path'), original_basename=dict(required=False), # Internal use only, for recursive ops recurse=dict(default=False, type='bool'), - force=dict(required=False, default=False, type='bool'), - follow=dict(required=False, default=True, type='bool'), - 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'), + 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'), # Note: Different default than file_common_args + _diff_peek=dict(default=None), # Internal use only, for internal checks in the action plugins + src=dict(required=False, default=None, type='path'), # Note: Should not be in file_common_args in future ), add_file_common_args=True, supports_check_mode=True ) params = module.params + state = params['state'] recurse = params['recurse'] force = params['force'] - diff_peek = params['diff_peek'] - src = params['src'] - b_src = to_bytes(src, errors='surrogate_or_strict') + diff_peek = params['_diff_peek'] 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'] 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 if diff_peek is not None: @@ -219,16 +223,16 @@ def main(): f = open(b_path, 'rb') head = f.read(8192) f.close() - if b("\x00") in head: + if b"\x00" in head: appears_binary = True except: pass 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) - # state should default to file, but since that creates many conflicts, - # default to 'current' when it exists. if state is None: if prev_state != 'absent': state = prev_state diff --git a/lib/ansible/plugins/action/__init__.py b/lib/ansible/plugins/action/__init__.py index c5cacfe36f..cf77314aa1 100644 --- a/lib/ansible/plugins/action/__init__.py +++ b/lib/ansible/plugins/action/__init__.py @@ -937,7 +937,7 @@ class ActionBase(with_metaclass(ABCMeta, object)): diff = {} 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: diff --git a/lib/ansible/plugins/action/assemble.py b/lib/ansible/plugins/action/assemble.py index 8b69026a76..f8e95c8811 100644 --- a/lib/ansible/plugins/action/assemble.py +++ b/lib/ansible/plugins/action/assemble.py @@ -103,7 +103,7 @@ class ActionModule(ActionBase): raise AnsibleActionFail("src and dest are required") 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() else: try: