Solution: Changing yaml files in ansible git repo with python
Description
I wanted tooling to make changes to an ansible git repository but found this not as straightforward as I had hoped. This solution takes care of the annoying problem of loading yaml and python writing it back out of its original order and so making the git commits hard to read for a single line change.
Solution is to use the ruamel.yaml library which preserves ordering. First time you use this on a file it might make some unexpected minor changes to whitespace but thats a bonus!
This example loads the yaml file and adds the command line argument ipaddr to the ip_address_list. Internally ip_address_list is a python array. While simple it shows the test_dictionary maintains order so the git commits are sane human readable changes.
Execute
Git log showing the results
Conclusion
This works well! For any production use just need to make sure to pull latest before making changes and finally push to origin. The script itself can be ran through a web interface as part of a multistage job. Next stage would be to actually run the playbook against the commit.