Files
computer-playbook/filter_plugins/ruby.py
T
kevinveenbirkenbach 5ec5b64dae - Add ruby_dq filter to correctly escape values for Ruby double-quoted strings
- Apply dotenv_quote to GITLAB_OMNIBUS_CONFIG to prevent .env parsing errors
- Fix docker compose failures with passwords containing ', $, € or newlines

https://chatgpt.com/share/697a48af-ba40-800f-8de6-930adda782b0
2026-01-28 18:34:35 +01:00

55 lines
1.1 KiB
Python

#
# Ansible filter for safely embedding values into Ruby double-quoted strings.
#
# Example:
# {{ my_password | ruby_dq }}
#
# Output:
# "escaped_value"
#
# Safe for GitLab Omnibus (gitlab.rb), Chef, Ruby DSLs.
class FilterModule:
def filters(self):
return {
"ruby_dq": self.ruby_double_quoted_string,
}
@staticmethod
def ruby_double_quoted_string(value):
"""
Escape a value for use inside a Ruby double-quoted string
and wrap it in double quotes.
Ruby rules handled:
- " -> \"
- \ -> \\
- newlines -> \n
- carriage return -> \r
- tab -> \t
NOT escaped on purpose:
- $
- €
- !
- '
"""
if value is None:
return '""'
s = str(value)
# Escape backslash first
s = s.replace("\\", "\\\\")
# Escape double quotes
s = s.replace('"', '\\"')
# Escape control characters
s = s.replace("\n", "\\n")
s = s.replace("\r", "\\r")
s = s.replace("\t", "\\t")
return f'"{s}"'