I also have the need to config manage yaml files. I have written an ansible module that attempts to be idempotent while editing yaml files.
I call it yedit (yaml-edit). https://github.com/kwoodson/ansible-role-yedit
Please let me know if you find it useful. I’ll be adding features when our team runs into the need, by request, or by pull-request.
Here is a simple playbook example:
roles:
- lib_yaml_editor
tasks:
- name: edit some yaml
yedit:
src: /path/to/yaml/file
key: foo
value: bar
- name: more complex data structure
yedit:
src: /path/to/yaml/file
key: a#b#c#d
value:
e:
f: This is a test
Should produce something that looks like this:
foo: bar
a:
b:
c:
d:
e:
f: This is a test
Edit: 5/27/2018