Hello Ansible Community,
I’m currently working on integrating Ansible with other tools and need to embed metadata directly in YAML files (such as host_vars
and group_vars
) that Ansible processes. The ideal solution would ensure Ansible completely ignores this metadata, treating it as invisible.
Approaches I’ve Explored:
-
YAML Document Separator (
---
) Unfortunately, using---
to separate metadata from variables broke Ansible’s parsing of the YAML file. -
YAML Anchors: I tried defining an anchor (
&my_metadata:
) and skipping references to it, but Ansible still loaded the data with a “null” key.&my_metadata: one: two three: four
turned out as:
null: one: two three: four
-
_ansible_
Prefix: Inspecting the Ansible source code, particularlyclean.py
, I found out that it strips away variables starting with_ansible_
. Leveraging this, I experimented with using a variable like_ansible_my_metadata_
to store metadata. It seems Ansible ignores this variable—no errors are thrown, and it does not appear when I print all variables with{{ vars }}
. -
Comments: I considered using comments for storing the metadata but parsing them is not ideal, as the external tool I’m using leverages PyYAML, which struggles with it.
-
Obscure Keys: I also considered using obscure keys for my metadata, but there’s a risk that Ansible could potentially use or process the metadata, which I wish to avoid.
-
Separate Files: While storing metadata separately works, I was hoping to keep everything within the Ansible YAML structure for enhanced portability.
Questions for the Community:
- Are there any other methods for embedding metadata that Ansible will reliably ignore?
- Are there potential drawbacks to relying on the
_ansible_
prefix behavior that I should be aware of?