'Ansible Playbook adding the same line multiple times to yml file

I have to add the same line several times in the same file. It must always be inserted after a certain line, which is the same for both locations.
I have tried it with the lineinfile module, but, there, only one line is always added. I have also tried with the replace module, but, then, no line is added at all.

My lineinfile task looks like this:

- name: Add Line
  ansible.builtin.lineinfile: 
    path: someyml.yml
    insertbefore: "template:"
    line: "{{ Cluster_API}}"

The replace task looks like this:

- name: Replace
  replace:
    path: someyml.yml
    regexp: '^(template:\n)(?!{{ Cluster_API }})'
    replace: '\1 {{ Cluster_API }}\n'

I know how the replace is written it should insert a line after template: but it doesn't even do that.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source