You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
1.1 KiB
Python
34 lines
1.1 KiB
Python
from functools import partial
|
|
import re
|
|
import subprocess
|
|
import sys
|
|
|
|
from ansible.errors import AnsibleFilterError
|
|
|
|
def systemd_escape(text, instance=False, mangle=False, path=False, suffix=None, template=None, unescape=False):
|
|
options_map = {
|
|
"instance": instance,
|
|
"mangle": mangle,
|
|
"path": path,
|
|
"unescape": unescape,
|
|
}
|
|
args_map = {
|
|
"suffix": suffix,
|
|
"template": template,
|
|
}
|
|
args = ["/usr/bin/env", "systemd-escape"] + [f"--{name}" for name, val in options_map.items() if val] + [f"--{name}={val}" for name, val in args_map.items() if val is not None] + [text]
|
|
result = subprocess.run(args, capture_output=True, text=True)
|
|
if result.returncode != 0:
|
|
raise AnsibleFilterError(re.sub('\u001b\\[.*?[@-~]', '', result.stderr.rstrip('\n')))
|
|
return result.stdout.rstrip('\n')
|
|
|
|
class FilterModule(object):
|
|
def filters(self):
|
|
return {
|
|
'systemd_escape': systemd_escape,
|
|
'systemd_escape_mount': partial(systemd_escape, path=True, suffix='mount')
|
|
}
|
|
|
|
if __name__ == '__main__':
|
|
print(systemd_escape(sys.argv[1]))
|