What ideas do others have on Best Practice for using lineinfile?
As a potential conversation starter, here’s a couple of ideas that have recently gelled for me:
Use two rules:
First delete all versions of the line
Then add the finished version of the line.
This makes the process more idempotent (which, granted lineinfile has inherent problems with…)
For context, this idea gells around adding a mailrelay statement for postfix’s main.cf. In my environment I have 4 different platforms with various ad-hoc maintenance (covering quite a few years).
Use a (trailing) comment header (i.e. a “here are our changes” line) and then append changes after this.
Some more context here, I suspect lineinfile appeals to those of us who have an existing set of servers that we are incrementally bringing into line. The “correct” path is likely to use a template of a file, but that requires determining which of the (our various) distro’s is the “correct” set of settings. For something like “use our mail relay” this is a fairly large hurdle.
Your reply, while correct, seems designed to shut down useful feedback. Perhaps you did not mean it that way.
I noted up front that:
The “correct” path is likely to use a template of a file
I’m aware that lineinfile has problems. Which is why I’m requesting feedback on how one might use it best.
For a lot of use cases lineinfile, is an immediately useful expedient step, with advantages that far outweigh it’s disadvantages.
In the case I suggested, for example:
adding a mailrelay statement for postfix’s main.cf.
I have 6 different legacy versions of main.cf floating around, covering two different distributions. Doing it the correct way means:
Reading through each version to determine if there is something special about a given setup.
Researching the parts I do not fully understand
Determining which of the differing parts should be in the final version
That work is good work, but it’s a lot of work. It’s not unrealistic to suggest that’s a week or more of work (postfix is pretty flexible, which translates to reading though a lot of options).
Most of those options don’t really have a final “correct” setting. But I know up front what relayhost should be.
you might have a look to the augeas role. I’ve not used it with ansible but with puppet, and if you are changing a standard configuration file is probably the best approach. Even if you don’t want to use the role, use augeas with a ‘command’ might also help