From 51e017647815e10f61afcb0ac60985b4eeff24ca Mon Sep 17 00:00:00 2001 From: Tomas Sedovic Date: Mon, 2 Oct 2017 09:49:32 +0200 Subject: Add dynamic inventory This adds an `inventory.py` script to the `sample-inventory` that lists all the necessary servers and groups dynamically, skipping the `static_inventory` role as well as the `hosts` creation. It also adds an `os_cinder` lookup function which is necessary for a seamless Cinder OpenShift registry integration without a static inventory. --- playbooks/provisioning/openstack/README.md | 13 ++++ .../openstack/sample-inventory/ansible.cfg | 3 + .../sample-inventory/group_vars/OSEv3.yml | 9 ++- .../openstack/sample-inventory/inventory.py | 89 ++++++++++++++++++++++ 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100755 playbooks/provisioning/openstack/sample-inventory/inventory.py (limited to 'playbooks') diff --git a/playbooks/provisioning/openstack/README.md b/playbooks/provisioning/openstack/README.md index 5e45add51..b96c9c9db 100644 --- a/playbooks/provisioning/openstack/README.md +++ b/playbooks/provisioning/openstack/README.md @@ -361,6 +361,19 @@ registry. Again in `OSEv3.yml`: The filesystem value here will be used in the initial formatting of the volume. +If you're using the dynamic inventory, you must uncomment these two values as +well: + + #openshift_hosted_registry_storage_openstack_volumeID: "{{ lookup('os_cinder', cinder_hosted_registry_name).id }}" + #openshift_hosted_registry_storage_volume_size: "{{ cinder_hosted_registry_size_gb }}Gi" + +But note that they use the `os_cinder` lookup plugin we provide, so you must +tell Ansible where to find it either in `ansible.cfg` (the one we provide is +configured properly) or by exporting the +`ANSIBLE_LOOKUP_PLUGINS=openshift-ansible-contrib/lookup_plugins` environment +variable. + + ### Use an existing Cinder volume for the OpenShift registry diff --git a/playbooks/provisioning/openstack/sample-inventory/ansible.cfg b/playbooks/provisioning/openstack/sample-inventory/ansible.cfg index 81d8ae10c..a21f023ea 100644 --- a/playbooks/provisioning/openstack/sample-inventory/ansible.cfg +++ b/playbooks/provisioning/openstack/sample-inventory/ansible.cfg @@ -1,6 +1,7 @@ # config file for ansible -- http://ansible.com/ # ============================================== [defaults] +ansible_user = openshift forks = 50 # work around privilege escalation timeouts in ansible timeout = 30 @@ -14,6 +15,8 @@ fact_caching_connection = .ansible/cached_facts fact_caching_timeout = 900 stdout_callback = skippy callback_whitelist = profile_tasks +lookup_plugins = openshift-ansible-contrib/lookup_plugins + [ssh_connection] ssh_args = -o ControlMaster=auto -o ControlPersist=900s -o GSSAPIAuthentication=no diff --git a/playbooks/provisioning/openstack/sample-inventory/group_vars/OSEv3.yml b/playbooks/provisioning/openstack/sample-inventory/group_vars/OSEv3.yml index 7d7683c62..2e897102e 100644 --- a/playbooks/provisioning/openstack/sample-inventory/group_vars/OSEv3.yml +++ b/playbooks/provisioning/openstack/sample-inventory/group_vars/OSEv3.yml @@ -27,9 +27,14 @@ openshift_hosted_registry_wait: True #openshift_hosted_registry_storage_access_modes: ['ReadWriteOnce'] #openshift_hosted_registry_storage_openstack_filesystem: xfs -## Configure this if you're attaching a Cinder volume you've set up. +## NOTE(shadower): This won't work until the openshift-ansible issue #5657 is fixed: +## https://github.com/openshift/openshift-ansible/issues/5657 ## If you're using the `cinder_hosted_registry_name` option from -## `all.yml`, this will be configured automaticaly. +## `all.yml`, uncomment these lines: +#openshift_hosted_registry_storage_openstack_volumeID: "{{ lookup('os_cinder', cinder_hosted_registry_name).id }}" +#openshift_hosted_registry_storage_volume_size: "{{ cinder_hosted_registry_size_gb }}Gi" + +## If you're using a Cinder volume you've set up yourself, uncomment these lines: #openshift_hosted_registry_storage_openstack_volumeID: e0ba2d73-d2f9-4514-a3b2-a0ced507fa05 #openshift_hosted_registry_storage_volume_size: 10Gi diff --git a/playbooks/provisioning/openstack/sample-inventory/inventory.py b/playbooks/provisioning/openstack/sample-inventory/inventory.py new file mode 100755 index 000000000..0b128ee40 --- /dev/null +++ b/playbooks/provisioning/openstack/sample-inventory/inventory.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python + +from __future__ import print_function + +import json +import os +import sys + +import shade + + +if __name__ == '__main__': + cloud = shade.openstack_cloud() + + inventory = {} + + # TODO(shadower): filter the servers based on the `OPENSHIFT_CLUSTER` + # environment variable. + cluster_hosts = [ + server for server in cloud.list_servers() + if 'metadata' in server and 'clusterid' in server.metadata] + + masters = [server.name for server in cluster_hosts + if server.metadata['host-type'] == 'master'] + + etcd = [server.name for server in cluster_hosts + if server.metadata['host-type'] == 'etcd'] + if not etcd: + etcd = masters + + infra_hosts = [server.name for server in cluster_hosts + if server.metadata['host-type'] == 'node' and + server.metadata['sub-host-type'] == 'infra'] + + app = [server.name for server in cluster_hosts + if server.metadata['host-type'] == 'node' and + server.metadata['sub-host-type'] == 'app'] + + nodes = list(set(masters + infra_hosts + app)) + + dns = [server.name for server in cluster_hosts + if server.metadata['host-type'] == 'dns'] + + lb = [server.name for server in cluster_hosts + if server.metadata['host-type'] == 'lb'] + + osev3 = list(set(nodes + etcd + lb)) + + groups = [server.metadata.group for server in cluster_hosts + if 'group' in server.metadata] + + inventory['cluster_hosts'] = { 'hosts': [s.name for s in cluster_hosts] } + inventory['OSEv3'] = { 'hosts': osev3 } + inventory['masters'] = { 'hosts': masters } + inventory['etcd'] = { 'hosts': etcd } + inventory['nodes'] = { 'hosts': nodes } + inventory['infra_hosts'] = { 'hosts': infra_hosts } + inventory['app'] = { 'hosts': app } + inventory['dns'] = { 'hosts': dns } + inventory['lb'] = { 'hosts': lb } + + for server in cluster_hosts: + if 'group' in server.metadata: + group = server.metadata.group + if group not in inventory: + inventory[group] = {'hosts': []} + inventory[group]['hosts'].append(server.name) + + inventory['_meta'] = { 'hostvars': {} } + + for server in cluster_hosts: + ssh_ip_address = server.public_v4 or server.private_v4 + vars = { + 'ansible_host': ssh_ip_address + } + + if server.public_v4: + vars['public_v4'] = server.public_v4 + # TODO(shadower): what about multiple networks? + if server.private_v4: + vars['private_v4'] = server.private_v4 + + node_labels = server.metadata.get('node_labels') + if node_labels: + vars['openshift_node_labels'] = node_labels + + inventory['_meta']['hostvars'][server.name] = vars + + print(json.dumps(inventory, indent=4, sort_keys=True)) -- cgit v1.2.3 From 181b8f6c82fe7f135b563edb74a39a44d279e32e Mon Sep 17 00:00:00 2001 From: Tomas Sedovic Date: Thu, 5 Oct 2017 10:26:47 +0200 Subject: Fix flake8 errors --- .../openstack/sample-inventory/inventory.py | 30 ++++++++++------------ 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'playbooks') diff --git a/playbooks/provisioning/openstack/sample-inventory/inventory.py b/playbooks/provisioning/openstack/sample-inventory/inventory.py index 0b128ee40..4949deac2 100755 --- a/playbooks/provisioning/openstack/sample-inventory/inventory.py +++ b/playbooks/provisioning/openstack/sample-inventory/inventory.py @@ -3,8 +3,6 @@ from __future__ import print_function import json -import os -import sys import shade @@ -19,7 +17,7 @@ if __name__ == '__main__': cluster_hosts = [ server for server in cloud.list_servers() if 'metadata' in server and 'clusterid' in server.metadata] - + masters = [server.name for server in cluster_hosts if server.metadata['host-type'] == 'master'] @@ -30,11 +28,11 @@ if __name__ == '__main__': infra_hosts = [server.name for server in cluster_hosts if server.metadata['host-type'] == 'node' and - server.metadata['sub-host-type'] == 'infra'] + server.metadata['sub-host-type'] == 'infra'] app = [server.name for server in cluster_hosts if server.metadata['host-type'] == 'node' and - server.metadata['sub-host-type'] == 'app'] + server.metadata['sub-host-type'] == 'app'] nodes = list(set(masters + infra_hosts + app)) @@ -42,22 +40,22 @@ if __name__ == '__main__': if server.metadata['host-type'] == 'dns'] lb = [server.name for server in cluster_hosts - if server.metadata['host-type'] == 'lb'] + if server.metadata['host-type'] == 'lb'] osev3 = list(set(nodes + etcd + lb)) groups = [server.metadata.group for server in cluster_hosts if 'group' in server.metadata] - inventory['cluster_hosts'] = { 'hosts': [s.name for s in cluster_hosts] } - inventory['OSEv3'] = { 'hosts': osev3 } - inventory['masters'] = { 'hosts': masters } - inventory['etcd'] = { 'hosts': etcd } - inventory['nodes'] = { 'hosts': nodes } - inventory['infra_hosts'] = { 'hosts': infra_hosts } - inventory['app'] = { 'hosts': app } - inventory['dns'] = { 'hosts': dns } - inventory['lb'] = { 'hosts': lb } + inventory['cluster_hosts'] = {'hosts': [s.name for s in cluster_hosts]} + inventory['OSEv3'] = {'hosts': osev3} + inventory['masters'] = {'hosts': masters} + inventory['etcd'] = {'hosts': etcd} + inventory['nodes'] = {'hosts': nodes} + inventory['infra_hosts'] = {'hosts': infra_hosts} + inventory['app'] = {'hosts': app} + inventory['dns'] = {'hosts': dns} + inventory['lb'] = {'hosts': lb} for server in cluster_hosts: if 'group' in server.metadata: @@ -66,7 +64,7 @@ if __name__ == '__main__': inventory[group] = {'hosts': []} inventory[group]['hosts'].append(server.name) - inventory['_meta'] = { 'hostvars': {} } + inventory['_meta'] = {'hostvars': {}} for server in cluster_hosts: ssh_ip_address = server.public_v4 or server.private_v4 -- cgit v1.2.3 From 3fb3db798d7f3d890f063315c8174e7252b9c054 Mon Sep 17 00:00:00 2001 From: Tomas Sedovic Date: Thu, 5 Oct 2017 12:36:03 +0200 Subject: Set public_v4 to private_v4 if it doesn't exist The DNS code expects a `public_v4` even when we use the provider networks. Let's just always export it. --- playbooks/provisioning/openstack/sample-inventory/inventory.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'playbooks') diff --git a/playbooks/provisioning/openstack/sample-inventory/inventory.py b/playbooks/provisioning/openstack/sample-inventory/inventory.py index 4949deac2..6a1b74b3d 100755 --- a/playbooks/provisioning/openstack/sample-inventory/inventory.py +++ b/playbooks/provisioning/openstack/sample-inventory/inventory.py @@ -72,8 +72,9 @@ if __name__ == '__main__': 'ansible_host': ssh_ip_address } - if server.public_v4: - vars['public_v4'] = server.public_v4 + public_v4 = server.public_v4 or server.private_v4 + if public_v4: + vars['public_v4'] = public_v4 # TODO(shadower): what about multiple networks? if server.private_v4: vars['private_v4'] = server.private_v4 -- cgit v1.2.3