From 8ca70ec4872d7c148b6c7419394c1a3f7298a4c5 Mon Sep 17 00:00:00 2001 From: Andy Hill Date: Fri, 13 Dec 2013 18:44:54 -0600 Subject: [PATCH] lldp support in Ansible This PR introduces support for a lldp module. lldpd is similar to CDP and can return useful details about a server's network like ports, switches, and VLANs. --- library/net_infrastructure/lldp | 67 +++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 library/net_infrastructure/lldp diff --git a/library/net_infrastructure/lldp b/library/net_infrastructure/lldp new file mode 100755 index 0000000000..eb027231d8 --- /dev/null +++ b/library/net_infrastructure/lldp @@ -0,0 +1,67 @@ +#!/usr/bin/python -tt +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +import subprocess + +DOCUMENTATION = ''' +--- +module: lldp +short_description: get details reported by lldp +description: + - Reads data out of lldp + +author: Andy Hill +notes: + - Requires lldpd running and lldp enabled on switches +''' + +def gather_lldp(): + cmd = ['lldpctl', '-f', 'keyvalue'] + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) + (output, err) = proc.communicate() + if output: + output_dict = {} + lldp_entries = output.split("\n") + + for entry in lldp_entries: + if entry: + path, value = entry.strip().split("=", 1) + path = path.split(".") + path_components, final = path[:-1], path[-1] + + current_dict = output_dict + for path_component in path_components: + current_dict[path_component] = current_dict.get(path_component, {}) + current_dict = current_dict[path_component] + current_dict[final] = value + return output_dict + + +def main(): + module = AnsibleModule({}) + + lldp_output = gather_lldp() + try: + data = {'lldp': lldp_output['lldp']} + module.exit_json(ansible_facts=data) + except TypeError: + module.fail_json(msg="lldpctl command failed. is lldpd running?") + +# this is magic, see lib/ansible/module_common.py +#<> + +main() +