I don’t think you can get line_in_file to do what you want. line_in_file is designed to operate on a single line. Keeping the commented out line greatly complicates matters. How many patchlevels to you intend to maintain as comments?
You may be able to do it with replace and a lot of testing. But I’d strongly encourage you to reconsider using # as a poor replacement for revision control.
For state=present, the pattern to replace if found. Only the last
line found will be replaced.
For state=absent, the pattern of the line(s) to remove.
Note the difference between the states *present/absent*. If *absent*
all matches are removed. If *present* only the last one is replaced.
The default state is *present*.
One of the scenarios on how to proceed might be removing all redundant
lines first. For example, given the file
I believe it’s always better to use designated Modules over or that said … you may want to get familiar (or at least be aware of ) where you can find any modules. that said … here is the (from that list) you can study it online or with the built-in help like one last comment … just like using designated modules for designated tasks in Ansible it’s always best to use designated Threads for different problems. This may not make a difference for you just now, but others who may be following the list have a chance to benefit from the solution somebody is digging out
Assuming you only have one line for each version, it will assure that the line for any version that’s marked false will be commented, and any line for a version marked true will be uncommented.
Where it gets hairy is when you have more than one line for any version. In that case, the second task only manages the first of the lines for that version. That shouldn’t happen, but you’ve already seen that it does sometimes. When it does, you can run the playbook with force_remove_versions set to true, and it will remove all the matching lines - both good and bad - before the second task puts the single entries back in the file, properly commented or not as the case may be.
For state=present, the pattern to replace if found. Only the last
line found will be replaced.
Thank you!
Assuming you only have one line for each version, it will assure
that the line for any version that's marked false will be
commented, and any line for a version marked true will be
uncommented.
Where it gets hairy is when you have more than one line for any
version. In that case, the second task only manages the first of