Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserve the order of dictionaries when applying changes #29

Open
richm opened this issue Apr 27, 2023 · 2 comments
Open

preserve the order of dictionaries when applying changes #29

richm opened this issue Apr 27, 2023 · 2 comments

Comments

@richm
Copy link
Contributor

richm commented Apr 27, 2023

For example, https://github.com/linux-system-roles/.github/blob/main/inventory/host_vars/network.yml#L9

  extra_vars:
    test_playbook: tests_default.yml
    network_provider: nm

when this was applied to the role, the order of the keys was not preserved, leading to a spurious change: https://github.com/linux-system-roles/network/pull/603/files#diff-20d1610925e550787a5f9dd9061f1e8ce9cee711b8f8165cd066b151179a9ba9L4

extra_vars:
-  test_playbook: tests_default.yml
  network_provider: nm
+  test_playbook: tests_default.yml

While new version is still semantically equivalent to the old version, it is confusing to reviewers. We should attempt to reduce that confusion by preserving the order of the elements e.g. is there some way to make Ansible/Jinja use an OrderedDict when parsing/writing out the elements? If there isn't, then maybe there is some way to sort the keys - the first time we do this we may see a large change, but subsequent changes should be much smaller.

@spetrosi

@spetrosi
Copy link
Contributor

It's a mystery to me why Ansible reorders keys. But even though Ansible reorders the keys, it does this consistently. i.e. in https://github.com/linux-system-roles/network/pull/607/files#diff-20d1610925e550787a5f9dd9061f1e8ce9cee711b8f8165cd066b151179a9ba9R5 test_playbook: tests_default.yml was added to a file manually earlier, then our automation reordered the lines, but the next time you run the automation the lines won't be reordered. So, going forward the file won't change. Only some manual edits to the file can be overwritten.

We can sort the keys with sort to ensure that they are in alphabetical order, but this would create much noice indeed.

@richm
Copy link
Contributor Author

richm commented Apr 28, 2023

It's a mystery to me why Ansible reorders keys. But even though Ansible reorders the keys, it does this consistently. i.e. in https://github.com/linux-system-roles/network/pull/607/files#diff-20d1610925e550787a5f9dd9061f1e8ce9cee711b8f8165cd066b151179a9ba9R5 test_playbook: tests_default.yml was added to a file manually earlier, then our automation reordered the lines, but the next time you run the automation the lines won't be reordered. So, going forward the file won't change. Only some manual edits to the file can be overwritten.

ok - then maybe this isn't really a problem?

We can sort the keys with sort to ensure that they are in alphabetical order, but this would create much noise indeed.

Yes, but maybe we shouldn't do anything just yet until we can confirm that this is an ongoing problem. See also ansible/ansible#17763 - maybe there is a way to denote that a dict should be parsed/formatted with an OrderedDict

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants