{"id":8824,"library":"ansible-pygments","title":"Ansible Pygments","description":"Ansible Pygments provides a Pygments lexer specifically designed to highlight Ansible output. It can be integrated into any application that uses Pygments for syntax highlighting, such as Sphinx documentation. The lexer is globally registered under the name `ansible-output` and also offers a Pygments style. The project is actively maintained with regular, typically minor, releases.","status":"active","version":"0.1.2","language":"en","source_language":"en","source_url":"https://github.com/ansible-community/ansible-pygments","tags":["ansible","pygments","syntax-highlighting","lexer","sphinx","documentation"],"install":[{"cmd":"pip install ansible-pygments","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Provides the core syntax highlighting framework.","package":"Pygments","optional":false}],"imports":[{"note":"Direct import of the lexer class for advanced usage or testing.","symbol":"AnsibleOutputLexer","correct":"from ansible_pygments.lexer import AnsibleOutputLexer"},{"note":"The recommended way to access the lexer, especially when integrating with tools that expect named Pygments lexers (e.g., Sphinx).","symbol":"get_lexer_by_name","correct":"from pygments.lexers import get_lexer_by_name\nlexer = get_lexer_by_name('ansible-output')"}],"quickstart":{"code":"from pygments import highlight\nfrom pygments.lexers import get_lexer_by_name\nfrom pygments.formatters import HtmlFormatter\n\nansible_output = \"\"\"\n[WARNING]: Unable to find '/nosuchfile' in expected paths (use -vvvvv to see paths)\nok: [localhost] => {\n    \"msg\": \"Hello from Ansible!\"\n}\n\"\"\"\n\ntry:\n    lexer = get_lexer_by_name('ansible-output')\n    formatter = HtmlFormatter()\n    highlighted_html = highlight(ansible_output, lexer, formatter)\n    print(highlighted_html)\nexcept Exception as e:\n    print(f\"Error highlighting: {e}\")\n    print(\"Make sure 'ansible-pygments' and 'Pygments' are installed.\")","lang":"python","description":"This quickstart demonstrates how to use the `ansible-output` lexer with Pygments to highlight a string containing Ansible output and render it as HTML."},"warnings":[{"fix":"When highlighting untrusted user input, ensure Pygments processes are terminated after a reasonable timeout and limit concurrent processes to prevent resource exhaustion.","message":"Pygments, and by extension `ansible-pygments`, can be susceptible to Denial-of-Service (DoS) attacks if processing arbitrary, untrusted user inputs. Crafted inputs might lead to long processing times or excessive memory usage.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Pin `ansible-pygments` to exact versions in production environments (e.g., `ansible-pygments==0.1.2`) and review release notes carefully before upgrading.","message":"As a 0.x.x version library, while actively maintained, `ansible-pygments` may introduce minor API changes or breaking modifications between patch or minor versions without adhering to strict semantic versioning (e.g., a 0.1.x to 0.2.x bump might include breaking changes).","severity":"gotcha","affected_versions":"All 0.x.x versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure `ansible-pygments` is correctly installed: `pip install ansible-pygments`. If using a virtual environment, activate it before installing.","cause":"The `ansible-pygments` package is not installed in the Python environment where Pygments is attempting to load the lexer, or the installation is corrupted.","error":"Pygments lexer 'ansible-output' not found"},{"fix":"Always use the full registered name `ansible-output` when referencing the lexer, e.g., `get_lexer_by_name('ansible-output')` or `.. code-block:: ansible-output` in Sphinx.","cause":"The user attempted to reference the Ansible output lexer using the incorrect alias, `ansible`, instead of the globally registered name `ansible-output`.","error":"Exception: no lexer for alias 'ansible' found"}]}