When I fist started to use yedit to modify YAML files I got the following warning:

[WARNING]: The value {...} (type dict) in a string field was converted to "{...}" (type string). If this does not look like what you expect, quote the entire value to ensure it does not change.

 

My playbook did look similar to the examples from kwoodson/ansible-role-yedit:

- name: add client ssh key to yaml config for borg backup
  yedit:
    src: "{{ host_vars_dir }}/{{ backup_destination }}.yml"
    key: "borgbackup_clients#{{ inventory_hostname }}"
    separator: '#'
    value:
      ssh_key: "{{ sshkey.stdout }}"
  delegate_to: localhost

After some research I found the proposed solution to disable the warning which I did not like.

Later on I figured out the parsing can be improved using the to_yaml filter:

- name: add client ssh key to yaml config for borg backup
  vars:
    cfg:
      ssh_key: "{{ sshkey.stdout }}"
  yedit:
    src: "{{ host_vars_dir }}/{{ backup_destination }}.yml"
    key: "borgbackup_clients#{{ inventory_hostname }}"
    separator: '#'
    value: "{{ cfg | to_yaml }}"      
  delegate_to: localhost
  throttle: 1

Furthermore I experienced applying this play to multiple hosts simultaneously does not work. Therefore I added the keyword throttle to ensure this task is executed one host after another.