How to edit after a specific line in a text file after searching

Hi everyone, I am currently trying to automate the hardening of machines with the use of the audit.pol file from the group policy software. I have managed to automate the parsing of the file into a text file with the help of LGPO software but I am trying to figure out how to do the following:

-Check the text file if a setting exist (Settings are usually a few lines long)
-If it exist simple update the value
-If it does not exist, append the settings into the text file

I have attached a screenshot of how the text file looks like a template of how a setting will look like:

Computer
PathToRegistry
RegistryName
RegistryValue

So my first step is to search if the registry name exist in the text file, if it exists then i have to edit the next line after it which is registry value. If it doesn’t append the 4 lines of details in the format shown above. Searching if the registry name exist can be done by storing the text file as a registry in the ansible playbook however how do I edit the next line after the registry name? And is it possible to do the if else in a single task?

(attachments)