fix(cache): keep the vault tag when utils.cache.yaml loads a !vault block

utils.cache.yaml registered its own !vault constructor that returned a plain str, and it overrides the VaultScalar constructor of utils.handler.vault whenever it registers last. Every writer that loads a host_vars file through the cached loader, changes one key and dumps it back then re-emitted each credential as an untagged quoted string, and Ansible handed the $ANSIBLE_VAULT block itself to the roles.

08680e933d and c88647b3a1 moved tor_node.py and network_mode.py to the round-trip helpers. The shared loader now returns a VaultScalar, so every yaml_io writer keeps the tag regardless of import order. VaultScalar subclasses str, so the other readers of the cached loader see the same value.

test_a_round_trip_keeps_the_vault_tag fails against the previous constructor: re-registering the plain-str constructor on CSafeLoader and SafeLoader drops '!vault |' from the dumped file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-10-24 12:23:45 +02:00
1 parent 2b2f5dea36
commit 5725fe6c20
2 files changed
+25 -1

No files matched your search

@@ -5,6 +5,16 @@ from pathlib import Path
from cli.administration.inventory.provision.yaml_io import dump_yaml, load_yaml
from utils.cache.yaml import dump_yaml_str
_VAULT_CRED = """\
applications:
svc-db-mariadb:
credentials:
root_password: !vault |
$ANSIBLE_VAULT;1.1;AES256
33323532656330393531653863306437306438353765363637326435636235396237
3435626336306666643833633030333736316234623365610a303233663130636566
"""
class TestYamlIO(unittest.TestCase):
def test_load_yaml_missing_returns_empty_dict(self):
@@ -19,6 +29,17 @@ class TestYamlIO(unittest.TestCase):
dump_yaml(p, data)
self.assertEqual(load_yaml(p), data)
def test_a_round_trip_keeps_the_vault_tag(self):
with tempfile.TemporaryDirectory() as tmpdir:
p = Path(tmpdir) / "host_vars.yml"
p.write_text(_VAULT_CRED, encoding="utf-8")
data = load_yaml(p)
data["NETWORK_MODE"] = "multi"
dump_yaml(p, data)
out = p.read_text(encoding="utf-8") # nocheck: cache-read -- tempdir
self.assertIn("!vault |", out)
self.assertNotIn("root_password: '$ANSIBLE_VAULT", out)
def test_load_yaml_rejects_non_mapping(self):
with tempfile.TemporaryDirectory() as tmpdir:
p = Path(tmpdir) / "bad.yml"