I'm trying to solve a problem where I want to add a setting 'pci=noaer' to all kernel definitions in /boot/grub/grub.conf, but only if the setting doesn't exist. I've managed to get the basic functionality to work with the replace module, in that the module WILL add the setting to the relevant lines, but the problem is in NOT adding the setting to lines where it already exists. In other words, if I run the playbook once, it'll add the setting as needed, but every other run of the playbook after that adds another instance of the setting to each line. The following is an example of the line I want to update and the regexp and replace lines I currently have configured to update it:
Line to change:
kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3
After the playbook runs the above line should look like:
kernel /vmlinuz-2.6.32-279.22.1.el6.x86_64 ro root=/dev/sda3 pci=noaer
Relevant replace module parameters:
regexp: '(^\s+kernel /vmlinuz.+(?!pci=noaer).*$)'
replace: '\1 pci=noaer'
I've tried using the \b sequences to clearly define the boundaries of pci=noaer (e.g. (?!\bpci=noaer\b)), but with no luck. I've tested this a bit on regex101 but haven't really been able to definitively understand why the negative lookahead isn't doing what it's supposed to be doing (looking for cases where the pci=noaer string isn't there) - or how I'm using it within the replace module. I've had similar issues with negative lookaheads/lookbehinds in lineinfile, and I'm perfectly willing to admit this is total user error, given that I'm fairly new to ansible and python, and my regex skills are fairly limited thus far. But I've searched and searched and searched and I can't seem to find any good indication of why the negative lookaround sequences don't work the way I expect them to within these modules. Could this be a case of not properly managing the greediness of my regex configuration?
For what it's worth, I have seen that lookahead/lookbehind might have issues in some of the 2.x versions of Ansible, so perhaps this is a version problem? I'm running Ansible 2.6.20 with an underlying Python version of 2.6.6. If this is indeed an issue with my version, is there a way around it without having to update to something more recent?
A bit of additional information: This is being done across multiple CentOS 6-based systems where I don't feel like I can necessarily expect the kernel lines in the grub.conf file to be the same from one system to the next (perhaps they should be, but that's a topic for a different discussion). That's why the fairly non-specific regex. Also, since it's CentOS 6, there is no equivalent of update-grub or grub2-mkconfig for that OS. It's just a matter of editing the /etc/grub.conf or /boot/grub/grub.conf file and rebooting.