Skip to content

raise exception on duplicate keys on the same document hierarchy level #59

@gburiola

Description

@gburiola

At the moment, reclass allows duplicate keys in any YAML file and silently uses the last value.

For example, this node file:

$ cat /tmp/reclass/nodes/node1.yml
parameters:
    key1: True
    key2: other value
    key1: False

Returns the following:

$ reclass  --inventory -b /tmp/reclass/ | yq -y .nodes.node1.parameters
_reclass_:
  environment: base
  name:
    full: node1
    short: node1
key1: false
key2: other value

In a very large YAML file, this is an easy to make mistake and can have very bad consequences.

We could use something like what's described in the link below to look for duplicate keys on reclass/storage/yamldata.py
https://gist.github.com/pypt/94d747fe5180851196eb

That would throw an error on yaml.load():

while constructing a mapping
  in "/tmp/reclass/nodes/node1.yml", line 2, column 5
found duplicate key (key1)
  in "/tmp/reclass/nodes/node1.yml", line 4, column 5

If you're happy with this approach I'm happy to implement it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions