Skip to content

Commit

Permalink
Ansible Inventory - NornirNoValidInventoryError Exception (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlmontanari authored and dbarrosop committed Jan 14, 2020
1 parent e7e416b commit 3e65272
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 8 additions & 0 deletions nornir/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,13 @@ def __str__(self) -> str:
return "Subtask: {} (failed)\n".format(self.task)


class NornirNoValidInventoryError(Exception):
"""
Raised by nornir when :meth:`nornir.plugins.inventory.parse` fails to load any valid inventory
"""

pass


class ConflictingConfigurationWarning(UserWarning):
pass
14 changes: 12 additions & 2 deletions nornir/plugins/inventory/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
InventoryElement,
VarsDict,
)
from nornir.core.exceptions import NornirNoValidInventoryError

import ruamel.yaml
from ruamel.yaml.composer import ComposerError
from ruamel.yaml.scanner import ScannerError


VARS_FILENAME_EXTENSIONS = ["", ".yml", ".yaml"]
VARS_FILENAME_EXTENSIONS = ["", ".ini", ".yml", ".yaml"]


YAML = ruamel.yaml.YAML(typ="safe")
Expand Down Expand Up @@ -250,7 +251,9 @@ def parse(hostsfile: str) -> Tuple[HostsDict, GroupsDict, DefaultsDict]:
parser = YAMLParser(hostsfile)
except (ScannerError, ComposerError):
logger.error("AnsibleInventory: file %r is not INI or YAML file", hostsfile)
raise
raise NornirNoValidInventoryError(
f"AnsibleInventory: no valid inventory source(s) to parse. Tried: {hostsfile}"
)

parser.parse()

Expand All @@ -259,6 +262,13 @@ def parse(hostsfile: str) -> Tuple[HostsDict, GroupsDict, DefaultsDict]:

class AnsibleInventory(Inventory):
def __init__(self, hostsfile: str = "hosts", *args: Any, **kwargs: Any) -> None:
"""
Ansible Inventory plugin supporting ini, yaml, and dynamic inventory sources.
Arguments:
hostsfile: Path to valid Ansible inventory
"""
host_vars, group_vars, defaults = parse(hostsfile)
super().__init__(
hosts=host_vars, groups=group_vars, defaults=defaults, *args, **kwargs
Expand Down
4 changes: 2 additions & 2 deletions tests/plugins/inventory/test_ansible.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os

from nornir.plugins.inventory import ansible
from nornir.core.exceptions import NornirNoValidInventoryError

import pytest

import ruamel.yaml
from ruamel.yaml.scanner import ScannerError


BASE_PATH = os.path.join(os.path.dirname(__file__), "ansible")
Expand Down Expand Up @@ -57,5 +57,5 @@ def test_inventory(self, case):

def test_parse_error(self):
base_path = os.path.join(BASE_PATH, "parse_error")
with pytest.raises(ScannerError):
with pytest.raises(NornirNoValidInventoryError):
ansible.parse(hostsfile=os.path.join(base_path, "source", "hosts"))

0 comments on commit 3e65272

Please sign in to comment.