From 04367d8b9cad8f511145cd95001031cc7d71b4b8 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 8 Apr 2026 15:46:30 +0200 Subject: [PATCH] [PR #11742/bdd31745 backport][stable-12] nmcli: use get_best_parsable_locale() to support UTF-8 connection names (#11757) nmcli: use get_best_parsable_locale() to support UTF-8 connection names (#11742) * nmcli: start locale fix - normalize run_command environ to LANGUAGE=C, LC_ALL=C Work in progress - issue #10384 (UTF-8 conn_name support) requires deeper investigation beyond simple locale variable normalization. * nmcli: use get_best_parsable_locale() to support UTF-8 connection names Fixes issue where UTF-8 connection names (e.g. Chinese characters) were corrupted to '????' when LC_ALL=C forced ASCII encoding, causing connection_exists() to always return False for non-ASCII names. * add changelog fragment for PR #11742 --------- (cherry picked from commit bdd3174563202e99e75fd6c831ec5992f24f6c82) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> Co-authored-by: Claude Sonnet 4.6 --- changelogs/fragments/11742-run-command-locale-nmcli.yml | 6 ++++++ plugins/modules/nmcli.py | 4 +++- tests/unit/plugins/modules/test_nmcli.py | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/11742-run-command-locale-nmcli.yml diff --git a/changelogs/fragments/11742-run-command-locale-nmcli.yml b/changelogs/fragments/11742-run-command-locale-nmcli.yml new file mode 100644 index 0000000000..57b36cbd37 --- /dev/null +++ b/changelogs/fragments/11742-run-command-locale-nmcli.yml @@ -0,0 +1,6 @@ +bugfixes: + - nmcli - use ``get_best_parsable_locale()`` to set locale environment for ``run_command()`` calls, + fixing UTF-8 connection names being corrupted to ``????`` under ``LC_ALL=C`` + (https://github.com/ansible-collections/community.general/issues/10384, + https://github.com/ansible-collections/community.general/issues/11737, + https://github.com/ansible-collections/community.general/pull/11742). diff --git a/plugins/modules/nmcli.py b/plugins/modules/nmcli.py index de98a9013e..51b57aa658 100644 --- a/plugins/modules/nmcli.py +++ b/plugins/modules/nmcli.py @@ -1705,6 +1705,7 @@ RETURN = r"""# import re from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.common.locale import get_best_parsable_locale from ansible.module_utils.common.text.converters import to_text @@ -2915,7 +2916,8 @@ def create_module() -> AnsibleModule: ], supports_check_mode=True, ) - module.run_command_environ_update = dict(LANG="C", LC_ALL="C", LC_MESSAGES="C", LC_CTYPE="C") + locale = get_best_parsable_locale(module) + module.run_command_environ_update = dict(LANGUAGE=locale, LC_ALL=locale) return module diff --git a/tests/unit/plugins/modules/test_nmcli.py b/tests/unit/plugins/modules/test_nmcli.py index f32585d6c0..e31ddb6a2f 100644 --- a/tests/unit/plugins/modules/test_nmcli.py +++ b/tests/unit/plugins/modules/test_nmcli.py @@ -1652,6 +1652,9 @@ def mocker_set( """ Common mocker object """ + mocker.patch( + "ansible_collections.community.general.plugins.modules.nmcli.get_best_parsable_locale", return_value="C" + ) get_bin_path = mocker.patch("ansible.module_utils.basic.AnsibleModule.get_bin_path") get_bin_path.return_value = "/usr/bin/nmcli" connection = mocker.patch.object(nmcli.Nmcli, "connection_exists")