I need to use replace to find a regexp that includes \n; however, I keep getting an error. How can I properly do this (don’t even know if I am using back reference correct either, but if I can get past this error I think I can figure that part out)?
Thanks in advance!
Sorry, last error message is slightly innaccurate… I was testing a change in that one (difference is brackets ):
`
TASK [dns_update : include_tasks] ********************************************************************************************************************************************************************************************************
fatal: [ansible-ub18]: FAILED! => {“reason”: “Syntax Error while loading YAML.\n found unknown escape character\n\nThe error appears to have been in ‘/etc/ansible/role/dns_update/tasks/Ubuntu18.yml’: line 7, column 15, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n regexp: ".nameservers.$\n.addresses:(.$)"\n replace: "\1 {{ dns1 }}, {{ dns 2}}"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - "{{ foo }}"\n”}
I’d recommend any time you need to use escape characters, that you use single quotes around the value, instead of double quotes. It will resolve many of the issues with how pyyaml does it’s parsing
I have tried single quotes too around the regexp, but get a similar issue
`
TASK [dns_update : include_tasks] ********************************************************************************************************************************************************************************************************
fatal: [ansible-ub18]: FAILED! => {“reason”: “Syntax Error while loading YAML.\n found unknown escape character\n\nThe error appears to have been in ‘/etc/ansible/role/dns_update/tasks/Ubuntu18.yml’: line 7, column 15, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n regexp: ‘.nameservers.$\n.addresses:(.$)’\n replace: "\1 {{ dns1 }}, {{ dns 2}}"\n ^ here\nWe could be wrong, but this one looks like it might be an issue with\nmissing quotes. Always quote template expression brackets when they\nstart a value. For instance:\n\n with_items:\n - {{ foo }}\n\nShould be written as:\n\n with_items:\n - "{{ foo }}"\n”}
Thx all. Those helped. I now get it to replace, but it replaces my string and not just my backref. Not sure what I am doing wrong. I think that is just the default behavior of replace. Can’t use quantifiers with negative look aheads/behinds.
I didn’t post it so as to not overburden everyone with my issues; however, if it isn’t a big issue I could use some help as I can’t seem to get it working. I am trying to replace the DNS servers, located in the second addresses line toward the bottom.
I tried it, and the regexp test shows that the regexp is good, but it doesn’t replace the line. It just says ok, and continues on. Here is my slightly modified variant also doing the same thing (or to be more specific, not doing anything):
I tried it, and the regexp test shows that the regexp is good, but it
doesn't replace the line. It just says ok, and continues on. Here is my
slightly modified variant also doing the same thing (or to be more
specific, not doing anything):
You are right… I switched to lineinfile sometime between yesterday and today. I switched back to replace due to your success (and I prefer to use it as I believe it will replace all found instances).
Seems to be working now, however, one follow-up question regarding backrefs. I thought \1 replaced the content of group1? Is that incorrect? The content that was replaced doesn’t fall in a group (sorry, I lack experience with backrefs).
Here is my regex tester. You can see the “full match” is replaced, and not the “Group 1”: https://regex101.com/r/jfGg83/4
Oh, I think I misunderstood (as I suspected) how backrefs work with substitution. I thought what was referenced was being replaced, when in reality it seems to just be putting back what we want to keep and just replacing the rest.
Yes, all the things matching the regexp: line will be replaced with the content of the replace: line.
Since nameservers: and address: shouldn't be discard/replace, the parenthesis around them make it so that we can reference them as \1 in the replace: line.