1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

ansible_galaxy_install: minor refactor (#8413)

* minor refactor

* add changelog frag

* remove commented code

* set use_old_vardict to false
This commit is contained in:
Alexei Znamensky 2024-05-26 23:57:24 +12:00 committed by GitHub
parent ec886203fc
commit e7ee90a937
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 21 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- ansible_galaxy_install - minor refactor in the module (https://github.com/ansible-collections/community.general/pull/8413).

View file

@ -171,7 +171,7 @@ RETURN = """
import re import re
from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt as fmt from ansible_collections.community.general.plugins.module_utils.cmd_runner import CmdRunner, cmd_runner_fmt
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper, ModuleHelperException from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper, ModuleHelperException
@ -180,7 +180,9 @@ class AnsibleGalaxyInstall(ModuleHelper):
_RE_LIST_PATH = re.compile(r'^# (?P<path>.*)$') _RE_LIST_PATH = re.compile(r'^# (?P<path>.*)$')
_RE_LIST_COLL = re.compile(r'^(?P<elem>\w+\.\w+)\s+(?P<version>[\d\.]+)\s*$') _RE_LIST_COLL = re.compile(r'^(?P<elem>\w+\.\w+)\s+(?P<version>[\d\.]+)\s*$')
_RE_LIST_ROLE = re.compile(r'^- (?P<elem>\w+\.\w+),\s+(?P<version>[\d\.]+)\s*$') _RE_LIST_ROLE = re.compile(r'^- (?P<elem>\w+\.\w+),\s+(?P<version>[\d\.]+)\s*$')
_RE_INSTALL_OUTPUT = None # Set after determining ansible version, see __init_module__() _RE_INSTALL_OUTPUT = re.compile(
r'^(?:(?P<collection>\w+\.\w+):(?P<cversion>[\d\.]+)|- (?P<role>\w+\.\w+) \((?P<rversion>[\d\.]+)\)) was installed successfully$'
)
ansible_version = None ansible_version = None
output_params = ('type', 'name', 'dest', 'requirements_file', 'force', 'no_deps') output_params = ('type', 'name', 'dest', 'requirements_file', 'force', 'no_deps')
@ -198,17 +200,18 @@ class AnsibleGalaxyInstall(ModuleHelper):
required_if=[('type', 'both', ['requirements_file'])], required_if=[('type', 'both', ['requirements_file'])],
supports_check_mode=False, supports_check_mode=False,
) )
use_old_vardict = False
command = 'ansible-galaxy' command = 'ansible-galaxy'
command_args_formats = dict( command_args_formats = dict(
type=fmt.as_func(lambda v: [] if v == 'both' else [v]), type=cmd_runner_fmt.as_func(lambda v: [] if v == 'both' else [v]),
galaxy_cmd=fmt.as_list(), galaxy_cmd=cmd_runner_fmt.as_list(),
requirements_file=fmt.as_opt_val('-r'), requirements_file=cmd_runner_fmt.as_opt_val('-r'),
dest=fmt.as_opt_val('-p'), dest=cmd_runner_fmt.as_opt_val('-p'),
force=fmt.as_bool("--force"), force=cmd_runner_fmt.as_bool("--force"),
no_deps=fmt.as_bool("--no-deps"), no_deps=cmd_runner_fmt.as_bool("--no-deps"),
version=fmt.as_bool("--version"), version=cmd_runner_fmt.as_fixed("--version"),
name=fmt.as_list(), name=cmd_runner_fmt.as_list(),
) )
def _make_runner(self, lang): def _make_runner(self, lang):
@ -232,25 +235,18 @@ class AnsibleGalaxyInstall(ModuleHelper):
try: try:
runner = self._make_runner("C.UTF-8") runner = self._make_runner("C.UTF-8")
with runner("version", check_rc=False, output_process=process) as ctx: with runner("version", check_rc=False, output_process=process) as ctx:
return runner, ctx.run(version=True) return runner, ctx.run()
except UnsupportedLocale as e: except UnsupportedLocale:
runner = self._make_runner("en_US.UTF-8") runner = self._make_runner("en_US.UTF-8")
with runner("version", check_rc=True, output_process=process) as ctx: with runner("version", check_rc=True, output_process=process) as ctx:
return runner, ctx.run(version=True) return runner, ctx.run()
def __init_module__(self): def __init_module__(self):
# self.runner = CmdRunner(self.module, command=self.command, arg_formats=self.command_args_formats, force_lang=self.force_lang)
self.runner, self.ansible_version = self._get_ansible_galaxy_version() self.runner, self.ansible_version = self._get_ansible_galaxy_version()
if self.ansible_version < (2, 11): if self.ansible_version < (2, 11):
self.module.fail_json( self.module.fail_json(
msg="Support for Ansible 2.9 and ansible-base 2.10 has ben removed." msg="Support for Ansible 2.9 and ansible-base 2.10 has been removed."
) )
# Collection install output changed:
# ansible-base 2.10: "coll.name (x.y.z)"
# ansible-core 2.11+: "coll.name:x.y.z"
self._RE_INSTALL_OUTPUT = re.compile(r'^(?:(?P<collection>\w+\.\w+)(?: \(|:)(?P<cversion>[\d\.]+)\)?'
r'|- (?P<role>\w+\.\w+) \((?P<rversion>[\d\.]+)\))'
r' was installed successfully$')
self.vars.set("new_collections", {}, change=True) self.vars.set("new_collections", {}, change=True)
self.vars.set("new_roles", {}, change=True) self.vars.set("new_roles", {}, change=True)
if self.vars.type != "collection": if self.vars.type != "collection":