Add lookup plugin for tripleo heat templates

This patch adds a lookup plugin for tripleo heat templates
that playbooks can use as: "{{ lookup('tht') }}"

The lookup will return a list of tuples, each tuple
containing the path and the contents of the file.

To implement this the auth token as well as the plan
name have been added to the inventory.

Change-Id: I8ab1563dc887e51339e669592011f2115cfe5e4c
This commit is contained in:
Florian Fuchs 2017-03-03 14:54:54 +00:00
parent d9d3fe4ffc
commit 9463eb3dfc
4 changed files with 117 additions and 20 deletions

View File

@ -0,0 +1,5 @@
---
features:
- |
Added a lookup plugin to access or loop over the current plan's template
files in a validation playbook.

View File

@ -26,13 +26,14 @@ import os
import sys
from heatclient import client as heat_client
from keystoneauth1.identity import generic as ks_id
from keystoneauth1 import session
import mistralclient.api.base
import mistralclient.api.client
from novaclient import client as nova_client
from oslo_config import cfg
from tripleo_validations.utils import get_auth_session
opts = [
cfg.StrOpt('host', help='List details about the specific host'),
cfg.BoolOpt('list', help='List active hosts'),
@ -70,7 +71,6 @@ class TripleoInventory(object):
def __init__(self, configs):
self.configs = configs
self._session = None
self._ksclient = None
self._hclient = None
self._mclient = None
self._nclient = None
@ -117,10 +117,17 @@ class TripleoInventory(object):
'hosts': ['localhost'],
'vars': {
'ansible_connection': 'local',
'os_auth_token': self.session.get_token(),
'plan': self.configs.plan,
},
}
}
swift_url = self.session.get_endpoint(service_type='object-store',
endpoint_type='publicURL')
if swift_url:
ret['undercloud']['vars']['undercloud_swift_url'] = swift_url
public_vip = self.get_overcloud_output('PublicVip')
if public_vip:
ret['undercloud']['vars']['overcloud_public_vip'] = public_vip
@ -131,12 +138,14 @@ class TripleoInventory(object):
passwords = overcloud_environment.get('passwords', {})
admin_password = passwords.get('AdminPassword', '')
if admin_password:
ret['undercloud']['vars']['overcloud_admin_password'] = admin_password
ret['undercloud']['vars']['overcloud_admin_password'] = \
admin_password
endpoint_map = self.get_overcloud_output('EndpointMap')
if endpoint_map:
horizon_endpoint = endpoint_map.get('HorizonPublic', {}).get('uri')
if horizon_endpoint:
ret['undercloud']['vars']['overcloud_horizon_url'] = horizon_endpoint
ret['undercloud']['vars']['overcloud_horizon_url'] = \
horizon_endpoint
controller_group = self.fetch_stack_resources('Controller')
if controller_group:
@ -167,21 +176,17 @@ class TripleoInventory(object):
@property
def session(self):
if self._session is None:
if self.configs.auth_token:
auth = ks_id.Token(auth_url=self.configs.auth_url,
token=self.configs.auth_token,
project_name=self.configs.project_name,
project_domain_id='default')
else:
auth = ks_id.Password(auth_url=self.configs.auth_url,
username=self.configs.username,
password=self.configs.password,
project_name=self.configs.project_name,
user_domain_id='default',
project_domain_id='default')
self._session = session.Session(auth=auth,
verify=self.configs.cacert)
try:
self._session = get_auth_session(self.configs.auth_url,
self.configs.username,
self.configs.project_name,
self.configs.password,
self.configs.auth_token,
self.configs.cacert)
except Exception as e:
print("Error connecting to Keystone: {}".format(e.message),
file=sys.stderr)
sys.exit(1)
return self._session
@property
@ -230,5 +235,6 @@ def main():
inventory.host()
sys.exit(0)
if __name__ == '__main__':
main()

View File

@ -0,0 +1,47 @@
#!/usr/bin/env python
# Copyright 2017 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from __future__ import print_function
from keystoneauth1.identity import generic as ks_id
from keystoneauth1 import session
from swiftclient.client import Connection
def get_auth_session(auth_url, username, project_name, password=None,
auth_token=None, cacert=None):
if auth_token:
auth = ks_id.Token(auth_url=auth_url,
token=auth_token,
project_name=project_name,
project_domain_id='default')
else:
auth = ks_id.Password(auth_url=auth_url,
username=username,
password=password,
project_name=project_name,
user_domain_id='default',
project_domain_id='default')
return session.Session(auth=auth, verify=cacert)
def get_swift_client(preauthurl, preauthtoken):
return Connection(preauthurl=preauthurl,
preauthtoken=preauthtoken,
retries=10,
starting_backoff=3,
max_backoff=120)

View File

@ -0,0 +1,39 @@
#!/usr/bin/env python
# Copyright 2017 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from ansible.plugins.lookup import LookupBase
from tripleo_validations.utils import get_swift_client
class LookupModule(LookupBase):
def run(self, terms, variables=None, **kwargs):
"""Returns the current plan files.
Returns a list of tuples, one for each plan file,
containing the template path and the template content.
"""
ret = []
swift_client = get_swift_client(variables['undercloud_swift_url'],
variables['os_auth_token'])
container = swift_client.get_container(variables['plan'])
for item in container[1]:
obj = swift_client.get_object(variables['plan'], item['name'])
ret.append((item['name'], obj))
return ret