|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from ansible.errors import AnsibleActionFail |
| 4 | +from ansible.plugins.action import ActionBase |
| 5 | +from ansible.utils.display import Display |
| 6 | +from ansible.utils.vars import combine_vars |
| 7 | + |
| 8 | +display = Display() |
| 9 | + |
| 10 | + |
| 11 | +class ActionModule(ActionBase): |
| 12 | + '''Handle path''' |
| 13 | + |
| 14 | + def run(self, tmp=None, task_vars=None): |
| 15 | + |
| 16 | + result = super(ActionModule, self).run(tmp, task_vars) |
| 17 | + |
| 18 | + validation, args = self.validate_argument_spec( |
| 19 | + argument_spec={ |
| 20 | + 'path': {'type': 'path', 'required': True}, |
| 21 | + 'state': {'type': 'str', 'choices': ['present', 'absent', 'file'], 'default': 'present'}, |
| 22 | + 'content': {'type': 'str'}, |
| 23 | + 'template': {'type': 'path'}, |
| 24 | + 'vars': {'type': 'dict', 'default': {}}, |
| 25 | + 'user': {'type': 'str'}, |
| 26 | + 'group': {'type': 'str'}, |
| 27 | + 'mode': {'type': 'raw'}, |
| 28 | + 'validate': {'type': 'str'}, |
| 29 | + }, |
| 30 | + mutually_exclusive=( |
| 31 | + ['content', 'template'], |
| 32 | + ), |
| 33 | + ) |
| 34 | + |
| 35 | + if args['state'] == 'absent': |
| 36 | + display.v('Use "ansible.builtin.file" to ensure "%s" is absent.' % args['path']) |
| 37 | + result.update( |
| 38 | + self._execute_module( |
| 39 | + module_name='ansible.builtin.file', |
| 40 | + module_args={ |
| 41 | + 'path': args['path'], |
| 42 | + 'state': args['state'], |
| 43 | + }, |
| 44 | + task_vars=task_vars, |
| 45 | + ) |
| 46 | + ) |
| 47 | + elif (args['state'] in ['present', 'file']) and args['content'] is not None: |
| 48 | + display.v('Use "ansible.builtin.copy" to ensure "%s" content.' % args['path']) |
| 49 | + task = self._task.copy() |
| 50 | + task.args = { |
| 51 | + 'dest': args['path'], |
| 52 | + 'content': args['content'], |
| 53 | + 'owner': args['user'], |
| 54 | + 'group': args['group'], |
| 55 | + 'mode': args['mode'], |
| 56 | + 'validate': args['validate'], |
| 57 | + } |
| 58 | + result.update( |
| 59 | + self._shared_loader_obj.action_loader.get( |
| 60 | + 'ansible.builtin.copy', |
| 61 | + task=task, |
| 62 | + connection=self._connection, |
| 63 | + play_context=self._play_context, |
| 64 | + loader=self._loader, |
| 65 | + templar=self._templar, |
| 66 | + shared_loader_obj=self._shared_loader_obj, |
| 67 | + ).run(task_vars=combine_vars(task_vars, args['vars'])) |
| 68 | + ) |
| 69 | + elif (args['state'] in ['present', 'file']) and args['template']: |
| 70 | + display.v('Use "ansible.builtin.template" to ensure "%s" template.' % args['path']) |
| 71 | + task = self._task.copy() |
| 72 | + task.args = { |
| 73 | + 'src': args['template'], |
| 74 | + 'dest': args['path'], |
| 75 | + 'owner': args['user'], |
| 76 | + 'group': args['group'], |
| 77 | + 'mode': args['mode'], |
| 78 | + 'validate': args['validate'], |
| 79 | + } |
| 80 | + result.update( |
| 81 | + self._shared_loader_obj.action_loader.get( |
| 82 | + 'ansible.builtin.template', |
| 83 | + task=task, |
| 84 | + connection=self._connection, |
| 85 | + play_context=self._play_context, |
| 86 | + loader=self._loader, |
| 87 | + templar=self._templar, |
| 88 | + shared_loader_obj=self._shared_loader_obj, |
| 89 | + ).run(task_vars=combine_vars(task_vars, args['vars'])) |
| 90 | + ) |
| 91 | + elif args['state'] == 'file': |
| 92 | + raise AnsibleActionFail('one of the following is required: content, template') |
| 93 | + elif args['state'] == 'present': |
| 94 | + display.v('Use "ansible.builtin.file" to ensure "%s" is a directory.' % args['path']) |
| 95 | + result.update( |
| 96 | + self._execute_module( |
| 97 | + module_name='ansible.builtin.file', |
| 98 | + module_args={ |
| 99 | + 'path': args['path'], |
| 100 | + 'state': 'directory', |
| 101 | + 'owner': args['user'], |
| 102 | + 'group': args['group'], |
| 103 | + 'mode': args['mode'], |
| 104 | + }, |
| 105 | + task_vars=task_vars, |
| 106 | + ) |
| 107 | + ) |
| 108 | + |
| 109 | + return result |
0 commit comments