Skip to content

Instantly share code, notes, and snippets.

@jpic
Created May 12, 2026 07:57
Show Gist options
  • Select an option

  • Save jpic/d92fc7fa8adf562bdc1269682f94d774 to your computer and use it in GitHub Desktop.

Select an option

Save jpic/d92fc7fa8adf562bdc1269682f94d774 to your computer and use it in GitHub Desktop.
action_example.py
from ansible.plugins.action import ActionBase
import re
import sys
# ANSI Colors
RESET = "\033[0m"
COMMENT = "\033[90m" # gray
KEY = "\033[36m" # cyan
STRING = "\033[32m" # green
NUMBER = "\033[33m" # yellow
BOOLEAN = "\033[35m" # magenta
PUNCT = "\033[37m" # light gray
def highlight_yaml(text: str) -> str:
lines = text.splitlines()
result = []
for line in lines:
# Comment
if '#' in line:
comment_pos = line.find('#')
code = line[:comment_pos]
comment = line[comment_pos:]
line = code + COMMENT + comment + RESET
# Key: value
line = re.sub(r'^(\s*)([\w-]+):', r'\1' + KEY + r'\2' + RESET + ':', line)
# Strings
line = re.sub(r'(["\'])(.*?)(["\'])', STRING + r'\1\2\3' + RESET, line)
# Numbers
line = re.sub(r'\b(\d+)\b', NUMBER + r'\1' + RESET, line)
# Booleans & null
line = re.sub(r'\b(true|false|yes|no|null|None)\b', BOOLEAN + r'\1' + RESET, line, flags=re.I)
result.append(line)
return '\n'.join(result)
class ActionModule(ActionBase):
def run(self, tmp=None, task_vars=None):
result = dict(changed=False)
sample = """
database:
host: localhost
port: 5432
enabled: true
timeout: 30.5
password: "secret123"
tags: [dev, staging]
# This is a comment
logging:
level: INFO
file: app.log
"""
self._display.display(highlight_yaml(sample))
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment