from __future__ import (absolute_import, division, print_function) __metaclass__ = type import os from ansible.errors import AnsibleError, AnsibleAction, AnsibleActionFail, AnsibleActionSkip, AnsibleConnectionFailure from ansible.module_utils._text import to_native from ansible.plugins.action import ActionBase class ActionModule(ActionBase): TRANSFERS_FILES = True def run(self, tmp=None, task_vars=None): if task_vars is None: task_vars = dict() if self._task.environment and any(self._task.environment): self._display.warning('openwrt_sysupgrade module does not support the environment keyword') result = super(ActionModule, self).run(tmp, task_vars) del tmp # tmp no longer has any effect self._cleanup_remote_tmp = False try: if self._play_context.check_mode: raise AnsibleActionSkip('Check mode is not supported for this task.') result['changed'] = True try: image = to_native(self._task.args.get('image', ''), errors='surrogate_or_strict') image = self._loader.get_real_file(self._find_needle('files', image), decrypt=False) except AnsibleError as e: raise AnsibleActionFail(to_native(e)) tmp_img = self._connection._shell.join_path(self._connection._shell.tmpdir, os.path.basename(image)) self._transfer_file(image, tmp_img) self._fixup_perms2((self._connection._shell.tmpdir, tmp_img), execute=False) args = to_native(self._task.args.get('args', ''), errors='surrogate_or_strict') script_cmd = ' '.join(['sysupgrade', args, tmp_img]) script_cmd = self._connection._shell.wrap_for_exec(script_cmd) try: result.update(self._low_level_execute_command(cmd=script_cmd)) except AnsibleConnectionFailure as e: result['rc'] = 0 if 'rc' in result and result['rc'] != 0: raise AnsibleActionFail('non-zero return code') except AnsibleAction as e: result.update(e.result) return result