From nobody Mon Feb 9 14:15:11 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1531324496449549.7245381090406; Wed, 11 Jul 2018 08:54:56 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A521E91509; Wed, 11 Jul 2018 15:54:53 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6ADB25B687; Wed, 11 Jul 2018 15:54:53 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 05D4418037EC; Wed, 11 Jul 2018 15:54:53 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6BFshua002240 for ; Wed, 11 Jul 2018 11:54:43 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6210D2026D76; Wed, 11 Jul 2018 15:54:43 +0000 (UTC) Received: from inaba.usersys.redhat.com (unknown [10.43.2.44]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 019D92026D6B for ; Wed, 11 Jul 2018 15:54:42 +0000 (UTC) From: Andrea Bolognani To: libvir-list@redhat.com Date: Wed, 11 Jul 2018 17:54:32 +0200 Message-Id: <20180711155436.22284-5-abologna@redhat.com> In-Reply-To: <20180711155436.22284-1-abologna@redhat.com> References: <20180711155436.22284-1-abologna@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Subject: [libvirt] [jenkins-ci PATCH 4/8] lcitool: Add inventory handling X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Wed, 11 Jul 2018 15:54:55 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" We use an actual YAML parser this time around, and bring the behavior more in line with what Ansible is doing, so interoperability should be more solid overall. New in this implementation is more flexibility in defining host lists, including support for explicit lists as well as glob patterns. Signed-off-by: Andrea Bolognani --- guests/lcitool | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/guests/lcitool b/guests/lcitool index 6d4010b..3564eb8 100755 --- a/guests/lcitool +++ b/guests/lcitool @@ -19,11 +19,13 @@ =20 import argparse import crypt +import fnmatch import os import random import string import sys import textwrap +import yaml =20 # This is necessary to maintain Python 2.7 compatibility try: @@ -139,10 +141,95 @@ class Config: =20 return root_hash_file =20 +class Inventory: + + def __init__(self): + try: + parser =3D configparser.SafeConfigParser() + parser.read("./ansible.cfg") + inventory_path =3D parser.get("defaults", "inventory") + except: + raise Error("Can't find inventory location in ansible.cfg") + + self._facts =3D {} + try: + # We can only deal with trivial inventories, but that's + # all we need right now and we can expand support further + # later on if necessary + with open(inventory_path, "r") as f: + for line in f: + host =3D line.strip() + self._facts[host] =3D {} + except: + raise Error( + "Missing or invalid inventory ({})".format( + inventory_path, + ) + ) + + for host in self._facts: + try: + self._facts[host] =3D self._read_all_facts(host) + self._facts[host]["inventory_hostname"] =3D host + except: + raise Error("Can't load facts for '{}'".format(host)) + + def _add_facts_from_file(self, facts, yaml_path): + with open(yaml_path, "r") as f: + some_facts =3D yaml.load(f) + for fact in some_facts: + facts[fact] =3D some_facts[fact] + + def _read_all_facts(self, host): + facts =3D {} + + # We load from group_vars/ first and host_vars/ second, sorting + # files alphabetically; doing so should result in our view of + # the facts matching Ansible's + for source in ["./group_vars/all/", "./host_vars/{}/".format(host)= ]: + for item in sorted(os.listdir(source)): + yaml_path =3D os.path.join(source, item) + if not os.path.isfile(yaml_path): + continue + if not yaml_path.endswith(".yml"): + continue + self._add_facts_from_file(facts, yaml_path) + + return facts + + def expand_pattern(self, pattern): + if pattern =3D=3D None: + raise Error("Missing host list") + + if pattern =3D=3D "all": + pattern =3D "*" + + # This works correctly for single hosts as well as more complex + # cases such as explicit lists, glob patterns and any combination + # of the above + matches =3D [] + for partial_pattern in pattern.split(","): + + partial_matches =3D [] + for host in self._facts: + if fnmatch.fnmatch(host, partial_pattern): + partial_matches +=3D [host] + + if len(partial_matches) =3D=3D 0: + raise Error("Invalid host list '{}'".format(pattern)) + + matches +=3D partial_matches + + return list(set(matches)) + + def get_facts(self, host): + return self._facts[host] + class Application: =20 def __init__(self): self._config =3D Config() + self._inventory =3D Inventory() =20 self._parser =3D argparse.ArgumentParser( conflict_handler =3D "resolve", --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list