[colug-432] Automatically Edit Configuration File

Jon Miller jonebird at gmail.com
Thu Nov 19 12:18:16 EST 2015


Looks like that module is much like the 'lineinfile' module in that it is
operating on a single line. My thought would be to write a custom module to
be used by Ansible. I've written a few myself and its not hard to do
especially if you're writing it in Python.

Reason I think the custom module is the correct route to go is because you
need to have a partial state machine in your parser to ensure you're editing
'Require' lines that only exist within the '<Directory />' sections. You can
have a simple 'in_directory' boolean and then only edit 'Require' lines when
that is true.

Ansible Module Development: http://docs.ansible.com/ansible/developing_modules.html

-- 
Jon Miller

Greg Sidelinger writes:

> Try the replace module.
>
> http://docs.ansible.com/ansible/replace_module.html
>
> On Wed, Nov 18, 2015 at 10:26 PM <jep200404 at columbus.rr.com> wrote:
>
>     How would you automate the editing of a configuration file as
>     described below?
>     
>     I am automating the configuration of a system with Ansible.
>     For /etc/httpd/conf/httpd.conf, I need to make sure that the
>     <Directory /> section has a "Require all granted" line instead
>     of a "Require all denied" line. "Require all" lines in other
>     sections need to be left alone.
>     
>     original /etc/httpd/conf/httpd.conf
>     
>     ...
>     <Directory />
>     AllowOverride none
>     Require all denied
>     </Directory>
>     ...
>     <Files ".ht*">
>     Require all denied
>     </Files>
>     ...
>     
>     desired /etc/httpd/conf/httpd.conf
>     
>     ...
>     <Directory />
>     AllowOverride none
>     Require all granted
>     </Directory>
>     ...
>     <Files ".ht*">
>     Require all denied
>     </Files>
>     ...
>     
>     I can write an awk script or probably even a sed script to do it,
>     but it is desirable to edit with a Ansible module instead of an
>     external shell command, because of the way Ansible modules
>     understand when something has really changed or not.
>     
>     I don't see how to use the lineinfile module in this situation,
>     because it will only change the last line to match a regex.
>     Matching "Require all" would change the line in the <Files ".ht*">,
>     section instead of in the <Directory /> section.
>     
>     It would be easy to just replace the whole file with a new one from
>     Ansible, but that is also not preferred because when some new
>     version of httpd.conf arrives, the wholesale replacement would
>     clobber other changes in the configuration file.
>     _______________________________________________
>     colug-432 mailing list
>     colug-432 at colug.net
>     http://lists.colug.net/mailman/listinfo/colug-432
>     



More information about the colug-432 mailing list