diff --git a/changelogs/fragments/11396-in-def-imports.yml b/changelogs/fragments/11396-in-def-imports.yml new file mode 100644 index 0000000000..cb1f858a11 --- /dev/null +++ b/changelogs/fragments/11396-in-def-imports.yml @@ -0,0 +1,7 @@ +minor_changes: + - jenkins_script - move ``import`` statemetns to the top of the file (https://github.com/ansible-collections/community.general/pull/11396). + - linode - move ``import`` statemetns to the top of the file (https://github.com/ansible-collections/community.general/pull/11396). + - one_image - move ``import`` statemetns to the top of the file (https://github.com/ansible-collections/community.general/pull/11396). + - one_image_info - move ``import`` statemetns to the top of the file (https://github.com/ansible-collections/community.general/pull/11396). + - one_service - move ``import`` statemetns to the top of the file (https://github.com/ansible-collections/community.general/pull/11396). + - one_vm - move ``import`` statemetns to the top of the file (https://github.com/ansible-collections/community.general/pull/11396). diff --git a/plugins/modules/jenkins_script.py b/plugins/modules/jenkins_script.py index c70edb1ac7..0f615d47b5 100644 --- a/plugins/modules/jenkins_script.py +++ b/plugins/modules/jenkins_script.py @@ -103,6 +103,7 @@ output: import json from http import cookiejar +from string import Template from urllib.parse import urlencode from ansible.module_utils.basic import AnsibleModule @@ -153,8 +154,6 @@ def main(): module.params["force_basic_auth"] = True if module.params["args"] is not None: - from string import Template - try: script_contents = Template(module.params["script"]).substitute(module.params["args"]) except KeyError as err: diff --git a/plugins/modules/linode.py b/plugins/modules/linode.py index 8bdf01fee0..a62d57f9dc 100644 --- a/plugins/modules/linode.py +++ b/plugins/modules/linode.py @@ -272,6 +272,8 @@ EXAMPLES = r""" delegate_to: localhost """ +import random +import string import time import traceback @@ -295,8 +297,6 @@ def randompass(): # It must contain at least two of these four character classes: # lower case letters - upper case letters - numbers - punctuation # we play it safe :) - import random - import string # as of python 2.4, this reseeds the PRNG from urandom random.seed() diff --git a/plugins/modules/one_image.py b/plugins/modules/one_image.py index 4be5c9b08b..8a831f6701 100644 --- a/plugins/modules/one_image.py +++ b/plugins/modules/one_image.py @@ -368,6 +368,8 @@ snapshots: """ +import time + from ansible_collections.community.general.plugins.module_utils.opennebula import OpenNebulaModule @@ -489,8 +491,6 @@ class ImageModule(OpenNebulaModule): return result def wait_for_ready(self, image_id, wait_timeout=60): - import time - start_time = time.time() while (time.time() - start_time) < wait_timeout: @@ -507,8 +507,6 @@ class ImageModule(OpenNebulaModule): self.module.fail_json(msg="Wait timeout has expired!") def wait_for_delete(self, image_id, wait_timeout=60): - import time - start_time = time.time() while (time.time() - start_time) < wait_timeout: diff --git a/plugins/modules/one_image_info.py b/plugins/modules/one_image_info.py index 70dca77a63..ee76e9eb10 100644 --- a/plugins/modules/one_image_info.py +++ b/plugins/modules/one_image_info.py @@ -269,6 +269,7 @@ images: name: SampleName """ +import re from ansible_collections.community.general.plugins.module_utils.opennebula import OpenNebulaModule @@ -345,8 +346,6 @@ class ImageInfoModule(OpenNebulaModule): pool = self.get_all_images() if name_pattern.startswith("~"): - import re - if name_pattern[1] == "*": pattern = re.compile(name_pattern[2:], re.IGNORECASE) else: diff --git a/plugins/modules/one_service.py b/plugins/modules/one_service.py index dcf5b03b76..bd2db1a035 100644 --- a/plugins/modules/one_service.py +++ b/plugins/modules/one_service.py @@ -231,6 +231,9 @@ roles: """ import os +import time +from collections import namedtuple + from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.urls import open_url @@ -396,8 +399,6 @@ def create_service(module, auth, template_id, service_name, custom_attrs, unique def wait_for_service_to_become_ready(module, auth, service_id, wait_timeout): - import time - start_time = time.time() while (time.time() - start_time) < wait_timeout: @@ -729,8 +730,6 @@ def get_connection_info(module): module.fail_json( msg="One or more connection parameters (api_url, api_username, api_password) were not specified" ) - from collections import namedtuple - auth_params = namedtuple("auth", ("url", "user", "password")) return auth_params(url=url, user=username, password=password) diff --git a/plugins/modules/one_vm.py b/plugins/modules/one_vm.py index 9e6775a43b..7ca4a639c5 100644 --- a/plugins/modules/one_vm.py +++ b/plugins/modules/one_vm.py @@ -682,8 +682,12 @@ try: except ImportError: HAS_PYONE = False - +import copy import os +import re +import time +from collections import namedtuple + from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.common.dict_transformations import dict_merge @@ -875,7 +879,6 @@ def get_vm_info(client, vm): "security_groups": vm.TEMPLATE["NIC"].get("SECURITY_GROUPS", ""), } ) - import time current_time = time.localtime() vm_start_time = time.localtime(vm.STIME) @@ -1146,8 +1149,6 @@ def get_all_vms_by_attributes(client, attributes_dict, labels_list): vm_list.append(vm) pool = vm_list - import copy - vm_list = copy.copy(pool) for vm in pool: @@ -1367,8 +1368,6 @@ LCM_STATES = [ def wait_for_state(module, client, vm, wait_timeout, state_predicate): - import time - start_time = time.time() while (time.time() - start_time) < wait_timeout: @@ -1551,8 +1550,6 @@ def release_vm(module, client, vm): def check_name_attribute(module, attributes): if attributes.get("NAME"): - import re - if re.match(r"^[^#]+#*$", attributes.get("NAME")) is None: module.fail_json( msg=f"Illegal 'NAME' attribute: '{attributes.get('NAME')}" @@ -1638,7 +1635,6 @@ def get_connection_info(module): module.fail_json(msg=f"Error occurs when read ONE_AUTH file at '{authfile}'") if not url: module.fail_json(msg="Opennebula API url (api_url) is not specified") - from collections import namedtuple auth_params = namedtuple("auth", ("url", "username", "password")) @@ -1752,8 +1748,6 @@ def main(): if count_attributes: count_attributes = {key.upper(): value for key, value in count_attributes.items()} if not attributes: - import copy - module.warn( "When you pass `count_attributes` without `attributes` option when deploying, `attributes` option will have same values implicitly." ) @@ -1820,8 +1814,6 @@ def main(): module.fail_json(msg="`count` has to be greater than 0") if permissions is not None: - import re - if re.match("^[0-7]{3}$", permissions) is None: module.fail_json(msg="Option `mode` has to have exactly 3 digits and be in the octet format e.g. 600")