module

Krikri::PluginHelpers::BlockEditor

BlockEditor - pure marker-delimited-block matching/insertion logic for the blockinfile plugin, factored out the same way LineEditor is for lineinfile so it can be unit tested without touching the filesystem.

Behavior verified empirically against ansible-playbook (not assumed from docs): the begin/end marker lines are matched by exact equality (not regex), an existing block's insertion position never moves once found (only its interior is rewritten), a fresh block is inserted with no surrounding blank line via the exact same insertafter/insertbefore rules lineinfile already uses (default EOF), and an unchanged run's message is the empty string (not e.g. "Block already present").

Class methods

apply(lines : Array(String), marker_begin_line : String, marker_end_line : String, block_lines : Array(String), state : String, insertafter : String | Nil, insertbefore : String | Nil, append_newline : Bool = false, prepend_newline : Bool = false) : Tuple(Array(String), Bool)

Returns {new_lines, changed}. append_newline/prepend_newline mirror Ansible's blank-line padding around the block (present state only): prepend puts a blank line between the preceding content and the block (skipped at BOF or when the preceding line is already blank), append puts one between the block and what follows it (skipped at EOF or when that line is already blank) - both no-ops on reruns, since the blank line they add satisfies them the next time.

Real blockinfile.py builds its marker-delimited blocklines only when the task is present AND the block is non-empty (if present and block:), so a missing/empty block means "remove", but the two padding flags are gated on present ALONE - a state: present task with no block: at all still inserts the blank line they ask for and reports changed (verified against ansible-core 2.19.11).

Source