diff options
| author | Egor Tensin <egor@tensin.name> | 2025-11-23 19:16:52 +0100 |
|---|---|---|
| committer | Egor Tensin <egor@tensin.name> | 2025-11-23 21:28:40 +0100 |
| commit | 66913759ffbc86b101f0d69f372cad5186a729f2 (patch) | |
| tree | fdbc26666cba63ba20c3af9a2112c62a9be5db64 | |
| parent | plugins: add apt_wrapper (diff) | |
| download | infra-ansible-66913759ffbc86b101f0d69f372cad5186a729f2.tar.gz infra-ansible-66913759ffbc86b101f0d69f372cad5186a729f2.zip | |
plugins: factor out read_file
| -rwxr-xr-x | plugins/action/apt_wrapper.py | 41 | ||||
| -rwxr-xr-x | plugins/action/read_file.py | 15 | ||||
| -rw-r--r-- | plugins/module_utils/file.py | 34 |
3 files changed, 53 insertions, 37 deletions
diff --git a/plugins/action/apt_wrapper.py b/plugins/action/apt_wrapper.py index 00decc2..b322e10 100755 --- a/plugins/action/apt_wrapper.py +++ b/plugins/action/apt_wrapper.py @@ -1,11 +1,9 @@ #!/usr/bin/python -import base64 import re -from ansible.errors import AnsibleActionFail -from ansible.module_utils.common.text.converters import to_text from ansible.plugins.action import ActionBase +from ansible_collections.tensin.infra.plugins.module_utils import file LOG_PATH = '/var/log/dpkg.log' @@ -14,10 +12,7 @@ class ActionModule(ActionBase): def run(self, tmp=None, task_vars=None): super().run(tmp, task_vars) - if self._check_dpkg_log(task_vars): - before = self._parse_dpkg_log(task_vars) - else: - before = [] + before = self._parse_dpkg_log(task_vars, default='') apt_result = self._execute_module( module_name='ansible.builtin.apt', @@ -30,38 +25,10 @@ class ActionModule(ActionBase): apt_result['packages'] = after[len(before):] return apt_result - def _check_dpkg_log(self, task_vars): - return self._is_file_readable(LOG_PATH, task_vars) - - def _parse_dpkg_log(self, task_vars): - contents = self._read_file(LOG_PATH, task_vars) + def _parse_dpkg_log(self, task_vars, default=None): + contents = file.read_file(self, LOG_PATH, task_vars, default=default) lines = contents.splitlines() regex = re.compile(' install | upgrade | remove | purge ') lines = [line for line in lines if regex.search(line)] lines = [' '.join(line.split(' ')[2:]) for line in lines] return lines - - def _is_file_readable(self, path, task_vars): - stat_result = self._execute_module( - module_name='ansible.builtin.stat', - module_args=dict( - path=path, - get_attributes=False, - get_checksum=False, - get_mime=False, - ), - task_vars=task_vars, - ) - if stat_result.get('failed'): - raise AnsibleActionFail(f"Couldn't stat file {path}", result=stat_result) - return stat_result['stat']['readable'] - - def _read_file(self, path, task_vars): - read_result = self._execute_module( - module_name='ansible.builtin.slurp', - module_args=dict(src=path), - task_vars=task_vars, - ) - if read_result.get('failed'): - raise AnsibleActionFail(f"Couldn't read file {path}", result=read_result) - return base64.b64decode(to_text(read_result['content'])).decode('utf-8') diff --git a/plugins/action/read_file.py b/plugins/action/read_file.py new file mode 100755 index 0000000..3370846 --- /dev/null +++ b/plugins/action/read_file.py @@ -0,0 +1,15 @@ +#!/usr/bin/python + +from ansible.plugins.action import ActionBase +from ansible_collections.tensin.infra.plugins.module_utils import file + + +class ActionModule(ActionBase): + def run(self, tmp=None, task_vars=None): + result = super().run(tmp, task_vars) + + path = self._task.args['path'] + default = self._task.args.get('default') + + result['content'] = file.read_file(self, path, task_vars, default=default) + return result diff --git a/plugins/module_utils/file.py b/plugins/module_utils/file.py new file mode 100644 index 0000000..86a4f27 --- /dev/null +++ b/plugins/module_utils/file.py @@ -0,0 +1,34 @@ +import base64 + +from ansible.errors import AnsibleActionFail +from ansible.module_utils.common.text.converters import to_text + + +def does_file_exist(action, path, task_vars): + stat_result = action._execute_module( + module_name='ansible.builtin.stat', + module_args=dict( + path=path, + get_attributes=False, + get_checksum=False, + get_mime=False, + ), + task_vars=task_vars, + ) + if stat_result.get('failed'): + raise AnsibleActionFail(f"Couldn't stat file {path}", result=stat_result) + return stat_result['stat']['exists'] + + +def read_file(action, path, task_vars, default=False): + if default is not None: + if not does_file_exist(action, path, task_vars): + return default + read_result = action._execute_module( + module_name='ansible.builtin.slurp', + module_args=dict(src=path), + task_vars=task_vars, + ) + if read_result.get('failed'): + raise AnsibleActionFail(f"Couldn't read file {path}", result=read_result) + return base64.b64decode(to_text(read_result['content'])).decode('utf-8') |
