Add module and playbook for overcloudrc generation

playbook: cli-generate-overcloudrc.yaml
module: tripleo_generate_overcloudrc

Change-Id: I9d3bb745dbd67b870bace87da48ccc77773b7209
This commit is contained in:
Rabi Mishra 2020-05-05 12:18:53 +05:30
parent 43282081d0
commit 03c6c36eb5
3 changed files with 210 additions and 0 deletions

View File

@ -0,0 +1,14 @@
=====================================
Module - tripleo_generate_overcloudrc
=====================================
This module provides for the following ansible plugin:
* tripleo_generate_overcloudrc
.. ansibleautoplugin::
:module: tripleo_ansible/ansible_plugins/modules/tripleo_generate_overcloudrc.py
:documentation: true
:examples: true

View File

@ -0,0 +1,152 @@
#!/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
from ansible.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 tripleo_common.utils import overcloudrc as rc_utils
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = '''
---
module: tripleo_generate_overcloudrc
short_description: Generate overcloudrc
version_added: "2.8"
description:
- "Generate overcloudrc."
options:
container:
description:
- Overcloud plan container name
type: str
default: overcloud
no_proxy:
description:
- Comma-separated string of hosts that shouldn't be proxied
type: str
default: ''
author:
- Rabi Mishra (@ramishra)
requirements: ["openstacksdk", "tripleo-common"]
'''
EXAMPLES = '''
- name: Generate overcloudrc
tripleo_generate_overcloudrc:
container: overcloud
no_proxy: 'myhost'
'''
RETURN = '''
overcloudrc:
description: overcloudrc string
returned: always
type: str
no_log: true
sample:
# Clear any old environment that may conflict.
for key in $( set | awk '{FS="="} /^OS_/ {print $1}' ); do unset $key ; done
export OS_USERNAME=admin
export OS_PROJECT_NAME=admin
export OS_USER_DOMAIN_NAME=Default
export OS_PROJECT_DOMAIN_NAME=Default
export OS_NO_CACHE=True
export OS_CLOUDNAME=overcloud-0
export no_proxy=,10.20.1.38,192.168.24.14
export PYTHONWARNINGS='ignore:Certificate has no, ignore:A true SSLContext object is not available'
export OS_AUTH_TYPE=password
export OS_PASSWORD=xxxxxxxxxx
export OS_AUTH_URL=http://x.x.x.x:5000
export OS_IDENTITY_API_VERSION=3
export OS_COMPUTE_API_VERSION=2.latest
export OS_IMAGE_API_VERSION=2
export OS_VOLUME_API_VERSION=3
export OS_REGION_NAME=regionOne
'''
def run_module():
result = dict(
success=False,
error="",
overcloudrc=""
)
argument_spec = openstack_full_argument_spec(
**yaml.safe_load(DOCUMENTATION)['options']
)
module = AnsibleModule(
argument_spec,
supports_check_mode=True,
**openstack_module_kwargs()
)
try:
container = module.params.get('container')
no_proxy = module.params.get('no_proxy')
_, conn = openstack_cloud_from_module(module)
tripleo = tc.TripleOCommon(session=conn.session)
# if the user is working with this module in only check mode we do not
# want to make any changes to the environment, just return the current
# state with no modifications
if module.check_mode:
module.exit_json(**result)
swift = tripleo.get_object_client()
heat = tripleo.get_orchestration_client()
overcloudrc = rc_utils.create_overcloudrc(swift, heat,
container, no_proxy)
result['overcloudrc'] = overcloudrc['overcloudrc']
result['success'] = True
except Exception as err:
result['error'] = str(err)
result['msg'] = ("Error generating overcloudrc for plan %s: %s" % (
container, 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,44 @@
---
# 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.
- name: Generate overcloudrc
connection: "{{ (tripleo_target_host is defined) | ternary('ssh', 'local') }}"
hosts: "{{ tripleo_target_host | default('localhost') }}"
remote_user: "{{ tripleo_target_user | default(lookup('env', 'USER')) }}"
gather_facts: "{{ (tripleo_target_host is defined) | ternary(true, false) }}"
any_errors_fatal: true
vars:
container: overcloud
no_proxy: ''
pre_tasks:
- name: Check for required inputs
set_fact:
output_dir: "{{ lookup('env', 'PWD') }}"
when:
- output_dir is undefined
tasks:
- name: Generate overcloudrc
tripleo_generate_overcloudrc:
container: "{{ container }}"
no_proxy: "{{ no_proxy }}"
register: overcloudrc_result
- name: Write overcloudrc
copy:
content: "{{ overcloudrc_result['overcloudrc'] }}"
dest: "{{ output_dir}}/{{ container }}rc"