From 5f471b8e5b08c6bf79b3ac3f577c570bdc9d4434 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Sat, 11 Oct 2025 06:09:10 +1300 Subject: [PATCH] refactor dict from literal list (#10891) * refactor dict from literal list * add changelog frag --- changelogs/fragments/10891-dict-refactor.yml | 6 ++++++ plugins/lookup/dependent.py | 2 +- plugins/module_utils/scaleway.py | 5 +---- plugins/modules/pacemaker_cluster.py | 8 ++++---- plugins/modules/pacemaker_resource.py | 6 +++--- plugins/modules/pacemaker_stonith.py | 4 ++-- 6 files changed, 17 insertions(+), 14 deletions(-) create mode 100644 changelogs/fragments/10891-dict-refactor.yml diff --git a/changelogs/fragments/10891-dict-refactor.yml b/changelogs/fragments/10891-dict-refactor.yml new file mode 100644 index 0000000000..63d5e585ff --- /dev/null +++ b/changelogs/fragments/10891-dict-refactor.yml @@ -0,0 +1,6 @@ +minor_changes: + - dependent lookup plugin - refactor dict initialization, no impact to users (https://github.com/ansible-collections/community.general/pull/10891). + - scaleway module_utils - improve code readability, no impact to users (https://github.com/ansible-collections/community.general/pull/10891). + - pacemaker_cluster.py - refactor dict initialization, no impact to users (https://github.com/ansible-collections/community.general/pull/10891). + - pacemaker_resource.py - refactor dict initialization, no impact to users (https://github.com/ansible-collections/community.general/pull/10891). + - pacemaker_stonith.py - refactor dict initialization, no impact to users (https://github.com/ansible-collections/community.general/pull/10891). diff --git a/plugins/lookup/dependent.py b/plugins/lookup/dependent.py index 7331aebfea..4581aabc3f 100644 --- a/plugins/lookup/dependent.py +++ b/plugins/lookup/dependent.py @@ -180,7 +180,7 @@ class LookupModule(LookupBase): if isinstance(values, Mapping): for idx, val in sorted(values.items()): - current[key] = dict([('key', idx), ('value', val)]) + current[key] = dict(key=idx, value=val) self.__process(result, terms, index + 1, current, templar, variables) elif isinstance(values, Sequence): for elt in values: diff --git a/plugins/module_utils/scaleway.py b/plugins/module_utils/scaleway.py index d4cb4eb662..d618123e90 100644 --- a/plugins/module_utils/scaleway.py +++ b/plugins/module_utils/scaleway.py @@ -109,10 +109,7 @@ class SecretVariables(object): @staticmethod def dict_to_list(source_dict): - return [ - dict(key=var[0], value=var[1]) - for var in source_dict.items() - ] + return [dict(key=k, value=v) for k, v in source_dict.items()] @staticmethod def list_to_dict(source_list, hashed=False): diff --git a/plugins/modules/pacemaker_cluster.py b/plugins/modules/pacemaker_cluster.py index cc1e2c22ce..3327bd9228 100644 --- a/plugins/modules/pacemaker_cluster.py +++ b/plugins/modules/pacemaker_cluster.py @@ -87,7 +87,7 @@ class PacemakerCluster(StateModuleHelper): def __init_module__(self): self.runner = pacemaker_runner(self.module) self.vars.set('apply_all', True if not self.module.params['name'] else False) - get_args = dict([('cli_action', 'cluster'), ('state', 'status'), ('name', None), ('apply_all', self.vars.apply_all)]) + get_args = dict(cli_action='cluster', state='status', name=None, apply_all=self.vars.apply_all) if self.module.params['state'] == "maintenance": get_args['cli_action'] = "property" get_args['state'] = "config" @@ -121,9 +121,9 @@ class PacemakerCluster(StateModuleHelper): def _get(self): with self.runner('cli_action state name') as ctx: result = ctx.run(cli_action=self.vars.get_args['cli_action'], state=self.vars.get_args['state'], name=self.vars.get_args['name']) - return dict([('rc', result[0]), - ('out', result[1] if result[1] != "" else None), - ('err', result[2])]) + return dict(rc=result[0], + out=(result[1] if result[1] != "" else None), + err=result[2]) def state_cleanup(self): with self.runner('cli_action state name', output_process=self._process_command_output(True, "Fail"), check_mode_skip=True) as ctx: diff --git a/plugins/modules/pacemaker_resource.py b/plugins/modules/pacemaker_resource.py index 6d5763f004..fa2fc18fe2 100644 --- a/plugins/modules/pacemaker_resource.py +++ b/plugins/modules/pacemaker_resource.py @@ -205,9 +205,9 @@ class PacemakerResource(StateModuleHelper): def _get(self): with self.runner('cli_action state name') as ctx: result = ctx.run(cli_action="resource", state='status') - return dict([('rc', result[0]), - ('out', result[1] if result[1] != "" else None), - ('err', result[2])]) + return dict(rc=result[0], + out=(result[1] if result[1] != "" else None), + err=result[2]) def fmt_as_stack_argument(self, value, arg): if value is not None: diff --git a/plugins/modules/pacemaker_stonith.py b/plugins/modules/pacemaker_stonith.py index 8398123d97..3ca72d03a2 100644 --- a/plugins/modules/pacemaker_stonith.py +++ b/plugins/modules/pacemaker_stonith.py @@ -183,8 +183,8 @@ class PacemakerStonith(StateModuleHelper): def fmt_stonith_operations(self): modified_stonith_operations = [] for stonith_operation in self.vars.stonith_operations: - modified_stonith_operations.append(dict([("operation_action", stonith_operation.get('operation_action')), - ("operation_option", stonith_operation.get('operation_options'))])) + modified_stonith_operations.append(dict(operation_action=stonith_operation.get('operation_action'), + operation_option=stonith_operation.get('operation_options'))) return modified_stonith_operations def state_absent(self):