|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# Copyright: (c) 2025, StackHPC |
| 4 | +# Apache 2 License |
| 5 | + |
| 6 | +from ansible.module_utils.basic import AnsibleModule |
| 7 | +import openstack |
| 8 | + |
| 9 | +DOCUMENTATION = """ |
| 10 | +--- |
| 11 | +module: map_hosts |
| 12 | +short_description: Creates map of OpenStack VM network topology |
| 13 | +description: |
| 14 | + - Creates map representing the network topology tree of an OpenStack project with a heirarchy |
| 15 | + of: Availability Zone -> Hypervisors -> VMs/Baremetal instances |
| 16 | +options: |
| 17 | + compute_vms: |
| 18 | + description: |
| 19 | + - List of VM names within the target OpenStack project to include in the tree |
| 20 | + required: true |
| 21 | + type: str |
| 22 | +author: |
| 23 | + - Steve Brasier, William Tripp, StackHPC |
| 24 | +""" |
| 25 | + |
| 26 | +RETURN = """ |
| 27 | +topology: |
| 28 | + description: |
| 29 | + Map representing tree of project topology. Top level keys are AZ names, their values |
| 30 | + are maps of shortened unique identifiers of hosts UUIDs to lists of VM names |
| 31 | + returned: success |
| 32 | + type: dict[str, dict[str,list[str]]] |
| 33 | + sample: |
| 34 | + "nova-az": |
| 35 | + "afe9": |
| 36 | + - "mycluster-compute-0" |
| 37 | + - "mycluster-compute-1" |
| 38 | + "00f9": |
| 39 | + - "mycluster-compute-vm-on-other-hypervisor" |
| 40 | +""" |
| 41 | + |
| 42 | +EXAMPLES = """ |
| 43 | +- name: Get topology map |
| 44 | + map_hosts: |
| 45 | + compute_vms: |
| 46 | + - mycluster-compute-0 |
| 47 | + - mycluster-compute-1 |
| 48 | +""" |
| 49 | + |
| 50 | +def min_prefix(uuids, start=4): |
| 51 | + """ Take a list of uuids and return the smallest length >= start which keeps them unique """ |
| 52 | + for length in range(start, len(uuids[0])): |
| 53 | + prefixes = set(uuid[:length] for uuid in uuids) |
| 54 | + if len(prefixes) == len(uuids): |
| 55 | + return length |
| 56 | + |
| 57 | +def run_module(): |
| 58 | + module_args = dict( |
| 59 | + compute_vms=dict(type='list', elements='str', required=True) |
| 60 | + ) |
| 61 | + module = AnsibleModule(argument_spec=module_args, supports_check_mode=True) |
| 62 | + |
| 63 | + conn = openstack.connection.from_config() |
| 64 | + |
| 65 | + servers = [s for s in conn.compute.servers() if s["name"] in module.params["compute_vms"]] |
| 66 | + |
| 67 | + topo = {} |
| 68 | + all_host_ids = [] |
| 69 | + for s in servers: |
| 70 | + az = s['availability_zone'] |
| 71 | + host_id = s['host_id'] |
| 72 | + if host_id != '': # empty string if e.g. server is shelved |
| 73 | + all_host_ids.append(host_id) |
| 74 | + if az not in topo: |
| 75 | + topo[az] = {} |
| 76 | + if host_id not in topo[az]: |
| 77 | + topo[az][host_id] = [] |
| 78 | + topo[az][host_id].append(s['name']) |
| 79 | + |
| 80 | + uuid_len = min_prefix(list(set(all_host_ids))) |
| 81 | + |
| 82 | + for az in topo: |
| 83 | + topo[az] = dict((k[:uuid_len], v) for (k, v) in topo[az].items()) |
| 84 | + |
| 85 | + result = { |
| 86 | + "changed": False, |
| 87 | + "topology": topo, |
| 88 | + } |
| 89 | + |
| 90 | + module.exit_json(**result) |
| 91 | + |
| 92 | + |
| 93 | +def main(): |
| 94 | + run_module() |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + main() |
0 commit comments