Module tripleo_list_overclouds

When upgrading the undercloud it will be necessary to have some
ansible tasks to loop on tasks which operate on every existing stack.

This simple module will lookup existing heat stacks and return a list
of stacks which appear to be overcloud stacks. They are assumed to be
overcloud stacks if they have the output: AnsibleHostVarsMap.

Change-Id: Ib7dc2c968a5aaff40696f591caa2399c0fb5a7bc
Blueprint: nova-less-deploy
This commit is contained in:
Steve Baker 2020-06-08 04:22:36 +00:00
parent 4ce6fda21b
commit 669af49bbb
2 changed files with 188 additions and 0 deletions

View File

@ -0,0 +1,135 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2018 OpenStack Foundation
# 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 absolute_import
from __future__ import division
from __future__ import print_function
import yaml
try:
from ansible.module_utils import tripleo_common_utils as tc
except ImportError:
from tripleo_ansible.ansible_plugins.module_utils import tripleo_common_utils as tc
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.openstack import openstack_full_argument_spec
from ansible.module_utils.openstack import openstack_module_kwargs
from ansible.module_utils.openstack import openstack_cloud_from_module
from heatclient import exc as heat_exc
from tripleo_common import inventory as inventory
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = '''
---
module: tripleo_list_overclouds
short_description: List all currently deployed overcloud stacks
version_added: "2.8"
description:
- "List all currently deployed overcloud stacks"
options: {}
author:
- Steve Baker (@stevebaker)
'''
RETURN = '''
stacks:
description: List of stacks
returned: always
type: list
sample:
- id: 6ea20112-acc5-41d8-9481-78f1151bcfaa
stack_name: overcloud
- id: 9345a389-4345-482c-9e18-db226c011e56
stack_name: other-overcloud
'''
EXAMPLES = '''
- name: Get overcloud stacks
tripleo_list_overclouds:
register: overclouds
- name: Display stack names
debug:
msg: "overcloud {{ item.stack_name }}
loop: "{{ overclouds.stacks }}
'''
def get_overclouds(heat_client):
for stack in heat_client.stacks.list():
try:
heat_client.stacks.output_show(stack.stack_name, 'AnsibleHostVarsMap')
yield {
"id": stack.id,
"stack_name": stack.stack_name
}
except heat_exc.NotFound:
pass
def run_module():
result = dict(
success=False,
changed=False,
error="",
)
argument_spec = openstack_full_argument_spec(
**yaml.safe_load(DOCUMENTATION)['options']
)
module = AnsibleModule(
argument_spec,
supports_check_mode=False,
**openstack_module_kwargs()
)
try:
_, conn = openstack_cloud_from_module(module)
tripleo = tc.TripleOCommon(session=conn.session)
heat_client = tripleo.get_orchestration_client()
result['stacks'] = list(get_overclouds(heat_client))
result['success'] = True
result['changed'] = False
except Exception as err:
result['error'] = str(err)
result['msg'] = ("Error getting overclouds: %s" % err)
module.fail_json(**result)
# in the event of a successful module execution, you will want to
# simple AnsibleModule.exit_json(), passing the key/value results
module.exit_json(**result)
def main():
run_module()
if __name__ == '__main__':
main()

View File

@ -0,0 +1,53 @@
# Copyright 2019 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.
import mock
from heatclient import exc as heat_exc
from tripleo_ansible.ansible_plugins.modules import tripleo_list_overclouds as tlo
from tripleo_ansible.tests import base as tests_base
class TestTripleoListOveclouds(tests_base.TestCase):
def test_get_overclouds(self):
mock_heat = mock.Mock()
mock_stacks = mock.Mock()
mock_heat.stacks = mock_stacks
mock_stacks.list.return_value = [
mock.Mock(id='111', stack_name='overcloud'),
mock.Mock(id='222', stack_name='some-other-stack'),
mock.Mock(id='333', stack_name='other-overcloud'),
]
output_result = {"output": {
"output_key": "AnsibleHostVarsMap",
"output_value": {}
}}
mock_stacks.output_show.side_effect = [
output_result,
heat_exc.NotFound(),
output_result
]
result = list(tlo.get_overclouds(mock_heat))
self.assertEqual([{
'id': '111',
'stack_name': 'overcloud'
}, {
'id': '333',
'stack_name': 'other-overcloud'
}], result)