add 2 lines with lineinfile but only if not already there

I’m new to Ansible and learning a lot but I have a stupid question. I just can not quite see the answer.
I have a file

aaa
bbb
ccc

I want to add
ddd
eee

but only if it does not already exist. So I can rerun playbook without creating dups.

I’ve tried register and when.
I have tried using backrefs=yes

It is just slipping though my fingers. I’m sure this is child’s play for many of you . Please instruct…

You would need to use regexp. So, if you are trying to write ddd, the defined regexp would be :

regexp:‘^ddd’

This way lineinfile will look for a line matching that regexp before writting. If it finds that line, it will get overwritten with the same content. Else, it will be added. state = present also needs to be specified.