2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-08 14:24:58 +02:00
|
|
|
# Copyright (c) Ansible project
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: locale_gen
|
|
|
|
short_description: Creates or removes locales
|
|
|
|
description:
|
2023-02-20 17:28:47 +01:00
|
|
|
- Manages locales by editing /etc/locale.gen and invoking locale-gen.
|
2020-03-09 10:11:07 +01:00
|
|
|
author:
|
2023-02-20 17:28:47 +01:00
|
|
|
- Augustus Kling (@AugustusKling)
|
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
name:
|
2020-11-04 09:02:50 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-02-20 17:28:47 +01:00
|
|
|
- Name and encoding of the locale, such as "en_GB.UTF-8".
|
2020-03-09 10:11:07 +01:00
|
|
|
required: true
|
|
|
|
state:
|
2023-02-20 17:28:47 +01:00
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- Whether the locale shall be present.
|
|
|
|
choices: [ absent, present ]
|
|
|
|
default: present
|
2023-06-30 13:13:16 +02:00
|
|
|
notes:
|
|
|
|
- This module does not support RHEL-based systems.
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: Ensure a locale exists
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.locale_gen:
|
2020-03-09 10:11:07 +01:00
|
|
|
name: de_CH.UTF-8
|
|
|
|
state: present
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
from subprocess import Popen, PIPE, call
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
LOCALE_NORMALIZATION = {
|
|
|
|
".utf8": ".UTF-8",
|
|
|
|
".eucjp": ".EUC-JP",
|
|
|
|
".iso885915": ".ISO-8859-15",
|
|
|
|
".cp1251": ".CP1251",
|
|
|
|
".koi8r": ".KOI8-R",
|
|
|
|
".armscii8": ".ARMSCII-8",
|
|
|
|
".euckr": ".EUC-KR",
|
|
|
|
".gbk": ".GBK",
|
|
|
|
".gb18030": ".GB18030",
|
|
|
|
".euctw": ".EUC-TW",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# ===========================================
|
|
|
|
# location module specific support methods.
|
|
|
|
#
|
|
|
|
|
|
|
|
def is_available(name, ubuntuMode):
|
|
|
|
"""Check if the given locale is available on the system. This is done by
|
|
|
|
checking either :
|
|
|
|
* if the locale is present in /etc/locales.gen
|
|
|
|
* or if the locale is present in /usr/share/i18n/SUPPORTED"""
|
2023-06-30 13:13:16 +02:00
|
|
|
__regexp = r'^#?\s*(?P<locale>\S+[\._\S]*) (?P<charset>\S+)\s*$'
|
2020-03-09 10:11:07 +01:00
|
|
|
if ubuntuMode:
|
|
|
|
__locales_available = '/usr/share/i18n/SUPPORTED'
|
|
|
|
else:
|
|
|
|
__locales_available = '/etc/locale.gen'
|
|
|
|
|
|
|
|
re_compiled = re.compile(__regexp)
|
|
|
|
fd = open(__locales_available, 'r')
|
|
|
|
for line in fd:
|
|
|
|
result = re_compiled.match(line)
|
|
|
|
if result and result.group('locale') == name:
|
|
|
|
return True
|
|
|
|
fd.close()
|
2023-06-30 13:13:16 +02:00
|
|
|
# locale may be installed but not listed in the file, for example C.UTF-8 in some systems
|
|
|
|
return is_present(name)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def is_present(name):
|
|
|
|
"""Checks if the given locale is currently installed."""
|
|
|
|
output = Popen(["locale", "-a"], stdout=PIPE).communicate()[0]
|
|
|
|
output = to_native(output)
|
|
|
|
return any(fix_case(name) == fix_case(line) for line in output.splitlines())
|
|
|
|
|
|
|
|
|
|
|
|
def fix_case(name):
|
|
|
|
"""locale -a might return the encoding in either lower or upper case.
|
|
|
|
Passing through this function makes them uniform for comparisons."""
|
|
|
|
for s, r in LOCALE_NORMALIZATION.items():
|
|
|
|
name = name.replace(s, r)
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
|
|
|
def set_locale(name, enabled=True):
|
|
|
|
""" Sets the state of the locale. Defaults to enabled. """
|
|
|
|
search_string = r'#{0,1}\s*%s (?P<charset>.+)' % name
|
|
|
|
if enabled:
|
|
|
|
new_string = r'%s \g<charset>' % (name)
|
|
|
|
else:
|
|
|
|
new_string = r'# %s \g<charset>' % (name)
|
|
|
|
try:
|
|
|
|
f = open("/etc/locale.gen", "r")
|
|
|
|
lines = [re.sub(search_string, new_string, line) for line in f]
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
try:
|
|
|
|
f = open("/etc/locale.gen", "w")
|
|
|
|
f.write("".join(lines))
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
|
|
def apply_change(targetState, name):
|
|
|
|
"""Create or remove locale.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
targetState -- Desired state, either present or absent.
|
|
|
|
name -- Name including encoding such as de_CH.UTF-8.
|
|
|
|
"""
|
|
|
|
if targetState == "present":
|
|
|
|
# Create locale.
|
|
|
|
set_locale(name, enabled=True)
|
|
|
|
else:
|
|
|
|
# Delete locale.
|
|
|
|
set_locale(name, enabled=False)
|
|
|
|
|
|
|
|
localeGenExitValue = call("locale-gen")
|
|
|
|
if localeGenExitValue != 0:
|
|
|
|
raise EnvironmentError(localeGenExitValue, "locale.gen failed to execute, it returned " + str(localeGenExitValue))
|
|
|
|
|
|
|
|
|
|
|
|
def apply_change_ubuntu(targetState, name):
|
|
|
|
"""Create or remove locale.
|
|
|
|
|
|
|
|
Keyword arguments:
|
|
|
|
targetState -- Desired state, either present or absent.
|
|
|
|
name -- Name including encoding such as de_CH.UTF-8.
|
|
|
|
"""
|
|
|
|
if targetState == "present":
|
|
|
|
# Create locale.
|
|
|
|
# Ubuntu's patched locale-gen automatically adds the new locale to /var/lib/locales/supported.d/local
|
|
|
|
localeGenExitValue = call(["locale-gen", name])
|
|
|
|
else:
|
|
|
|
# Delete locale involves discarding the locale from /var/lib/locales/supported.d/local and regenerating all locales.
|
|
|
|
try:
|
|
|
|
f = open("/var/lib/locales/supported.d/local", "r")
|
|
|
|
content = f.readlines()
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
try:
|
|
|
|
f = open("/var/lib/locales/supported.d/local", "w")
|
|
|
|
for line in content:
|
|
|
|
locale, charset = line.split(' ')
|
|
|
|
if locale != name:
|
|
|
|
f.write(line)
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
# Purge locales and regenerate.
|
|
|
|
# Please provide a patch if you know how to avoid regenerating the locales to keep!
|
|
|
|
localeGenExitValue = call(["locale-gen", "--purge"])
|
|
|
|
|
|
|
|
if localeGenExitValue != 0:
|
|
|
|
raise EnvironmentError(localeGenExitValue, "locale.gen failed to execute, it returned " + str(localeGenExitValue))
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
name=dict(type='str', required=True),
|
|
|
|
state=dict(type='str', default='present', choices=['absent', 'present']),
|
|
|
|
),
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
name = module.params['name']
|
|
|
|
state = module.params['state']
|
|
|
|
|
2022-09-25 16:31:32 +02:00
|
|
|
if not os.path.exists("/var/lib/locales/supported.d/"):
|
|
|
|
if os.path.exists("/etc/locale.gen"):
|
|
|
|
# We found the common way to manage locales.
|
|
|
|
ubuntuMode = False
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2023-06-30 13:13:16 +02:00
|
|
|
module.fail_json(msg="/etc/locale.gen and /var/lib/locales/supported.d/local are missing. Is the package 'locales' installed?")
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2022-09-25 16:31:32 +02:00
|
|
|
# Ubuntu created its own system to manage locales.
|
|
|
|
ubuntuMode = True
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if not is_available(name, ubuntuMode):
|
|
|
|
module.fail_json(msg="The locale you've entered is not available "
|
|
|
|
"on your system.")
|
|
|
|
|
|
|
|
if is_present(name):
|
|
|
|
prev_state = "present"
|
|
|
|
else:
|
|
|
|
prev_state = "absent"
|
|
|
|
changed = (prev_state != state)
|
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=changed)
|
|
|
|
else:
|
|
|
|
if changed:
|
|
|
|
try:
|
|
|
|
if ubuntuMode is False:
|
|
|
|
apply_change(state, name)
|
|
|
|
else:
|
|
|
|
apply_change_ubuntu(state, name)
|
|
|
|
except EnvironmentError as e:
|
|
|
|
module.fail_json(msg=to_native(e), exitValue=e.errno)
|
|
|
|
|
|
|
|
module.exit_json(name=name, changed=changed, msg="OK")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|