I have a kernel line in my grub.conf i would like to append “pcie_aspm=off” to if it’s not there.
To my understanding backrefs would possibly be able to do that ?
I don’t wanna template up grub.conf just for that small addition, but do not want to replace the whole kernel line either as it might not be 100% the same on all my systems.
Yes this is possible with backrefs - aka capturing groups- and not just at the end of the line.
one possibility is to leverage negative lookahead regexp [1]
then one call to lineinfile is enough.
warning, the regexp can be quite complex
here is a simplified l example of a configuration file that has a line
starting with something like
OPTIONS=“-a option1 -b another:option -x -z athird.option”
the point is to check if -x is NOT there and add it as the first option (could have been added at the end by swapping -x and \1)
Thanks for this example! , Lineinfile is very powerful, but documentation and regex does not explain it explicitly. Like how to use backrefs and more detailed examples for these would be great! (Like how to add stuff to end of line, start of the line, before/after the regex, replace line and EOF/BOF)
Thanks, that confirmed what I feared strange regex options to do something somewhat simple.
I hope someone will be able to document this better in the future, or make a simpler way to do this. Then again I have no clue if anyone other than me would find this useful.
Until then I will stick to a sed command to not pull my already grey hair completely away
I should say it’s almost always better to just template the file unless you think the user is going to alter it.
In any event, I’ll remind everyone the examples and documentation are open source. The source to the module contains the docstring that gets rendered, so additional examples (as well as patches) are welcome, and easily added!
It’s hard for me to tell what the best way to explain something to someone else is sometimes, so it’s usually best for those who figure things out to submit something. In some cases we may edit/polish things up a bit or have some comments or suggestions of things to add.
At least for grub configs I’m not happy with templating, if i fx. do a kernel upgrade it will not point at the right stuff anymore… but that can be templated as well i know