feat: release v1.1.1 - host sorting by name/IP, sync destination overrides, and ~ expansion
Build and Publish Release / Build Packages and Publish (push) Successful in 25s
Build and Publish Release / Build Packages and Publish (push) Successful in 25s
This commit is contained in:
1 parent
598e5d7397
commit
62710d8bb7
19 files changed
+190
-34
No files matched your search
@@ -1,6 +1,6 @@
|
||||
"""Universal homelab and fleet inventory collector and deduplicator."""
|
||||
|
||||
__version__ = "1.1.0"
|
||||
__version__ = "1.1.1"
|
||||
|
||||
_yaml_warning_emitted = False
|
||||
|
||||
|
||||
@@ -116,6 +116,28 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
dest="filter_source",
|
||||
help="Filter hosts by source provider (e.g. 'proxmox', 'static', 'file', 'k3s')."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--sort", "--sort-by",
|
||||
dest="sort_by",
|
||||
choices=["name", "ip"],
|
||||
default="name",
|
||||
help="Sort hosts by 'name' or 'ip' (default: name)."
|
||||
)
|
||||
parser.add_argument(
|
||||
"-r", "--reverse",
|
||||
action="store_true",
|
||||
help="Reverse the sort order."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--ansible-dest",
|
||||
type=Path,
|
||||
help="Target output path for Ansible inventory in --sync mode (overrides config)."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--lc-dest",
|
||||
type=Path,
|
||||
help="Target output path for linux-connector inventory in --sync mode (overrides config)."
|
||||
)
|
||||
return parser
|
||||
|
||||
def main(argv: Optional[List[str]] = None) -> int:
|
||||
@@ -205,6 +227,9 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
source=args.filter_source
|
||||
)
|
||||
|
||||
# 4c. Sort inventory by name or IP
|
||||
inv = inv.sort(sort_by=args.sort_by, reverse=args.reverse)
|
||||
|
||||
# 5. Inspection / Dry-run Mode
|
||||
if args.dry_run:
|
||||
filter_desc = []
|
||||
@@ -231,8 +256,17 @@ def main(argv: Optional[List[str]] = None) -> int:
|
||||
|
||||
# 7. Simultaneous Synchronization Mode (--sync)
|
||||
if args.sync:
|
||||
ansible_dest = app_config.output_ansible or Path("inventory/hosts.yaml")
|
||||
lc_dest = (app_config.output_linux_connector or Path("~/.config/linux-connector/hosts.yaml")).expanduser()
|
||||
ansible_dest = (
|
||||
args.ansible_dest
|
||||
or (args.output if args.output else None)
|
||||
or app_config.output_ansible
|
||||
or Path("inventory/hosts.yaml")
|
||||
).expanduser()
|
||||
lc_dest = (
|
||||
args.lc_dest
|
||||
or app_config.output_linux_connector
|
||||
or Path("~/.config/linux-connector/hosts.yaml")
|
||||
).expanduser()
|
||||
|
||||
AnsibleYamlExporter().write_to_file(inv, ansible_dest)
|
||||
if not is_quiet:
|
||||
|
||||
@@ -202,12 +202,12 @@ class ConfigLoader:
|
||||
destinations = data.get("destinations", {})
|
||||
if destinations:
|
||||
if "ansible" in destinations:
|
||||
config.output_ansible = Path(destinations["ansible"])
|
||||
config.output_ansible = Path(destinations["ansible"]).expanduser()
|
||||
if "linux_connector" in destinations:
|
||||
config.output_linux_connector = Path(destinations["linux_connector"])
|
||||
config.output_linux_connector = Path(destinations["linux_connector"]).expanduser()
|
||||
if "yaml" in destinations:
|
||||
config.output_yaml = Path(destinations["yaml"])
|
||||
config.output_yaml = Path(destinations["yaml"]).expanduser()
|
||||
if "xml" in destinations:
|
||||
config.output_xml = Path(destinations["xml"])
|
||||
config.output_xml = Path(destinations["xml"]).expanduser()
|
||||
|
||||
return config
|
||||
@@ -17,7 +17,7 @@ class AnsibleJsonExporter(BaseExporter):
|
||||
}
|
||||
|
||||
# Populate hostvars
|
||||
for name, host in sorted(data.hosts.items()):
|
||||
for name, host in data.hosts.items():
|
||||
hvars: Dict[str, Any] = {
|
||||
"ansible_host": host.ansible_host,
|
||||
"ansible_user": host.ansible_user,
|
||||
@@ -26,7 +26,8 @@ class AnsibleJsonExporter(BaseExporter):
|
||||
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
|
||||
tags_list = host.tags
|
||||
hvars["tags"] = tags_list
|
||||
for k, v in host.variables.items():
|
||||
hvars[k] = v
|
||||
payload["_meta"]["hostvars"][name] = hvars
|
||||
@@ -34,7 +35,7 @@ class AnsibleJsonExporter(BaseExporter):
|
||||
# Populate groups
|
||||
for grp_name, host_names in sorted(data.groups.items()):
|
||||
payload[grp_name] = {
|
||||
"hosts": sorted(host_names)
|
||||
"hosts": list(host_names)
|
||||
}
|
||||
|
||||
return json.dumps(payload, indent=2) + "\n"
|
||||
@@ -17,7 +17,7 @@ class AnsibleYamlExporter(BaseExporter):
|
||||
grouped_hosts = set()
|
||||
for group_name, host_names in sorted(data.groups.items()):
|
||||
group_dict: Dict[str, Any] = {"hosts": {}}
|
||||
for name in sorted(host_names):
|
||||
for name in host_names:
|
||||
host = data.get_host(name)
|
||||
if not host:
|
||||
continue
|
||||
@@ -37,10 +37,10 @@ class AnsibleYamlExporter(BaseExporter):
|
||||
structure["all"]["children"][group_name] = group_dict
|
||||
|
||||
# Ungrouped hosts placed directly under all.hosts
|
||||
ungrouped = {name: host for name, host in data.hosts.items() if name not in grouped_hosts}
|
||||
ungrouped = [host for name, host in data.hosts.items() if name not in grouped_hosts]
|
||||
if ungrouped:
|
||||
structure["all"]["hosts"] = {}
|
||||
for name, host in sorted(ungrouped.items()):
|
||||
for host in ungrouped:
|
||||
host_vars = {
|
||||
"ansible_host": host.ansible_host,
|
||||
"ansible_user": host.ansible_user,
|
||||
@@ -50,7 +50,7 @@ class AnsibleYamlExporter(BaseExporter):
|
||||
host_vars["tags"] = host.tags
|
||||
for k, v in host.variables.items():
|
||||
host_vars[k] = v
|
||||
structure["all"]["hosts"][name] = host_vars
|
||||
structure["all"]["hosts"][host.name] = host_vars
|
||||
|
||||
return structure
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ class GenericYamlExporter(BaseExporter):
|
||||
|
||||
def _build_structure(self, data: InventoryData) -> Dict[str, Any]:
|
||||
hosts_list: List[Dict[str, Any]] = []
|
||||
for name, host in sorted(data.hosts.items()):
|
||||
for name, host in data.hosts.items():
|
||||
h_dict: Dict[str, Any] = {
|
||||
"name": host.name,
|
||||
"ip": host.ansible_host,
|
||||
@@ -22,7 +22,7 @@ class GenericYamlExporter(BaseExporter):
|
||||
|
||||
groups_dict: Dict[str, List[str]] = {}
|
||||
for grp_name, members in sorted(data.groups.items()):
|
||||
groups_dict[grp_name] = sorted(members)
|
||||
groups_dict[grp_name] = list(members)
|
||||
|
||||
return {
|
||||
"hosts": hosts_list,
|
||||
|
||||
@@ -11,7 +11,7 @@ class IniExporter(BaseExporter):
|
||||
grouped_hosts = set()
|
||||
for grp_name, host_names in sorted(data.groups.items()):
|
||||
lines.append(f"[{grp_name}]")
|
||||
for h_name in sorted(host_names):
|
||||
for h_name in host_names:
|
||||
host = data.get_host(h_name)
|
||||
if not host:
|
||||
continue
|
||||
@@ -29,7 +29,7 @@ class IniExporter(BaseExporter):
|
||||
lines.append("")
|
||||
|
||||
# Ungrouped hosts
|
||||
ungrouped = [h for name, h in sorted(data.hosts.items()) if name not in grouped_hosts]
|
||||
ungrouped = [h for name, h in data.hosts.items() if name not in grouped_hosts]
|
||||
if ungrouped:
|
||||
lines.append("[ungrouped]")
|
||||
for host in ungrouped:
|
||||
|
||||
@@ -8,7 +8,7 @@ class LinuxConnectorExporter(BaseExporter):
|
||||
|
||||
def _build_structure(self, data: InventoryData) -> Dict[str, Any]:
|
||||
hosts_list: List[Dict[str, Any]] = []
|
||||
for name, host in sorted(data.hosts.items()):
|
||||
for name, host in data.hosts.items():
|
||||
h_dict: Dict[str, Any] = {
|
||||
"name": host.name,
|
||||
"hostname": host.ansible_host,
|
||||
@@ -25,7 +25,7 @@ class LinuxConnectorExporter(BaseExporter):
|
||||
|
||||
groups_dict: Dict[str, List[str]] = {}
|
||||
for grp_name, members in sorted(data.groups.items()):
|
||||
groups_dict[grp_name] = sorted(members)
|
||||
groups_dict[grp_name] = list(members)
|
||||
|
||||
return {
|
||||
"hosts": hosts_list,
|
||||
|
||||
@@ -12,7 +12,7 @@ class XmlExporter(BaseExporter):
|
||||
root.set("generated_at", datetime.now(timezone.utc).isoformat())
|
||||
|
||||
hosts_elem = ET.SubElement(root, "hosts")
|
||||
for name, host in sorted(data.hosts.items()):
|
||||
for name, host in data.hosts.items():
|
||||
h_elem = ET.SubElement(hosts_elem, "host")
|
||||
h_elem.set("name", host.name)
|
||||
h_elem.set("ip", host.ansible_host)
|
||||
@@ -43,7 +43,7 @@ class XmlExporter(BaseExporter):
|
||||
for grp_name, members in sorted(data.groups.items()):
|
||||
g_elem = ET.SubElement(groups_elem, "group")
|
||||
g_elem.set("name", grp_name)
|
||||
for m in sorted(members):
|
||||
for m in members:
|
||||
m_elem = ET.SubElement(g_elem, "member")
|
||||
m_elem.text = m
|
||||
|
||||
|
||||
@@ -56,3 +56,29 @@ class InventoryData:
|
||||
continue
|
||||
filtered.add_host(host)
|
||||
return filtered
|
||||
|
||||
def sort(self, sort_by: str = "name", reverse: bool = False) -> "InventoryData":
|
||||
sorted_inv = InventoryData()
|
||||
|
||||
def ip_sort_key(ip: str):
|
||||
clean_ip = (ip or "").strip()
|
||||
if not clean_ip:
|
||||
return (1, ())
|
||||
try:
|
||||
parts = tuple(int(p) for p in clean_ip.split("."))
|
||||
if len(parts) == 4:
|
||||
return (0, parts)
|
||||
except Exception:
|
||||
pass
|
||||
return (2, clean_ip)
|
||||
|
||||
def record_key(host: HostRecord):
|
||||
if sort_by.lower() == "ip":
|
||||
return (ip_sort_key(host.ansible_host), host.name.lower())
|
||||
return (host.name.lower(), ip_sort_key(host.ansible_host))
|
||||
|
||||
sorted_records = sorted(self.hosts.values(), key=record_key, reverse=reverse)
|
||||
for host in sorted_records:
|
||||
sorted_inv.add_host(host)
|
||||
|
||||
return sorted_inv
|
||||
Reference in new issue
Block a user