p3lim / vyaml

VyOS YAML config toolkit
GNU General Public License v3.0
10 stars 2 forks source link

Improve multiline banner support #9

Closed b- closed 11 months ago

b- commented 11 months ago

I have the following banner in my config:

[edit]
vyos@vyos-home# show system login banner pre-login
 pre-login "perchnet, a network experiment\nthis is vyos-home.perchnet.dn42"

VyOS interprets the \n sequences, and displays the banner as

perchnet, a network experiment
this is vyos-home.perchnet.dn42

When I run vyaml import, this gets expanded in the yaml as

system:
  login:
    banner:
      pre-login: 'perchnet, a network experiment

        this is vyos-home.perchnet.dn42'

which gets rejected by the API when I apply such a configuration with an error stating that newlines in strings are not allowed. (Apologies that I don't have the exact error on hand at the moment).

I didn't look at the vyaml code much yet, so I'm not sure if the VyOS API is returning the banner with the escape sequences already interpreted or if vyaml is somehow interpreting them, but either one shouldn't be too difficult to account for.

I intend to look more at the code and see if I can help with anything after the holiday

p3lim commented 11 months ago

Please try:

system:
  login:
    banner:
      pre-login: |
        perchnet, a network experiment
        this is vyos-home.perchnet.dn42
b- commented 11 months ago

Thanks! I'll try that later. But I don't think it was a syntax issue so much as the API rejecting a string with a literal newline, if I remember (and read) the error message correctly.

That reminds me, though, of the related issue I ran into and didn't write up yet — I don't want to nerd snipe myself with this too much today because I'm on my way to spend time with family (and I know I'll end up spending time with PyCharm instead if I don't make enough effort not to)… but I wanted to also make a general issue about improving “round-trips” for the sake of making it easier to and migrate and edit configs.

What I mean is, if I understand the spirit of vyaml enough, the following should return no differences:

$ vyaml apply -c vyaml.yml
$ diff <(vyaml render -c <(vyaml import)) <(vyaml render -c vyaml.yml)

But due to things like options that are set in the default config (logging and such), the import doesn't quite match the render.

Anyway, I'll play with this all later in the week. I'd love to be involved and think I could help. Thanks for writing this awesome piece of software!

p3lim commented 11 months ago

Vyaml is (unintentionally) not very strict, mostly because it doesn't use the VyOS python API much, which will likely change before 1.0 and be more in-line with the output from vyaml import.

p3lim commented 11 months ago

Also, the intention of vyaml is to fully control the config, vyaml apply will (essentially) reset the config to default before merging with the yaml file.

This is done atomically to prevent commits without changes.

b- commented 11 months ago

That's what I figured, and it's really cool! I think what I was trying to get at was that I'd be happy to document the differences and otherwise would like to be involved if it would help. This already is much more elegant to me than Ansible's VyOS support ever was.

p3lim commented 11 months ago

so I'm not sure if the VyOS API is returning the banner with the escape sequences already interpreted or if vyaml is somehow interpreting them

The VyOS API returns are 1:1 with the output of vyaml import, but I guess I'll have to go through it to deal with multiline values correctly.

What I mean is, if I understand the spirit of vyaml enough, the following should return no differences

That is not really the intention of the import command, it's more for getting started with vyaml without having to manually convert the entire config to yaml. There are runtime configuration options part of the import command that isn't required, a good example of this is system config-management commit-revisions, which are presented just because that's what VyOS' API outputs.

p3lim commented 11 months ago

vyaml import should correctly use multiline yaml notation now.

system:
  login:
    banner:
      pre-login: |-
        first line
        second line
set system login banner pre-login 'first line\nsecond line'