Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR #5680/488e828f backport][stable-6] ansible_galaxy_install: use locale C tentatively, else en_US #5722

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bugfixes:
- ansible_galaxy_install - try ``C.UTF-8`` and then fall back to ``en_US.UTF-8`` before failing (https://github.com/ansible-collections/community.general/pull/5680).
- ansible_galaxy_install - set default to raise exception if command's return code is different from zero (https://github.com/ansible-collections/community.general/pull/5680).
35 changes: 26 additions & 9 deletions plugins/modules/ansible_galaxy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
- >
B(Ansible 2.9/2.10): The C(ansible-galaxy) command changed significantly between Ansible 2.9 and
ansible-base 2.10 (later ansible-core 2.11). See comments in the parameters.
- >
The module will try and run using the C(C.UTF-8) locale.
If that fails, it will try C(en_US.UTF-8).
If that one also fails, the module will fail.
requirements:
- Ansible 2.9, ansible-base 2.10, or ansible-core 2.11 or newer
options:
Expand Down Expand Up @@ -185,7 +189,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.module_helper import ModuleHelper
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper, ModuleHelperException


class AnsibleGalaxyInstall(ModuleHelper):
Expand Down Expand Up @@ -226,11 +230,17 @@ class AnsibleGalaxyInstall(ModuleHelper):
version=fmt.as_bool("--version"),
name=fmt.as_list(),
)
force_lang = "en_US.UTF-8"
check_rc = True

def _make_runner(self, lang):
return CmdRunner(self.module, command=self.command, arg_formats=self.command_args_formats, force_lang=lang, check_rc=True)

def _get_ansible_galaxy_version(self):
class UnsupportedLocale(ModuleHelperException):
pass

def process(rc, out, err):
if (rc != 0 and "unsupported locale setting" in err) or (rc == 0 and "cannot change locale" in err):
raise UnsupportedLocale(msg=err)
line = out.splitlines()[0]
match = self._RE_GALAXY_VERSION.match(line)
if not match:
Expand All @@ -239,12 +249,18 @@ def process(rc, out, err):
version = tuple(int(x) for x in version.split('.')[:3])
return version

with self.runner("version", check_rc=True, output_process=process) as ctx:
return ctx.run(version=True)
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:
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)

def __init_module__(self):
self.runner = CmdRunner(self.module, command=self.command, arg_formats=self.command_args_formats, force_lang=self.force_lang)
self.ansible_version = self._get_ansible_galaxy_version()
# 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) and not self.vars.ack_min_ansiblecore211:
self.module.deprecate(
"Support for Ansible 2.9 and ansible-base 2.10 is being deprecated. "
Expand Down Expand Up @@ -339,11 +355,12 @@ def process(rc, out, err):
self._setup210plus()
with self.runner("type galaxy_cmd force no_deps dest requirements_file name", output_process=process) as ctx:
ctx.run(galaxy_cmd="install")
if self.verbosity > 2:
self.vars.set("run_info", ctx.run_info)


def main():
galaxy = AnsibleGalaxyInstall()
galaxy.run()
AnsibleGalaxyInstall.execute()


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
name: netbox.netbox
register: install_c0

- name: Assert collection was installed
- name: Assert collection netbox.netbox was installed
assert:
that:
- install_c0 is changed
Expand All @@ -34,7 +34,7 @@
name: ansistrano.deploy
register: install_r0

- name: Assert collection was installed
- name: Assert collection ansistrano.deploy was installed
assert:
that:
- install_r0 is changed
Expand All @@ -52,7 +52,7 @@
- install_r1 is not changed

###################################################
- name:
- name: Set requirements file path
set_fact:
reqs_file: '{{ remote_tmp_dir }}/reqs.yaml'

Expand Down