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).
On Mon, Sep 13, 2021 at 3:59 PM Matt Martz <[email protected]> wrote: > 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. > > On Mon, Sep 13, 2021 at 4:52 PM Ch Ho <[email protected]> wrote: > >> >> 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 <https://kislyuk.github.io/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 <https://github.com/ansible/ansible/issues/75696> was >> redirected to the mailing list. :-) >> >> Any ideas? >> >> Thanks! >> >> -- >> 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 [email protected]. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/ansible-project/7ba68c08-b728-480f-9d5f-090eaa2f1982n%40googlegroups.com >> <https://groups.google.com/d/msgid/ansible-project/7ba68c08-b728-480f-9d5f-090eaa2f1982n%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > > > -- > Matt Martz > @sivel > sivel.net > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/ansible-project/CAD8N0v_92GMWM%2BQtst8vLeTGGQjZ%3DRRz%2BtPjOLofpQQcCyH0Hw%40mail.gmail.com > <https://groups.google.com/d/msgid/ansible-project/CAD8N0v_92GMWM%2BQtst8vLeTGGQjZ%3DRRz%2BtPjOLofpQQcCyH0Hw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CALF5A-LVCsPm3yxn-hXALT_OHp3t3mHm8DARYqPgkmO3HY_%2BkQ%40mail.gmail.com.
