diff --git a/net_infrastructure/lldp b/net_infrastructure/lldp new file mode 100755 index 00000000000..eb027231d84 --- /dev/null +++ b/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() +