2014-09-26 01:01:01 +00:00
|
|
|
#!/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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-03-14 16:07:22 +00:00
|
|
|
ANSIBLE_METADATA = {'metadata_version': '1.0',
|
|
|
|
'status': ['preview'],
|
|
|
|
'supported_by': 'community'}
|
|
|
|
|
2016-12-06 10:35:25 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: lldp
|
2015-08-10 20:34:41 +00:00
|
|
|
requirements: [ lldpctl ]
|
2014-09-26 01:01:01 +00:00
|
|
|
version_added: 1.6
|
|
|
|
short_description: get details reported by lldp
|
|
|
|
description:
|
|
|
|
- Reads data out of lldpctl
|
|
|
|
options: {}
|
2016-12-08 05:34:16 +00:00
|
|
|
author: "Andy Hill (@andyhky)"
|
2014-09-26 01:01:01 +00:00
|
|
|
notes:
|
2017-01-27 23:20:31 +00:00
|
|
|
- Requires lldpd running and lldp enabled on switches
|
2014-09-26 01:01:01 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Retrieve switch/port information
|
|
|
|
- name: Gather information from lldp
|
|
|
|
lldp:
|
2017-01-27 23:45:23 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
- name: Print each switch/port
|
2016-12-01 11:17:32 +00:00
|
|
|
debug:
|
|
|
|
msg: "{{ lldp[item]['chassis']['name'] }} / {{ lldp[item]['port']['ifalias'] }}"
|
2016-11-10 17:12:03 +00:00
|
|
|
with_items: "{{ lldp.keys() }}"
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
# TASK: [Print each switch/port] ***********************************************************
|
|
|
|
# ok: [10.13.0.22] => (item=eth2) => {"item": "eth2", "msg": "switch1.example.com / Gi0/24"}
|
|
|
|
# ok: [10.13.0.22] => (item=eth1) => {"item": "eth1", "msg": "switch2.example.com / Gi0/3"}
|
|
|
|
# ok: [10.13.0.22] => (item=eth0) => {"item": "eth0", "msg": "switch3.example.com / Gi0/3"}
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
2017-02-02 19:45:22 +00:00
|
|
|
|
2017-06-22 12:22:34 +00:00
|
|
|
def gather_lldp(module):
|
2014-09-26 01:01:01 +00:00
|
|
|
cmd = ['lldpctl', '-f', 'keyvalue']
|
2017-06-22 12:22:34 +00:00
|
|
|
rc, output, err = module.run_command(cmd)
|
2014-09-26 01:01:01 +00:00
|
|
|
if output:
|
|
|
|
output_dict = {}
|
|
|
|
lldp_entries = output.split("\n")
|
|
|
|
|
|
|
|
for entry in lldp_entries:
|
2015-02-27 17:19:44 +00:00
|
|
|
if entry.startswith('lldp'):
|
2014-09-26 01:01:01 +00:00
|
|
|
path, value = entry.strip().split("=", 1)
|
|
|
|
path = path.split(".")
|
|
|
|
path_components, final = path[:-1], path[-1]
|
2015-04-08 16:33:40 +00:00
|
|
|
else:
|
|
|
|
value = current_dict[final] + '\n' + entry
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
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
|
2017-01-27 23:45:23 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule({})
|
|
|
|
|
2017-06-22 12:22:34 +00:00
|
|
|
lldp_output = gather_lldp(module)
|
2014-09-26 01:01:01 +00:00
|
|
|
try:
|
|
|
|
data = {'lldp': lldp_output['lldp']}
|
|
|
|
module.exit_json(ansible_facts=data)
|
|
|
|
except TypeError:
|
2017-01-27 23:20:31 +00:00
|
|
|
module.fail_json(msg="lldpctl command failed. is lldpd running?")
|
2017-01-27 23:45:23 +00:00
|
|
|
|
2014-09-26 01:01:01 +00:00
|
|
|
# import module snippets
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
|
2016-12-05 16:22:51 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|