From 5832e818a0084cd0e543dd4a43ed5cba32701401 Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 12 Oct 2024 12:30:11 +0200 Subject: [PATCH] Add script to list distribution versions --- personal_infra/inventory_os.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100755 personal_infra/inventory_os.py diff --git a/personal_infra/inventory_os.py b/personal_infra/inventory_os.py new file mode 100755 index 0000000..fa759a2 --- /dev/null +++ b/personal_infra/inventory_os.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 +import json +import subprocess + + +def parse_ansible(stdout): + result = {} + errors = set() + for line in stdout.splitlines(): + host_name, _ = line.split(" | ", 2) + if " => " not in line: + errors.add(host_name) + continue + _, facts_json = line.split(" => ", 2) + facts = json.loads(facts_json) + result[host_name] = facts + return result, errors + + +d = subprocess.run(["rye", "run", "ansible","-o", "-m", "setup", "-a", "filter=ansible_distribution", "!k8s"], stdout=subprocess.PIPE, encoding="utf8") + +d_data, d_errors = parse_ansible(d.stdout) + +v = subprocess.run(["rye", "run", "ansible", "-o", "-m", "setup", "-a", "filter=ansible_distribution_major_version", "!k8s"], stdout=subprocess.PIPE, encoding="utf8") + +v_data, v_errors = parse_ansible(v.stdout) + +for host_name in d_data.keys(): + print(host_name, d_data[host_name]["ansible_facts"]["ansible_distribution"], v_data[host_name]["ansible_facts"]["ansible_distribution_major_version"]) + +assert not d_errors and not v_errors, (v_errors, d_errors) -- 2.47.3