inventory.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. """
  3. This is an Ansible dynamic inventory for OpenStack.
  4. It requires your OpenStack credentials to be set in clouds.yaml or your shell
  5. environment.
  6. """
  7. from __future__ import print_function
  8. import json
  9. import shade
  10. def build_inventory():
  11. '''Build the dynamic inventory.'''
  12. cloud = shade.openstack_cloud()
  13. inventory = {}
  14. # TODO(shadower): filter the servers based on the `OPENSHIFT_CLUSTER`
  15. # environment variable.
  16. cluster_hosts = [
  17. server for server in cloud.list_servers()
  18. if 'metadata' in server and 'clusterid' in server.metadata]
  19. masters = [server.name for server in cluster_hosts
  20. if server.metadata['host-type'] == 'master']
  21. etcd = [server.name for server in cluster_hosts
  22. if server.metadata['host-type'] == 'etcd']
  23. if not etcd:
  24. etcd = masters
  25. infra_hosts = [server.name for server in cluster_hosts
  26. if server.metadata['host-type'] == 'node' and
  27. server.metadata['sub-host-type'] == 'infra']
  28. app = [server.name for server in cluster_hosts
  29. if server.metadata['host-type'] == 'node' and
  30. server.metadata['sub-host-type'] == 'app']
  31. nodes = list(set(masters + infra_hosts + app))
  32. dns = [server.name for server in cluster_hosts
  33. if server.metadata['host-type'] == 'dns']
  34. load_balancers = [server.name for server in cluster_hosts
  35. if server.metadata['host-type'] == 'lb']
  36. osev3 = list(set(nodes + etcd + load_balancers))
  37. inventory['cluster_hosts'] = {'hosts': [s.name for s in cluster_hosts]}
  38. inventory['OSEv3'] = {'hosts': osev3}
  39. inventory['masters'] = {'hosts': masters}
  40. inventory['etcd'] = {'hosts': etcd}
  41. inventory['nodes'] = {'hosts': nodes}
  42. inventory['infra_hosts'] = {'hosts': infra_hosts}
  43. inventory['app'] = {'hosts': app}
  44. inventory['dns'] = {'hosts': dns}
  45. inventory['lb'] = {'hosts': load_balancers}
  46. for server in cluster_hosts:
  47. if 'group' in server.metadata:
  48. group = server.metadata.group
  49. if group not in inventory:
  50. inventory[group] = {'hosts': []}
  51. inventory[group]['hosts'].append(server.name)
  52. inventory['_meta'] = {'hostvars': {}}
  53. for server in cluster_hosts:
  54. ssh_ip_address = server.public_v4 or server.private_v4
  55. hostvars = {
  56. 'ansible_host': ssh_ip_address
  57. }
  58. public_v4 = server.public_v4 or server.private_v4
  59. if public_v4:
  60. hostvars['public_v4'] = server.public_v4
  61. hostvars['openshift_public_ip'] = server.public_v4
  62. # TODO(shadower): what about multiple networks?
  63. if server.private_v4:
  64. hostvars['private_v4'] = server.private_v4
  65. # NOTE(shadower): Yes, we set both hostname and IP to the private
  66. # IP address for each node. OpenStack doesn't resolve nodes by
  67. # name at all, so using a hostname here would require an internal
  68. # DNS which would complicate the setup and potentially introduce
  69. # performance issues.
  70. hostvars['openshift_ip'] = server.private_v4
  71. hostvars['openshift_hostname'] = server.private_v4
  72. hostvars['openshift_public_hostname'] = server.name
  73. node_labels = server.metadata.get('node_labels')
  74. if node_labels:
  75. hostvars['openshift_node_labels'] = node_labels
  76. inventory['_meta']['hostvars'][server.name] = hostvars
  77. return inventory
  78. if __name__ == '__main__':
  79. print(json.dumps(build_inventory(), indent=4, sort_keys=True))