Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Perl module for automating the modification of config files?

by nysus (Parson)
on Oct 04, 2020 at 05:44 UTC ( #11122529=perlquestion: print w/replies, xml ) Need Help??

nysus has asked for the wisdom of the Perl Monks concerning the following question:

I want to make it easier to modify configuration files. For example, let's day I want to edit a postfix config file according to the directions here.

So I started writing simple code in a file that could be interpreted by perl to make the changes for me with one command per line:

uc mail_owner # "uc" is the command for "uncomment" uc hostname cv hostname {{fqdn}} # "cv" is the command for "change value", {{fqdn} +} is replaced with appropriate value ...

You get the idea. I started writing some code to interpret my config file modification commands and then realized someone had to have tackled this problem before. I did a search on metacpan but came up empty. Anyone familiar with this problem space and can help point me in the right direction?

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest Vicar";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Perl module for automating the modification of config files?
by Arunbear (Prior) on Oct 04, 2020 at 14:07 UTC
    This problem space is called configuration management and there are a number of tools being used for this e.g. Ansible, Salt, Puppet etc.

    Rex is the only one implemented in Perl that I know of (not sure how much that matters though).

      These solutions are meant for mid to large scale remote deployment of fixed versions.

      I suppose the actual management of config files is primarily done with templates° and must - if properly done - be adjusted for every new software version. This pays off when scalability matters.

      But the ability to adjust to an extended config file of a new version for a single installation will be limited, especially if it comes in the OP's shown form of a default cfg where one is supposed to uncomment options.

      In this case

      • storing the old default
      • alarming about diffs
      • adjusting the template manually

      might be the only pragmatic solution to have some efficiency.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      update

      °) please correct me otherwise!

Re: Perl module for automating the modification of config files?
by LanX (Sage) on Oct 04, 2020 at 11:41 UTC
    Your question is very generic and fuzzy (like so often).

    In short: It depends!

    In long: ...

    In order to change an arbitrary given config format you'd use a bunch of regex.

    If a fixed format is known beforehand people would prefer templates (plenty on cpan) to create it.

    Others define their own DSL (set of functions representing cfg actions)

    If the format language could also be dynamically expanded in future versions, one would also need rules for a parser and validator.

    This whole field is like manipulating XML, here might be dragons.

    update

    It's actually even more complex than XML because the example you are showing looks a lot like bash and inserts $variables.

    I.o.W. it's already a DSL.

    You are asking for a module which manipulates any other config language in a generic way.

    Holy grail...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Re: Perl module for automating the modification of config files?
by likbez (Sexton) on Oct 05, 2020 at 03:16 UTC
    In the past sysadmins often did this task using a programmable editor( vi/vim, emacs, THE, etc ).

    There are also some newer editors that use LUA as the scripting language, but none with Perl as a scripting language. See https://www.slant.co/topics/7340/~open-source-programmable-text-editors

    Here, for example, is a fragment from an old collection of hardening scripts called Titan, written for Solaris by Brad M. Powell. Example below uses vi which is the simplest, but probably not optimal choice, unless your primary editor is VIM.

    FixHostsEquiv() { if [ -f /etc/hosts.equiv -a -s /etc/hosts.equiv ]; then t_echo 2 " /etc/hosts.equiv exists and is not empty. Saving a co +py..." /bin/cp /etc/hosts.equiv /etc/hosts.equiv.ORIG if grep -s "^+$" /etc/hosts.equiv then ed - /etc/hosts.equiv <<- ! g/^+$/d w q ! fi else t_echo 2 " No /etc/hosts.equiv - PASSES CHECK" exit 1 fi

    For VIM/Emacs users the main benefit here is that you will know your editor better, instead of inventing/learning "yet another tool." That actually also is an argument against Ansible and friends: unless you operate a cluster or other sizable set of servers, why try to kill a bird with a cannon. Positive return on investment probably starts if you manage over 8 or even 16 boxes.

    Perl also can be used. But I would recommend to slurp the file into an array and operate with lines like in editor; a regex on the whole text are more difficult to write correctly then a regex for a line, although experts have no difficulties using just them. But we seldom acquire skills we can so without :-)

    On the other hand, that gives you a chance to learn splice function ;-)

    If the files are basically identical and need some slight customization you can use patch utility with pdsh, but you need to learn the ropes. Like Perl the patch utility was also written by Larry Wall and is a very flexible tool for such tasks. You need first to collect files from your servers into some central directory with pdsh/pdcp (which I think is a standard RPM on RHEL and other linuxes) or other tool, then to create diffs with one server to which you already applied the change (diff is your command language at this point), verify that on other server that this diff produced right results, apply it and then distribute the resulting files back to each server using again pdsh/pdcp. If you have a common NFS/GPFS/LUSTRA filesystem for all servers this is even simpler as you can store both the tree and diffs on common filesystem.

    The same central repository of config files can be used with vi and other approaches creating "poor man Ansible" for you .

    2020-10-05&06 Athanasius changed <pre> to <code> tags, &quot; to ", etc.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11122529]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2021-12-05 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (31 votes). Check out past polls.

    Notices?