mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
refactor eos_facts to use network_cli (#20760)
refactors the eos_facts module to use the network_cli plugin
This commit is contained in:
parent
271c0c4574
commit
c860a8641c
1 changed files with 76 additions and 63 deletions
|
@ -16,9 +16,11 @@
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
ANSIBLE_METADATA = {'status': ['preview'],
|
ANSIBLE_METADATA = {
|
||||||
'supported_by': 'core',
|
'status': ['preview'],
|
||||||
'version': '1.0'}
|
'supported_by': 'core',
|
||||||
|
'version': '1.0'
|
||||||
|
}
|
||||||
|
|
||||||
DOCUMENTATION = """
|
DOCUMENTATION = """
|
||||||
---
|
---
|
||||||
|
@ -47,31 +49,19 @@ options:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
EXAMPLES = """
|
EXAMPLES = """
|
||||||
# Note: examples below use the following provider dict to handle
|
|
||||||
# transport and authentication to the node.
|
|
||||||
vars:
|
|
||||||
cli:
|
|
||||||
host: "{{ inventory_hostname }}"
|
|
||||||
username: admin
|
|
||||||
password: admin
|
|
||||||
transport: cli
|
|
||||||
|
|
||||||
# Collect all facts from the device
|
# Collect all facts from the device
|
||||||
- eos_facts:
|
- eos_facts:
|
||||||
gather_subset: all
|
gather_subset: all
|
||||||
provider: "{{ cli }}"
|
|
||||||
|
|
||||||
# Collect only the config and default facts
|
# Collect only the config and default facts
|
||||||
- eos_facts:
|
- eos_facts:
|
||||||
gather_subset:
|
gather_subset:
|
||||||
- config
|
- config
|
||||||
provider: "{{ cli }}"
|
|
||||||
|
|
||||||
# Do not collect hardware facts
|
# Do not collect hardware facts
|
||||||
- eos_facts:
|
- eos_facts:
|
||||||
gather_subset:
|
gather_subset:
|
||||||
- "!hardware"
|
- "!hardware"
|
||||||
provider: "{{ cli }}"
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
RETURN = """
|
RETURN = """
|
||||||
|
@ -145,30 +135,44 @@ ansible_net_neighbors:
|
||||||
"""
|
"""
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from ansible.module_utils.netcli import CommandRunner, AddCommandError
|
from functools import partial
|
||||||
|
|
||||||
|
from ansible.module_utils import eos
|
||||||
|
from ansible.module_utils import eapi
|
||||||
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
|
from ansible.module_utils.local import LocalAnsibleModule
|
||||||
from ansible.module_utils.six import iteritems
|
from ansible.module_utils.six import iteritems
|
||||||
from ansible.module_utils.eos import NetworkModule
|
|
||||||
|
SHARED_LIB = 'eos'
|
||||||
|
|
||||||
|
|
||||||
def add_command(runner, command, output=None):
|
def get_ansible_module():
|
||||||
try:
|
if SHARED_LIB == 'eos':
|
||||||
runner.add_command(command, output)
|
return LocalAnsibleModule
|
||||||
except AddCommandError:
|
return AnsibleModule
|
||||||
# AddCommandError is raised for any issue adding a command to
|
|
||||||
# the runner. Silently ignore the exception in this case
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
def invoke(name, *args, **kwargs):
|
||||||
|
obj = globals().get(SHARED_LIB)
|
||||||
|
func = getattr(obj, name)
|
||||||
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
run_commands = partial(invoke, 'run_commands')
|
||||||
|
|
||||||
|
def check_args(module, warnings):
|
||||||
|
if SHARED_LIB == 'eapi':
|
||||||
|
eapi.check_args(module)
|
||||||
|
|
||||||
class FactsBase(object):
|
class FactsBase(object):
|
||||||
|
|
||||||
def __init__(self, runner):
|
COMMANDS = frozenset()
|
||||||
self.runner = runner
|
|
||||||
|
def __init__(self, module):
|
||||||
|
self.module = module
|
||||||
self.facts = dict()
|
self.facts = dict()
|
||||||
|
self.responses = None
|
||||||
|
|
||||||
self.load_commands()
|
def populate(self):
|
||||||
|
self.responses = run_commands(self.module, list(self.COMMANDS))
|
||||||
def load_commands(self):
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
|
|
||||||
class Default(FactsBase):
|
class Default(FactsBase):
|
||||||
|
@ -179,22 +183,24 @@ class Default(FactsBase):
|
||||||
'modelName': 'model'
|
'modelName': 'model'
|
||||||
}
|
}
|
||||||
|
|
||||||
def load_commands(self):
|
COMMANDS = [
|
||||||
add_command(self.runner, 'show version', output='json')
|
'show version | json',
|
||||||
add_command(self.runner, 'show hostname', output='json')
|
'show hostname | json',
|
||||||
add_command(self.runner, 'bash timeout 5 cat /mnt/flash/boot-config')
|
'bash timeout 5 cat /mnt/flash/boot-config'
|
||||||
|
]
|
||||||
|
|
||||||
def populate(self):
|
def populate(self):
|
||||||
data = self.runner.get_command('show version', 'json')
|
super(Default, self).populate()
|
||||||
|
data = self.responses[0]
|
||||||
for key, value in iteritems(self.SYSTEM_MAP):
|
for key, value in iteritems(self.SYSTEM_MAP):
|
||||||
if key in data:
|
if key in data:
|
||||||
self.facts[value] = data[key]
|
self.facts[value] = data[key]
|
||||||
|
|
||||||
self.facts.update(self.runner.get_command('show hostname', 'json'))
|
self.facts.update(self.responses[1])
|
||||||
self.facts.update(self.parse_image())
|
self.facts.update(self.parse_image())
|
||||||
|
|
||||||
def parse_image(self):
|
def parse_image(self):
|
||||||
data = self.runner.get_command('bash timeout 5 cat /mnt/flash/boot-config')
|
data = self.responses[2]
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
data = data['messages'][0]
|
data = data['messages'][0]
|
||||||
match = re.search(r'SWI=(.+)$', data, re.M)
|
match = re.search(r'SWI=(.+)$', data, re.M)
|
||||||
|
@ -206,21 +212,23 @@ class Default(FactsBase):
|
||||||
|
|
||||||
class Hardware(FactsBase):
|
class Hardware(FactsBase):
|
||||||
|
|
||||||
def load_commands(self):
|
COMMANDS = [
|
||||||
add_command(self.runner, 'dir all-filesystems', output='text')
|
'dir all-filesystems',
|
||||||
add_command(self.runner, 'show version', output='json')
|
'show version | json'
|
||||||
|
]
|
||||||
|
|
||||||
def populate(self):
|
def populate(self):
|
||||||
|
super(Hardware, self).populate()
|
||||||
self.facts.update(self.populate_filesystems())
|
self.facts.update(self.populate_filesystems())
|
||||||
self.facts.update(self.populate_memory())
|
self.facts.update(self.populate_memory())
|
||||||
|
|
||||||
def populate_filesystems(self):
|
def populate_filesystems(self):
|
||||||
data = self.runner.get_command('dir all-filesystems', 'text')
|
data = self.responses[0]
|
||||||
fs = re.findall(r'^Directory of (.+)/', data, re.M)
|
fs = re.findall(r'^Directory of (.+)/', data, re.M)
|
||||||
return dict(filesystems=fs)
|
return dict(filesystems=fs)
|
||||||
|
|
||||||
def populate_memory(self):
|
def populate_memory(self):
|
||||||
values = self.runner.get_command('show version', 'json')
|
values = self.responses[1]
|
||||||
return dict(
|
return dict(
|
||||||
memfree_mb=int(values['memFree']) / 1024,
|
memfree_mb=int(values['memFree']) / 1024,
|
||||||
memtotal_mb=int(values['memTotal']) / 1024
|
memtotal_mb=int(values['memTotal']) / 1024
|
||||||
|
@ -228,11 +236,11 @@ class Hardware(FactsBase):
|
||||||
|
|
||||||
class Config(FactsBase):
|
class Config(FactsBase):
|
||||||
|
|
||||||
def load_commands(self):
|
COMMANDS = ['show running-config']
|
||||||
add_command(self.runner, 'show running-config', output='text')
|
|
||||||
|
|
||||||
def populate(self):
|
def populate(self):
|
||||||
self.facts['config'] = self.runner.get_command('show running-config')
|
super(Config, self).populate()
|
||||||
|
self.facts['config'] = self.responses[0]
|
||||||
|
|
||||||
|
|
||||||
class Interfaces(FactsBase):
|
class Interfaces(FactsBase):
|
||||||
|
@ -248,18 +256,21 @@ class Interfaces(FactsBase):
|
||||||
'forwardingModel': 'type'
|
'forwardingModel': 'type'
|
||||||
}
|
}
|
||||||
|
|
||||||
def load_commands(self):
|
COMMANDS = [
|
||||||
add_command(self.runner, 'show interfaces', output='json')
|
'show interfaces | json',
|
||||||
add_command(self.runner, 'show lldp neighbors', output='json')
|
'show lldp neighbors | json'
|
||||||
|
]
|
||||||
|
|
||||||
def populate(self):
|
def populate(self):
|
||||||
|
super(Interfaces, self).populate()
|
||||||
|
|
||||||
self.facts['all_ipv4_addresses'] = list()
|
self.facts['all_ipv4_addresses'] = list()
|
||||||
self.facts['all_ipv6_addresses'] = list()
|
self.facts['all_ipv6_addresses'] = list()
|
||||||
|
|
||||||
data = self.runner.get_command('show interfaces', 'json')
|
data = self.responses[0]
|
||||||
self.facts['interfaces'] = self.populate_interfaces(data)
|
self.facts['interfaces'] = self.populate_interfaces(data)
|
||||||
|
|
||||||
data = self.runner.get_command('show lldp neighbors', 'json')
|
data = self.responses[1]
|
||||||
self.facts['neighbors'] = self.populate_neighbors(data['lldpNeighbors'])
|
self.facts['neighbors'] = self.populate_neighbors(data['lldpNeighbors'])
|
||||||
|
|
||||||
def populate_interfaces(self, data):
|
def populate_interfaces(self, data):
|
||||||
|
@ -318,11 +329,19 @@ FACT_SUBSETS = dict(
|
||||||
VALID_SUBSETS = frozenset(FACT_SUBSETS.keys())
|
VALID_SUBSETS = frozenset(FACT_SUBSETS.keys())
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
spec = dict(
|
"""main entry point for module execution
|
||||||
|
"""
|
||||||
|
argument_spec = dict(
|
||||||
gather_subset=dict(default=['!config'], type='list')
|
gather_subset=dict(default=['!config'], type='list')
|
||||||
)
|
)
|
||||||
|
|
||||||
module = NetworkModule(argument_spec=spec, supports_check_mode=True)
|
argument_spec.update(eapi.eapi_argument_spec)
|
||||||
|
|
||||||
|
cls = get_ansible_module()
|
||||||
|
module = cls(argument_spec=argument_spec, supports_check_mode=True)
|
||||||
|
|
||||||
|
warnings = list()
|
||||||
|
check_args(module, warnings)
|
||||||
|
|
||||||
gather_subset = module.params['gather_subset']
|
gather_subset = module.params['gather_subset']
|
||||||
|
|
||||||
|
@ -361,20 +380,13 @@ def main():
|
||||||
facts = dict()
|
facts = dict()
|
||||||
facts['gather_subset'] = list(runable_subsets)
|
facts['gather_subset'] = list(runable_subsets)
|
||||||
|
|
||||||
runner = CommandRunner(module)
|
|
||||||
|
|
||||||
instances = list()
|
instances = list()
|
||||||
for key in runable_subsets:
|
for key in runable_subsets:
|
||||||
instances.append(FACT_SUBSETS[key](runner))
|
instances.append(FACT_SUBSETS[key](module))
|
||||||
|
|
||||||
runner.run()
|
for inst in instances:
|
||||||
|
inst.populate()
|
||||||
try:
|
facts.update(inst.facts)
|
||||||
for inst in instances:
|
|
||||||
inst.populate()
|
|
||||||
facts.update(inst.facts)
|
|
||||||
except Exception:
|
|
||||||
module.exit_json(out=module.from_json(runner.items))
|
|
||||||
|
|
||||||
ansible_facts = dict()
|
ansible_facts = dict()
|
||||||
for key, value in iteritems(facts):
|
for key, value in iteritems(facts):
|
||||||
|
@ -385,4 +397,5 @@ def main():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
SHARED_LIB = 'eapi'
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue