From b427499e3e52c1525f9bc9aaea21bfd1ed962fa8 Mon Sep 17 00:00:00 2001 From: tstoner <33665760+tstoner@users.noreply.github.com> Date: Wed, 26 Sep 2018 06:27:09 -0400 Subject: [PATCH] Added feature facts to nxos_facts (#45934) * Added feature facts to nxos_facts * Fixed ansibot indentation * Resolved ansibot whitespace missing after ',' * Per PR suggestion, created method in base class to gather switch config and store in global variable to prevent multiple calls to switch. * Addressed ansibot blank line & whitespace after , --- .../modules/network/nxos/nxos_facts.py | 27 ++++++++++++++++++- .../nxos_facts/tests/common/sanity.yaml | 17 ++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/ansible/modules/network/nxos/nxos_facts.py b/lib/ansible/modules/network/nxos/nxos_facts.py index a4d41b3a2d..01fe850928 100644 --- a/lib/ansible/modules/network/nxos/nxos_facts.py +++ b/lib/ansible/modules/network/nxos/nxos_facts.py @@ -178,6 +178,9 @@ from ansible.module_utils.connection import ConnectionError from ansible.module_utils.six import string_types, iteritems +g_config = None + + class FactsBase(object): def __init__(self, module): @@ -201,6 +204,12 @@ class FactsBase(object): self.warnings.append('command %s failed, facts for this command will not be populated' % command_string) return None + def get_config(self): + global g_config + if not g_config: + g_config = get_config(self.module) + return g_config + def transform_dict(self, data, keymap): transform = dict() for key, fact in keymap: @@ -287,7 +296,22 @@ class Config(FactsBase): def populate(self): super(Config, self).populate() - self.facts['config'] = get_config(self.module) + self.facts['config'] = self.get_config() + + +class Features(FactsBase): + + def populate(self): + super(Features, self).populate() + data = self.get_config() + + if data: + features = [] + for line in data.splitlines(): + if line.startswith('feature'): + features.append(line.replace('feature', '').strip()) + + self.facts['features_enabled'] = features class Hardware(FactsBase): @@ -903,6 +927,7 @@ FACT_SUBSETS = dict( hardware=Hardware, interfaces=Interfaces, config=Config, + features=Features ) VALID_SUBSETS = frozenset(FACT_SUBSETS.keys()) diff --git a/test/integration/targets/nxos_facts/tests/common/sanity.yaml b/test/integration/targets/nxos_facts/tests/common/sanity.yaml index 6b9e789ff9..d895b85ebd 100644 --- a/test/integration/targets/nxos_facts/tests/common/sanity.yaml +++ b/test/integration/targets/nxos_facts/tests/common/sanity.yaml @@ -77,4 +77,21 @@ - "result.ansible_facts.ansible_net_memfree_mb > 1" - "result.ansible_facts.ansible_net_memtotal_mb > 1" +- name: "nxos_facts gather features facts" + nxos_facts: + gather_subset: features + provider: "{{ connection }}" + register: result + +- assert: + that: + # _facts modules should never report a change + - "result.changed == false" + + # Correct subsets are present + - "'features' in result.ansible_facts.ansible_net_gather_subset" + + # Items from the subset is present + - "result.ansible_facts.ansible_net_features_enabled is defined" + - debug: msg="END connection={{ ansible_connection }} nxos_facts sanity test"