What is ansible_managed for, how do you use it in a template, and how should it be set in current Ansible?
It puts a "this file is managed by Ansible" note at the top of deployed files, so nobody edits them by hand; write # {{ ansible_managed }} in the template. The old ansible_managed = ... setting in ansible.cfg is deprecated; set it as a normal variable instead.
# {{ ansible_managed }}
# DO NOT MAKE LOCAL MODIFICATIONS TO THIS FILE BECAUSE THEY WILL BE LOST
Rendered: # Ansible managed followed by the warning.
A template overwrites the whole file on every run where the content differs. An admin who fixes something directly on the host loses that fix at the next run, often without noticing. The header tells them to change the template (or its variables) instead.
How the text is defined:
- Older way (still common in material):
ansible_managed = Ansible managedin the[defaults]section ofansible.cfg. That setting is deprecated, scheduled for removal in ansible-core 2.23. - Current way: set
ansible_managedlike any other variable, for example ingroup_vars/all.yml, or use your own variable name.
Either way, put it inside the target format's comment syntax (#, ;, <!-- -->); the variable only supplies the text.
Go deeper:
Ansible configuration settings: DEFAULT_MANAGED_STR (official docs) โ the setting, its default and its deprecation.