inventory.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import json
  4. import shade
  5. if __name__ == '__main__':
  6. cloud = shade.openstack_cloud()
  7. inventory = {}
  8. # TODO(shadower): filter the servers based on the `OPENSHIFT_CLUSTER`
  9. # environment variable.
  10. cluster_hosts = [
  11. server for server in cloud.list_servers()
  12. if 'metadata' in server and 'clusterid' in server.metadata]
  13. masters = [server.name for server in cluster_hosts
  14. if server.metadata['host-type'] == 'master']
  15. etcd = [server.name for server in cluster_hosts
  16. if server.metadata['host-type'] == 'etcd']
  17. if not etcd:
  18. etcd = masters
  19. infra_hosts = [server.name for server in cluster_hosts
  20. if server.metadata['host-type'] == 'node' and
  21. server.metadata['sub-host-type'] == 'infra']
  22. app = [server.name for server in cluster_hosts
  23. if server.metadata['host-type'] == 'node' and
  24. server.metadata['sub-host-type'] == 'app']
  25. nodes = list(set(masters + infra_hosts + app))
  26. dns = [server.name for server in cluster_hosts
  27. if server.metadata['host-type'] == 'dns']
  28. lb = [server.name for server in cluster_hosts
  29. if server.metadata['host-type'] == 'lb']
  30. osev3 = list(set(nodes + etcd + lb))
  31. groups = [server.metadata.group for server in cluster_hosts
  32. if 'group' in server.metadata]
  33. inventory['cluster_hosts'] = {'hosts': [s.name for s in cluster_hosts]}
  34. inventory['OSEv3'] = {'hosts': osev3}
  35. inventory['masters'] = {'hosts': masters}
  36. inventory['etcd'] = {'hosts': etcd}
  37. inventory['nodes'] = {'hosts': nodes}
  38. inventory['infra_hosts'] = {'hosts': infra_hosts}
  39. inventory['app'] = {'hosts': app}
  40. inventory['dns'] = {'hosts': dns}
  41. inventory['lb'] = {'hosts': lb}
  42. for server in cluster_hosts:
  43. if 'group' in server.metadata:
  44. group = server.metadata.group
  45. if group not in inventory:
  46. inventory[group] = {'hosts': []}
  47. inventory[group]['hosts'].append(server.name)
  48. inventory['_meta'] = {'hostvars': {}}
  49. for server in cluster_hosts:
  50. ssh_ip_address = server.public_v4 or server.private_v4
  51. vars = {
  52. 'ansible_host': ssh_ip_address
  53. }
  54. public_v4 = server.public_v4 or server.private_v4
  55. if public_v4:
  56. vars['public_v4'] = public_v4
  57. # TODO(shadower): what about multiple networks?
  58. if server.private_v4:
  59. vars['private_v4'] = server.private_v4
  60. node_labels = server.metadata.get('node_labels')
  61. if node_labels:
  62. vars['openshift_node_labels'] = node_labels
  63. inventory['_meta']['hostvars'][server.name] = vars
  64. print(json.dumps(inventory, indent=4, sort_keys=True))