Basic handling of locales.
This commit is contained in:
parent
643690ffec
commit
6d1c2a92de
1 changed files with 101 additions and 0 deletions
101
library/system/locale
Normal file
101
library/system/locale
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
import os
|
||||||
|
import os.path
|
||||||
|
from subprocess import Popen, PIPE, call
|
||||||
|
|
||||||
|
DOCUMENTATION = '''
|
||||||
|
---
|
||||||
|
module: locale
|
||||||
|
short_description: Creates of removes locales.
|
||||||
|
description:
|
||||||
|
- Manages locales by editing /etc/locale.gen and invoking locale-gen.
|
||||||
|
version_added: "1.5"
|
||||||
|
options:
|
||||||
|
name:
|
||||||
|
description:
|
||||||
|
- Name and encoding of the locale, such as "en_GB.UTF-8".
|
||||||
|
required: true
|
||||||
|
default: null
|
||||||
|
aliases: []
|
||||||
|
state:
|
||||||
|
description:
|
||||||
|
- Whether the locale shall be present.
|
||||||
|
required: false
|
||||||
|
choices: ["present", "absent"]
|
||||||
|
default: "present"
|
||||||
|
'''
|
||||||
|
|
||||||
|
EXAMPLES = '''
|
||||||
|
# Ensure a locale exists.
|
||||||
|
- locale: name=de_CH.UTF-8 state=present
|
||||||
|
'''
|
||||||
|
|
||||||
|
# ===========================================
|
||||||
|
# location module specific support methods.
|
||||||
|
#
|
||||||
|
|
||||||
|
def is_present(name):
|
||||||
|
"""Checks if the given locale is currently installed."""
|
||||||
|
output = Popen(["locale", "-a"], stdout=PIPE).communicate()[0]
|
||||||
|
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."""
|
||||||
|
return name.replace(".utf8", ".UTF-8")
|
||||||
|
|
||||||
|
def replace_line(existing_line, new_line):
|
||||||
|
"""Replaces lines in /etc/locale.gen"""
|
||||||
|
with open("/etc/locale.gen", "r") as f:
|
||||||
|
lines = [line.replace(existing_line, new_line) for line in f]
|
||||||
|
with open("/etc/locale.gen", "w") as f:
|
||||||
|
f.write("".join(lines))
|
||||||
|
|
||||||
|
# ==============================================================
|
||||||
|
# main
|
||||||
|
|
||||||
|
def main():
|
||||||
|
|
||||||
|
module = AnsibleModule(
|
||||||
|
argument_spec = dict(
|
||||||
|
name = dict(required=True),
|
||||||
|
state = dict(choices=['present','absent'], required=True),
|
||||||
|
),
|
||||||
|
supports_check_mode=True
|
||||||
|
)
|
||||||
|
|
||||||
|
name = module.params['name']
|
||||||
|
if not "." in name:
|
||||||
|
module.fail_json(msg="Locale does not match pattern. Did you specify the encoding?")
|
||||||
|
state = module.params['state']
|
||||||
|
|
||||||
|
if not os.path.exists("/etc/locale.gen"):
|
||||||
|
module.fail_json(msg="/etc/locale.gen missing. Is the package “locales” installed?")
|
||||||
|
|
||||||
|
prev_state = "present" if is_present(name) else "absent"
|
||||||
|
changed = (prev_state!=state)
|
||||||
|
|
||||||
|
if module.check_mode:
|
||||||
|
module.exit_json(changed=changed)
|
||||||
|
else:
|
||||||
|
encoding = name.split(".")[1]
|
||||||
|
if changed:
|
||||||
|
if state=="present":
|
||||||
|
# Create locale.
|
||||||
|
replace_line("# "+name+" "+encoding, name+" "+encoding)
|
||||||
|
else:
|
||||||
|
# Delete locale.
|
||||||
|
replace_line(name+" "+encoding, "# "+name+" "+encoding)
|
||||||
|
|
||||||
|
localeGenExitValue = call("locale-gen")
|
||||||
|
if localeGenExitValue!=0:
|
||||||
|
module.fail_json(msg="locale.gen failed to execute, it returned "+localeGenExitValue)
|
||||||
|
|
||||||
|
module.exit_json(name=name, changed=changed, msg="OK")
|
||||||
|
|
||||||
|
# import module snippets
|
||||||
|
from ansible.module_utils.basic import *
|
||||||
|
|
||||||
|
main()
|
Loading…
Reference in a new issue