Re: [ALUG] Inserting env vars or other data into config files
On 09/05/12 17:59, Martijn Koster wrote:
I'd use a little script that greps for some markers to see if the file needs changing (or already has been), and then apply the required changes with diff/patch.
How would I make a change like this using diff/patch? Eg: If I have a config file that says (real example from ssmtp): .... AuthUser=someuser AuthPass=somepass mailhub=somehost:587 ... .. and I want to set the values of someuser/somepass/somehost, what would the input to patch look like? (A pointer to relevant resources would be fine!) Is there a reason why patch would be preferable to sed for something like this? Mark -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER
On Wed, 09 May 2012 18:42:46 +0100, mark@quarella.co.uk said:
what would the input to patch look like? (A pointer to relevant resources would be fine!)
Create a "template" file, eg: AuthUser=someuser AuthPass=somepass mailhub=somehost:587 Create one "real" file, eg: AuthUser=johnlennon AuthPass=pennylane mailhub=apple:587 Create the patch file: $ diff -U3 template real > mypatchfile The patch file looks like this: --- template 2012-05-10 07:19:44.000000000 +0100 +++ real 2012-05-10 07:19:57.000000000 +0100 @@ -1,4 +1,4 @@ - AuthUser=someuser - AuthPass=somepass - mailhub=somehost:587 + AuthUser=johnlennon + AuthPass=pennylane + mailhub=apple:587
Is there a reason why patch would be preferable to sed for something like this?
No, but there are many reasons why Puppet (or similar) would be preferable. To create each patch file (or sed script), you need to craft, by hand, the "real" version of the file above. If you're going to do that, you may as well just copy it to the destination anyway. -- "You can have everything in life you want if you help enough other people get what they want" - Zig Ziglar. Who did you help today?
On 10/05/12 07:22, Keith Edmunds wrote:
Create a "template" file, eg:
AuthUser=someuser AuthPass=somepass mailhub=somehost:587 [...]
OK, thanks for that, it's option to look at.
Is there a reason why patch would be preferable to sed for something like this? No, but there are many reasons why Puppet (or similar) would be preferable. To create each patch file (or sed script), you need to craft, by hand, the "real" version of the file above. If you're going to do that, you may as well just copy it to the destination anyway.
I can see that with diff, but I don't really see it with sed; all I'm telling it to do is take the distro-provided file, and make certain changes to it, leaving everything else the same. Generally speaking I would trust the distro to get the defaults right so if the distro has a newer version I'd want to use that but apply my changes to it. Is there a general tool for config file manipulation? Something that you just tell to set "AuthUser=myuser" and it would locate the relevant line and edit it, or insert it if not present. Something like sed or awk will do this but not in a simple way, such as: sometool myconfig.conf AuthUser=myuser Since everything in Linux uses text-based config files I'd have thought there was a standard way to edit them from the command line (something like Postfix's postconf but generalised). -- Mark Rogers // More Solutions Ltd (Peterborough Office) // 0844 251 1450 Registered in England (0456 0902) 21 Drakes Mews, Milton Keynes, MK8 0ER
participants (2)
-
Keith Edmunds -
Mark Rogers