Skip to content

Commit

Permalink
ansible_galaxy_install: minor refactor (#8413)
Browse files Browse the repository at this point in the history
* minor refactor

* add changelog frag

* remove commented code

* set use_old_vardict to false
  • Loading branch information
russoz authored May 26, 2024
1 parent ec88620 commit e7ee90a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8413-galaxy-refactor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- ansible_galaxy_install - minor refactor in the module (https://github.com/ansible-collections/community.general/pull/8413).
38 changes: 17 additions & 21 deletions plugins/modules/ansible_galaxy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@

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


Expand All @@ -180,7 +180,9 @@ class AnsibleGalaxyInstall(ModuleHelper):
_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_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

output_params = ('type', 'name', 'dest', 'requirements_file', 'force', 'no_deps')
Expand All @@ -198,17 +200,18 @@ class AnsibleGalaxyInstall(ModuleHelper):
required_if=[('type', 'both', ['requirements_file'])],
supports_check_mode=False,
)
use_old_vardict = False

command = 'ansible-galaxy'
command_args_formats = dict(
type=fmt.as_func(lambda v: [] if v == 'both' else [v]),
galaxy_cmd=fmt.as_list(),
requirements_file=fmt.as_opt_val('-r'),
dest=fmt.as_opt_val('-p'),
force=fmt.as_bool("--force"),
no_deps=fmt.as_bool("--no-deps"),
version=fmt.as_bool("--version"),
name=fmt.as_list(),
type=cmd_runner_fmt.as_func(lambda v: [] if v == 'both' else [v]),
galaxy_cmd=cmd_runner_fmt.as_list(),
requirements_file=cmd_runner_fmt.as_opt_val('-r'),
dest=cmd_runner_fmt.as_opt_val('-p'),
force=cmd_runner_fmt.as_bool("--force"),
no_deps=cmd_runner_fmt.as_bool("--no-deps"),
version=cmd_runner_fmt.as_fixed("--version"),
name=cmd_runner_fmt.as_list(),
)

def _make_runner(self, lang):
Expand All @@ -232,25 +235,18 @@ def process(rc, out, err):
try:
runner = self._make_runner("C.UTF-8")
with runner("version", check_rc=False, output_process=process) as ctx:
return runner, ctx.run(version=True)
except UnsupportedLocale as e:
return runner, ctx.run()
except UnsupportedLocale:
runner = self._make_runner("en_US.UTF-8")
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):
# 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()
if self.ansible_version < (2, 11):
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_roles", {}, change=True)
if self.vars.type != "collection":
Expand Down

0 comments on commit e7ee90a

Please sign in to comment.