Quiz Entry - updated: 2026.11.12
How does ansible.builtin.blockinfile stay idempotent when it inserts several lines, and what is the marker option for?
It wraps the block in marker comments (# BEGIN ANSIBLE MANAGED BLOCK / # END ANSIBLE MANAGED BLOCK); on the next run it finds those markers and replaces what is between them instead of adding a second copy. marker adapts the markers to the file's comment syntax or makes them unique.
* The markers are how blockinfile recognises its own block on the next run. *
- name: Add additional lines to a file
ansible.builtin.blockinfile:
path: /path/to/file
block: |
First line in the additional block of text
Second line in the additional block of text
state: present
The file then contains:
# BEGIN ANSIBLE MANAGED BLOCK
First line in the additional block of text
Second line in the additional block of text
# END ANSIBLE MANAGED BLOCK
block: | is a YAML literal block, which keeps the line breaks.
Two reasons to set marker:
- Comment syntax.
#is not a comment in every format; in XML it would break the file.marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->"uses the right syntax;{mark}becomesBEGINorEND. - Several blocks in one file. Two tasks with the same default markers would overwrite each other's block. Give each its own marker, for example
# {mark} ANSIBLE MANAGED BLOCK: ntp.
Go deeper:
ansible.builtin.blockinfile: marker (official docs) โ the
{mark}placeholder andmarker_begin/marker_end.