| 1 |
# (c) 2012, Michael DeHaan <[email protected]> |
| 2 |
# (c) 2013, Dylan Martin <[email protected]> |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
from __future__ import annotations |
| 19 |
|
| 20 |
import os |
| 21 |
|
| 22 |
from ansible.errors import AnsibleActionFail |
| 23 |
from ansible.module_utils.parsing.convert_bool import boolean |
| 24 |
from ansible.module_utils.urls import mask_url |
| 25 |
from ansible.plugins.action import ActionBase |
| 26 |
|
| 27 |
|
| 28 |
class ActionModule(ActionBase): |
| 29 |
|
| 30 |
TRANSFERS_FILES = True |
| 31 |
|
| 32 |
def run(self, tmp=None, task_vars=None): |
| 33 |
""" handler for unarchive operations """ |
| 34 |
if task_vars is None: |
| 35 |
task_vars = dict() |
| 36 |
|
| 37 |
super(ActionModule, self).run(tmp, task_vars) |
| 38 |
del tmp # tmp no longer has any effect |
| 39 |
|
| 40 |
source = self._task.args.get('src', None) |
| 41 |
dest = self._task.args.get('dest', None) |
| 42 |
remote_src = boolean(self._task.args.get('remote_src', False), strict=False) |
| 43 |
creates = self._task.args.get('creates', None) |
| 44 |
decrypt = self._task.args.get('decrypt', True) |
| 45 |
|
| 46 |
try: |
| 47 |
# "copy" is deprecated in favor of "remote_src". |
| 48 |
if 'copy' in self._task.args: |
| 49 |
# They are mutually exclusive. |
| 50 |
if 'remote_src' in self._task.args: |
| 51 |
raise AnsibleActionFail("parameters are mutually exclusive: ('copy', 'remote_src')") |
| 52 |
# We will take the information from copy and store it in |
| 53 |
# the remote_src var to use later in this file. |
| 54 |
self._task.args['remote_src'] = remote_src = not boolean(self._task.args.pop('copy'), strict=False) |
| 55 |
|
| 56 |
if source is None or dest is None: |
| 57 |
raise AnsibleActionFail("src (or content) and dest are required") |
| 58 |
|
| 59 |
if creates: |
| 60 |
# do not run the command if the line contains creates=filename |
| 61 |
# and the filename already exists. This allows idempotence |
| 62 |
# of command executions. |
| 63 |
creates = self._remote_expand_user(creates) |
| 64 |
if self._remote_file_exists(creates): |
| 65 |
return dict( |
| 66 |
msg=f'skipped, since {creates} exists', |
| 67 |
changed=False, |
| 68 |
skipped=True, # deprecated: description='remove this skipped return', core_version='2.25' |
| 69 |
) |
| 70 |
|
| 71 |
dest = self._remote_expand_user(dest) # CCTODO: Fix path for Windows hosts. |
| 72 |
|
| 73 |
# if not remote, we need to get source at controller |
| 74 |
if not remote_src: |
| 75 |
if '://' in source: |
| 76 |
# TODO: implement using open_url? |
| 77 |
raise AnsibleActionFail(f"Unsupported option, an URI src ({mask_url(source)}) is only supported when remote_src is True") |
| 78 |
else: |
| 79 |
source = os.path.expanduser(source) |
| 80 |
source = self._loader.get_real_file(self._find_needle('files', source), decrypt=decrypt) |
| 81 |
|
| 82 |
remote_stat = self._execute_remote_stat(dest, all_vars=task_vars, follow=True) |
| 83 |
|
| 84 |
if not remote_stat['exists'] or not remote_stat['isdir']: |
| 85 |
raise AnsibleActionFail("dest '%s' must be an existing dir" % dest) |
| 86 |
|
| 87 |
if not remote_src: |
| 88 |
# transfer the file to a remote tmp location |
| 89 |
tmp_src = self._connection._shell.join_path(self._connection._shell.tmpdir, 'source') |
| 90 |
self._transfer_file(source, tmp_src) |
| 91 |
|
| 92 |
# handle diff mode client side |
| 93 |
# handle check mode client side |
| 94 |
|
| 95 |
# remove action plugin only keys |
| 96 |
new_module_args = self._task.args.copy() |
| 97 |
for key in ('decrypt',): |
| 98 |
if key in new_module_args: |
| 99 |
del new_module_args[key] |
| 100 |
|
| 101 |
if not remote_src: |
| 102 |
# fix file permissions when the copy is done as a different user |
| 103 |
self._fixup_perms2((self._connection._shell.tmpdir, tmp_src)) |
| 104 |
new_module_args['src'] = tmp_src |
| 105 |
|
| 106 |
# execute the unarchive module now, with the updated args (using ansible.legacy prefix to eliminate collections |
| 107 |
# collisions with local override |
| 108 |
return self._execute_module(module_name='ansible.legacy.unarchive', module_args=new_module_args, task_vars=task_vars) |
| 109 |
finally: |
| 110 |
self._remove_tmp_path(self._connection._shell.tmpdir) |