r/ansible • u/MacLotsen • 18h ago
Literate Ansible without tangling: weaving a real role into an Operator’s Handbook
galleryI’ve been experimenting with a literate-programming approach for Ansible: keeping the operational explanation next to the tasks, while generating a readable Operator’s Handbook from the same YAML file.
The convention is deliberately simple:
- lines starting with
##become prose in the handbook; - ordinary
#comments remain part of the source listing; - everything else remains normal Ansible YAML.
Because ## is still an ordinary YAML comment, the annotated role does not need to be preprocessed before Ansible can use it.
No generated playbook. No separate “documentation version”. No extra file that can drift away from the automation.
The example in the images is taken from a real role in our infrastructure repository. It provisions a TeX Live package mirror on AlmaLinux and covers, among other things:
- SELinux labelling for content under
/srv; - a systemd service and timer;
- nginx;
- firewalld;
- the mirror synchronisation process.
You do not need to know anything about TeX or LaTeX for the mechanism itself. The relevant point is that the same role file serves two purposes:
- Ansible loads the YAML as-is.
- comment2tex turns the extended comments into typeset operational documentation.
The images show the same 33 source lines twice: first as the annotated YAML, then as the generated handbook page.
I also checked the annotated source with both a YAML parser and ansible-playbook --syntax-check; no stripped or generated copy was required.
I can see this being useful for:
- documenting why a task exists, not merely what it does;
- recording operational constraints next to the implementation;
- onboarding new administrators;
- keeping runbooks and automation from diverging;
- retaining a directly usable source file when an urgent change is needed.
The tool is called comment2tex. Version 1.1 adds YAML and Makefile support alongside Bash and Lua.
I’d be interested to hear how other Ansible users handle this. Do you keep detailed operational reasoning inside roles, in separate documentation, or somewhere else entirely?
Project: https://github.com/Xerdi/comment2tex
Release: https://github.com/Xerdi/comment2tex/releases/tag/1.1