| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Copyright Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: lldp_facts |
| 11 |
requirements: |
| 12 |
- C(lldpctl), usually provided by the C(lldpd) package. |
| 13 |
- The module was renamed from C(lldp) to C(lldp_facts) in community.general 13.0.0. |
| 14 |
The old name is available as an alias. Eventually a deprecation message will be shown |
| 15 |
when the old name is used. |
| 16 |
short_description: Get details reported by LLDP |
| 17 |
description: |
| 18 |
- Reads LLDP data from C(lldpd) using the CLI tool C(lldpctl). |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.general._attributes |
| 21 |
- community.general._attributes.facts |
| 22 |
- community.general._attributes.facts_module |
| 23 |
options: |
| 24 |
multivalues: |
| 25 |
description: If C(lldpctl) outputs an attribute multiple time represent all values as a list. |
| 26 |
type: bool |
| 27 |
default: false |
| 28 |
author: "Andy Hill (@andyhky)" |
| 29 |
notes: |
| 30 |
- Requires C(lldpd) running and LLDP enabled on switches. |
| 31 |
""" |
| 32 |
|
| 33 |
EXAMPLES = r""" |
| 34 |
# Retrieve switch/port information |
| 35 |
- name: Gather information from LLDP |
| 36 |
community.general.lldp: |
| 37 |
|
| 38 |
- name: Print each switch/port |
| 39 |
ansible.builtin.debug: |
| 40 |
msg: "{{ lldp[item]['chassis']['name'] }} / {{ lldp[item]['port']['ifname'] }}" |
| 41 |
with_items: "{{ lldp.keys() }}" |
| 42 |
|
| 43 |
# TASK: [Print each switch/port] *********************************************************** |
| 44 |
# ok: [10.13.0.22] => (item=eth2) => {"item": "eth2", "msg": "switch1.example.com / Gi0/24"} |
| 45 |
# ok: [10.13.0.22] => (item=eth1) => {"item": "eth1", "msg": "switch2.example.com / Gi0/3"} |
| 46 |
# ok: [10.13.0.22] => (item=eth0) => {"item": "eth0", "msg": "switch3.example.com / Gi0/3"} |
| 47 |
""" |
| 48 |
|
| 49 |
RESULTS = r""" |
| 50 |
ansible_facts: |
| 51 |
description: The returned facts. |
| 52 |
returned: success |
| 53 |
type: dict |
| 54 |
contains: |
| 55 |
lldp: |
| 56 |
descrition: |
| 57 |
- The parsed output. |
| 58 |
type: dict |
| 59 |
""" |
| 60 |
|
| 61 |
from ansible.module_utils.basic import AnsibleModule |
| 62 |
|
| 63 |
|
| 64 |
def gather_lldp(module): |
| 65 |
cmd = [module.get_bin_path("lldpctl"), "-f", "keyvalue"] |
| 66 |
rc, output, err = module.run_command(cmd) |
| 67 |
if output: |
| 68 |
output_dict = {} |
| 69 |
current_dict = {} |
| 70 |
lldp_entries = output.strip().split("\n") |
| 71 |
|
| 72 |
final = "" |
| 73 |
for entry in lldp_entries: |
| 74 |
if entry.startswith("lldp"): |
| 75 |
path, value = entry.strip().split("=", 1) |
| 76 |
path = path.split(".") |
| 77 |
path_components, final = path[:-1], path[-1] |
| 78 |
elif final in current_dict and isinstance(current_dict[final], str): |
| 79 |
current_dict[final] += f"\n{entry}" |
| 80 |
continue |
| 81 |
elif final in current_dict and isinstance(current_dict[final], list): |
| 82 |
current_dict[final][-1] += f"\n{entry}" |
| 83 |
continue |
| 84 |
else: |
| 85 |
continue |
| 86 |
|
| 87 |
current_dict = output_dict |
| 88 |
for path_component in path_components: |
| 89 |
current_dict[path_component] = current_dict.get(path_component, {}) |
| 90 |
if not isinstance(current_dict[path_component], dict): |
| 91 |
current_dict[path_component] = {"value": current_dict[path_component]} |
| 92 |
current_dict = current_dict[path_component] |
| 93 |
|
| 94 |
if final in current_dict and isinstance(current_dict[final], dict) and module.params["multivalues"]: |
| 95 |
current_dict = current_dict[final] |
| 96 |
final = "value" |
| 97 |
|
| 98 |
if final not in current_dict or not module.params["multivalues"]: |
| 99 |
current_dict[final] = value |
| 100 |
elif isinstance(current_dict[final], str): |
| 101 |
current_dict[final] = [current_dict[final], value] |
| 102 |
elif isinstance(current_dict[final], list): |
| 103 |
current_dict[final].append(value) |
| 104 |
|
| 105 |
return output_dict |
| 106 |
|
| 107 |
|
| 108 |
def main(): |
| 109 |
module_args = dict(multivalues=dict(type="bool", default=False)) |
| 110 |
module = AnsibleModule(module_args, supports_check_mode=True) |
| 111 |
module.run_command_environ_update = {"LANGUAGE": "C", "LC_ALL": "C"} |
| 112 |
|
| 113 |
lldp_output = gather_lldp(module) |
| 114 |
try: |
| 115 |
data = {"lldp": lldp_output["lldp"]} |
| 116 |
module.exit_json(ansible_facts=data) |
| 117 |
except TypeError: |
| 118 |
module.fail_json(msg="lldpctl command failed. is lldpd running?") |
| 119 |
|
| 120 |
|
| 121 |
if __name__ == "__main__": |
| 122 |
main() |