feat(slice-2): implement static file collector for YAML/INI and Ansible JSON dynamic inventory exporter
This commit is contained in:
1 parent
68802710b0
commit
951be389a1
11 files changed
+564
-22
No files matched your search
@@ -0,0 +1,40 @@
|
||||
import json
|
||||
from typing import Any, Dict
|
||||
from inventory_collector.exporters.base import BaseExporter
|
||||
from inventory_collector.models import InventoryData
|
||||
|
||||
class AnsibleJsonExporter(BaseExporter):
|
||||
"""Exports inventory in standard Ansible Dynamic Inventory JSON format with _meta.hostvars."""
|
||||
|
||||
def export(self, data: InventoryData) -> str:
|
||||
payload: Dict[str, Any] = {
|
||||
"_meta": {
|
||||
"hostvars": {}
|
||||
},
|
||||
"all": {
|
||||
"children": sorted(list(data.groups.keys()))
|
||||
}
|
||||
}
|
||||
|
||||
# Populate hostvars
|
||||
for name, host in sorted(data.hosts.items()):
|
||||
hvars: Dict[str, Any] = {
|
||||
"ansible_host": host.ansible_host,
|
||||
"ansible_user": host.ansible_user,
|
||||
"ansible_port": host.ansible_port,
|
||||
}
|
||||
if host.ansible_ssh_private_key_file:
|
||||
hvars["ansible_ssh_private_key_file"] = host.ansible_ssh_private_key_file
|
||||
if host.tags:
|
||||
hvars["tags"] = host.tags
|
||||
for k, v in host.variables.items():
|
||||
hvars[k] = v
|
||||
payload["_meta"]["hostvars"][name] = hvars
|
||||
|
||||
# Populate groups
|
||||
for grp_name, host_names in sorted(data.groups.items()):
|
||||
payload[grp_name] = {
|
||||
"hosts": sorted(host_names)
|
||||
}
|
||||
|
||||
return json.dumps(payload, indent=2) + "\n"
|
||||
Reference in new issue
Block a user