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

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

View file

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

View file

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