saltstack/salt — pull request #8048 by regilero (2013-10-23 14:52:59+00:00)
Title: Add salt.states.file.blockreplace to maintain a comment delimited block of text in an unmanaged file

When facing files which are not handled by ``states.file.managed`` it's always quite hard to maintain a block of edited content, while removing old managed content and keeping manual content intact.

I encouter this need on files like /etc/hosts but it could be also for a bind configuration file, or crontabs with several identical entries like in #7979. One way to do it would be ``states.file.replace`` with a MULTILINE regex. But, I do not like regex, and they do not work on multiline (see #7999). ``states.file.replace`` has also some problems like in-place editing via ``fileinput.input`` which allows parallel reads of an empty or partially filled file by other process while editing is in place. I've tried to reduce this problem using the salt atomic_file open method.

So I made this basic task, and I made it directly in states.file and modules/files as I also like the ``states.file.accumulated``` state and I used it to collect data for the edited block (without the templating fine usage).