From 26ab4c034b54c82f2f711d4aa37c43d61a581479 Mon Sep 17 00:00:00 2001 From: Bruno Goncalves Date: Jun 11 2018 09:41:57 +0000 Subject: fix standard-inventory-rpm Solve dynamic inventory output: ANSIBLE_INVENTORY=$(test -e inventory && echo inventory || echo /usr/share/ansible/inventory) TEST_SUBJECTS=local ansible-inventory --list [WARNING]: * Failed to parse /usr/share/ansible/inventory/standard-inventory-rpm with script plugin: Invalid data from file, expected dictionary and got: None [WARNING]: * Failed to parse /usr/share/ansible/inventory/standard-inventory-rpm with yaml plugin: Syntax Error while loading YAML. mapping values are not allowed in this context The error appears to have been in '/usr/share/ansible/inventory/standard-inventory-rpm': line 37, column 15, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: def main(argv): ^ here [WARNING]: * Failed to parse /usr/share/ansible/inventory/standard-inventory-rpm with ini plugin: /usr/share/ansible/inventory/standard-inventory-rpm:27: Expected key=value host variable assignment, got: argparse [WARNING]: Unable to parse /usr/share/ansible/inventory/standard-inventory-rpm as an inventory source six --- diff --git a/inventory/standard-inventory-rpm b/inventory/standard-inventory-rpm index 400eda8..739179a 100755 --- a/inventory/standard-inventory-rpm +++ b/inventory/standard-inventory-rpm @@ -38,7 +38,7 @@ def main(argv): parser = argparse.ArgumentParser(description="Inventory for local RPM installed host") parser.add_argument("--list", action="store_true", help="Verbose output") parser.add_argument('--host', help="Get host variables") - parser.add_argument("subjects", nargs="*", default=shlex.split(os.environ.get("TEST_SUBJECTS", ""))) + parser.add_argument("subjects", nargs="*", default=os.environ.get("TEST_SUBJECTS", "")) opts = parser.parse_args() try: @@ -56,22 +56,13 @@ def main(argv): def getlist(subjects): - repos = [] - rpms = [] hosts = [] variables = {} - for subject in subjects: - if subject.endswith(".rpm"): - rpms.append(subject) - elif isrepo(subject): - repos.append(subject) - - if repos or rpms: - host_vars = gethost(repos, rpms) - if host_vars: - hosts.append("rpms") - variables["rpms"] = vars + host_vars = gethost(subjects) + if host_vars: + hosts.append("rpms") + variables["rpms"] = host_vars if not hosts: return EMPTY_INVENTORY @@ -80,7 +71,20 @@ def getlist(subjects): "_meta": {"hostvars": variables}} -def gethost(repos, rpms): +def gethost(subjects): + subjects = shlex.split(subjects) + repos = [] + rpms = [] + + for subject in subjects: + if subject.endswith(".rpm"): + rpms.append(subject) + elif isrepo(subject): + repos.append(subject) + + if not repos and not rpms: + return EMPTY_INVENTORY + # The variables variables = { "ansible_connection": "local"