Reformatting playbooks, roles, tasks, etc to "canonical" form

94 views
Skip to first unread message

Ch Ho

unread,
Sep 13, 2021, 5:52:43 PM9/13/21
to Ansible Project

Hi all;

Our company has 150+ files in an ansible repository, written by many authors over years.

I'd like to reformat them into a canonical format, so that we can write utilities to update them without having to handle all the variations.

I tried to use the python version of `yq` to do it:
-----
cat input.yml \
  | sed 's/^\( *mode: \)\([^" ]*\)/\1"\2"/; s/^ *\(#.*\)/- "\1"/; s/^---/- "#---"/' \
| yq -Y . \
| sed 's/^\( *- name:\)/\n\1/; s/^- "#---"/---/; s/^- "\(#.*\)"/\1/' \
> output.yml

diff input.yml output.yml
-----
but that feels like a hack (had to work around comments being stripped, file permissions changing from octal to decimal, etc).

Ideally, ansible-playbook would have a `--dump-playbook-file=output.yml` to do this, since Ansible is the ultimate judge of what a playbook means, but my RFE was redirected to the mailing list.   :-)

Any ideas?

Thanks!

Matt Martz

unread,
Sep 13, 2021, 5:59:59 PM9/13/21
to ansible...@googlegroups.com
The YAML spec states that comments should not impact serialization or representation, and are not tied to any particular node.  As such, YAML parsers that follow the spec, or try to closely follow the spec, discard comments.

The ansible project utilizes PyYAML which is the official Python YAML parser, and as such, discards comments.

ruamel.yaml however, does have functionality to try and keep comments, but the ansible project does not use this parser.

Effectively, you would have to be bound by a non-spec YAML parser, to enable the functionality you want to use.  A community project could implement this, however you will run into issues where something like yamllint, and ruamel.yaml will disagree on formatting, so you'll need to manually adjust them afterwards.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/7ba68c08-b728-480f-9d5f-090eaa2f1982n%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

Richard Megginson

unread,
Sep 13, 2021, 6:25:48 PM9/13/21
to ansible...@googlegroups.com
We did something similar for the system roles project - https://github.com/linux-system-roles/auto-maintenance/#lsr_role2collectionpy - this script was designed to convert legacy roles into collection format, with some rewriting of modules to use the FQCN.  It uses ruamel in roundtrip mode so that the yaml that is written is as close to the original as possible.  (There are a few problems with ruamel, such as no control of line length wrapping - indentation of embedded comments - and a few other minor nits).

Reply all
Reply to author
Forward
0 new messages