To replace an existing line, you will want to use backrefs. Otherwise, I believe the default behavior is to insert the line after EOF or the last match of a specified regexp. Insertafter and backrefs are exclusive of each other, since the line will be added after, or replace an existing line, depending which you use.
So, maybe this would work (add backrefs and remove insertafter):
The trick now is that you have duplicates in your files, and I don’t yet have a great way to clean those up. Maybe a task with state=absent and then a task to add it back. I’m not entirely sure whether state=absent removes all matches, or just the last match, since I don’t use it much and it’s not documented.
Also, backrefs doesn’t work with create=yes. I discovered that unfortunate fact recently.