diff --git a/changelogs/fragments/10979-mod-utils-fstr.yml b/changelogs/fragments/10979-mod-utils-fstr.yml new file mode 100644 index 0000000000..cf0d84c817 --- /dev/null +++ b/changelogs/fragments/10979-mod-utils-fstr.yml @@ -0,0 +1,6 @@ +minor_changes: + - ibm_sa_utils module utils - use f-strings for string templating (https://github.com/ansible-collections/community.general/pull/10979). + - online module utils - use f-strings for string templating (https://github.com/ansible-collections/community.general/pull/10979). + - opennebula module utils - use f-strings for string templating (https://github.com/ansible-collections/community.general/pull/10979). + - redfish_utils module utils - use f-strings for string templating (https://github.com/ansible-collections/community.general/pull/10979). + - scaleway module utils - use f-strings for string templating (https://github.com/ansible-collections/community.general/pull/10979). diff --git a/plugins/module_utils/ibm_sa_utils.py b/plugins/module_utils/ibm_sa_utils.py index 0c8f3d274d..5153cabd81 100644 --- a/plugins/module_utils/ibm_sa_utils.py +++ b/plugins/module_utils/ibm_sa_utils.py @@ -57,8 +57,7 @@ def connect_ssl(module): endpoints) except errors.CommandFailedConnectionError as e: module.fail_json( - msg="Connection with Spectrum Accelerate system has " - "failed: {[0]}.".format(to_native(e))) + msg=f"Connection with Spectrum Accelerate system has failed: {e}.") def spectrum_accelerate_spec(): diff --git a/plugins/module_utils/online.py b/plugins/module_utils/online.py index 303abffab2..bcd3edf6e3 100644 --- a/plugins/module_utils/online.py +++ b/plugins/module_utils/online.py @@ -60,26 +60,24 @@ class Online(object): def __init__(self, module): self.module = module self.headers = { - 'Authorization': "Bearer %s" % self.module.params.get('api_token'), + 'Authorization': f"Bearer {self.module.params.get('api_token')}", 'User-Agent': self.get_user_agent_string(module), 'Content-type': 'application/json', } self.name = None def get_resources(self): - results = self.get('/%s' % self.name) + results = self.get(f'/{self.name}') if not results.ok: - raise OnlineException('Error fetching {0} ({1}) [{2}: {3}]'.format( - self.name, '%s/%s' % (self.module.params.get('api_url'), self.name), - results.status_code, results.json['message'] - )) + raise OnlineException( + f"Error fetching {self.name} ({self.module.params.get('api_url')}/{self.name}) [{results.status_code}: {results.json['message']}]") return results.json def _url_builder(self, path): if path[0] == '/': path = path[1:] - return '%s/%s' % (self.module.params.get('api_url'), path) + return f"{self.module.params.get('api_url')}/{path}" def send(self, method, path, data=None, headers=None): url = self._url_builder(path) @@ -101,7 +99,7 @@ class Online(object): @staticmethod def get_user_agent_string(module): - return "ansible %s Python %s" % (module.ansible_version, sys.version.split(' ', 1)[0]) + return f"ansible {module.ansible_version} Python {sys.version.split(' ', 1)[0]}" def get(self, path, data=None, headers=None): return self.send('GET', path, data, headers) diff --git a/plugins/module_utils/opennebula.py b/plugins/module_utils/opennebula.py index ce9ec76b0d..bb28266f8f 100644 --- a/plugins/module_utils/opennebula.py +++ b/plugins/module_utils/opennebula.py @@ -53,7 +53,8 @@ def render(to_render): yield f"{key}=[{','.join(recurse(item))}]" continue if isinstance(value, str): - yield '{0:}="{1:}"'.format(key, value.replace('\\', '\\\\').replace('"', '\\"')) + _value = value.replace('\\', '\\\\').replace('"', '\\"') + yield f'{key}="{_value}"' continue yield f'{key}="{value}"' return '\n'.join(recurse(to_render)) diff --git a/plugins/module_utils/redfish_utils.py b/plugins/module_utils/redfish_utils.py index 55332e46c6..53cbc3a1b0 100644 --- a/plugins/module_utils/redfish_utils.py +++ b/plugins/module_utils/redfish_utils.py @@ -2952,7 +2952,7 @@ class RedfishUtils(object): resources, media_types, media_match_strict=False, vendor=vendor) if not uri: return {'ret': False, - 'msg': f"Unable to find an available VirtualMedia resource {('supporting ' + str(media_types)) if media_types else ''}"} + 'msg': f"Unable to find an available VirtualMedia resource {f'supporting {media_types}' if media_types else ''}"} # confirm InsertMedia action found if ('Actions' not in data or diff --git a/plugins/module_utils/scaleway.py b/plugins/module_utils/scaleway.py index 0798e61317..2804ef4e71 100644 --- a/plugins/module_utils/scaleway.py +++ b/plugins/module_utils/scaleway.py @@ -180,13 +180,11 @@ class Scaleway(object): self.name = None def get_resources(self): - results = self.get('/%s' % self.name) + results = self.get(f'/{self.name}') if not results.ok: - raise ScalewayException('Error fetching {0} ({1}) [{2}: {3}]'.format( - self.name, '%s/%s' % (self.module.params.get('api_url'), self.name), - results.status_code, results.json['message'] - )) + raise ScalewayException( + f"Error fetching {self.name} ({self.module.params.get('api_url')}/{self.name}) [{results.status_code}: {results.json['message']}]") return results.json.get(self.name) @@ -198,7 +196,7 @@ class Scaleway(object): if path[0] == '/': path = path[1:] - return '%s/%s?%s' % (self.module.params.get('api_url'), path, query_string) + return f"{self.module.params.get('api_url')}/{path}?{query_string}" def send(self, method, path, data=None, headers=None, params=None): url = self._url_builder(path=path, params=params) @@ -223,7 +221,7 @@ class Scaleway(object): @staticmethod def get_user_agent_string(module): - return "ansible %s Python %s" % (module.ansible_version, sys.version.split(' ', 1)[0]) + return f"ansible {module.ansible_version} Python {sys.version.split(' ', 1)[0]}" def get(self, path, data=None, headers=None, params=None): return self.send(method='GET', path=path, data=data, headers=headers, params=params) @@ -247,21 +245,21 @@ class Scaleway(object): self.module.warn(str(x)) def fetch_state(self, resource): - self.module.debug("fetch_state of resource: %s" % resource["id"]) - response = self.get(path=self.api_path + "/%s" % resource["id"]) + self.module.debug(f"fetch_state of resource: {resource['id']}") + response = self.get(path=f"{self.api_path}/{resource['id']}") if response.status_code == 404: return "absent" if not response.ok: - msg = 'Error during state fetching: (%s) %s' % (response.status_code, response.json) + msg = f'Error during state fetching: ({response.status_code}) {response.json}' self.module.fail_json(msg=msg) try: - self.module.debug("Resource %s in state: %s" % (resource["id"], response.json["status"])) + self.module.debug(f"Resource {resource['id']} in state: {response.json['status']}") return response.json["status"] except KeyError: - self.module.fail_json(msg="Could not fetch state in %s" % response.json) + self.module.fail_json(msg=f"Could not fetch state in {response.json}") def fetch_paginated_resources(self, resource_key, **pagination_kwargs): response = self.get( @@ -270,9 +268,7 @@ class Scaleway(object): status_code = response.status_code if not response.ok: - self.module.fail_json(msg='Error getting {0} [{1}: {2}]'.format( - resource_key, - response.status_code, response.json['message'])) + self.module.fail_json(msg=f"Error getting {resource_key} [{response.status_code}: {response.json['message']}]") return response.json[resource_key] @@ -311,7 +307,7 @@ class Scaleway(object): state = self.fetch_state(resource) if state in stable_states: self.module.debug("It seems that the resource is not in transition anymore.") - self.module.debug("load-balancer in state: %s" % self.fetch_state(resource)) + self.module.debug(f"load-balancer in state: {self.fetch_state(resource)}") break time.sleep(wait_sleep_time)