Debian Codegraph @

ansible_collections/amazon/aws/plugins/action/s3_object.py

raw
1 # -*- coding: utf-8 -*-
2
3 # (c) 2012, Michael DeHaan <[email protected]>
4 # (c) 2018, Will Thames <[email protected]>
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 import os
8
9 from ansible.errors import AnsibleAction
10 from ansible.errors import AnsibleActionFail
11 from ansible.errors import AnsibleError
12 from ansible.errors import AnsibleFileNotFound
13 from ansible.module_utils.common.text.converters import to_text
14 from ansible.plugins.action import ActionBase
15 from ansible.utils.vars import merge_hash
16
17
18 class ActionModule(ActionBase):
19 TRANSFERS_FILES = True
20
21 def run(self, tmp=None, task_vars=None):
22 """handler for s3_object operations
23
24 This adds the magic that means 'src' can point to both a 'remote' file
25 on the 'host' or in the 'files/' lookup path on the controller.
26 """
27 self._supports_async = True
28
29 if task_vars is None:
30 task_vars = dict()
31
32 result = super().run(tmp, task_vars)
33 del tmp # tmp no longer has any effect
34
35 source = self._task.args.get("src", None)
36
37 try:
38 new_module_args = self._task.args.copy()
39 if source:
40 source = os.path.expanduser(source)
41
42 # For backward compatibility check if the file exists on the remote; it should take precedence
43 if not self._remote_file_exists(source):
44 try:
45 source = self._loader.get_real_file(self._find_needle("files", source), decrypt=False)
46 new_module_args["src"] = source
47 except AnsibleFileNotFound:
48 # module handles error message for nonexistent files
49 new_module_args["src"] = source
50 except AnsibleError as e:
51 raise AnsibleActionFail(to_text(e))
52
53 wrap_async = self._task.async_val and not self._connection.has_native_async
54 # execute the s3_object module with the updated args
55 result = merge_hash(
56 result, self._execute_module(module_args=new_module_args, task_vars=task_vars, wrap_async=wrap_async)
57 )
58
59 if not wrap_async:
60 # remove a temporary path we created
61 self._remove_tmp_path(self._connection._shell.tmpdir)
62
63 except AnsibleAction as e:
64 result.update(e.result)
65 return result