Generate ansible.cfg for UC/standalone deployments

Add CLI command that allows to generate and manually
customize the default ansible configuration used with
UC/standalone deployments. Use the config generator
from tripleo common ansible actions for that.

If there is no pre-created user's ansible.cfg in
--output-dir, generate the default one with as a part of
the deployment command.

Depends-On: Ie7dd9039ee74aac046679e831ffda143257e3825
Change-Id: I695916e9271392127635834078e57655857182f1
Signed-off-by: Bogdan Dobrelya <bdobreli@redhat.com>
This commit is contained in:
Bogdan Dobrelya 2018-07-04 16:28:12 +03:00 committed by Emilien Macchi
parent d32cfff375
commit a9dd33a8c1
5 changed files with 112 additions and 2 deletions

View File

@ -0,0 +1,17 @@
---
features:
- |
``tripleo config generate ansible`` generates the default ``ansible.cfg``
in the given ``--output-dir`` (defaults to `$HOME`). The remote user
setting for ansible will be set to the ``--deployment-user`` value
(defaults to 'stack').
.. note:: Do not confuse the generated config with ``~/.ansible.cfg``.
The latter takes the lower precedence.
You may want to customize the generated config so it will be used
with all undercloud and standalone deployments.
.. note:: Overcloud deployments use Mistral workflows to configure ansible
for its own use, but the basic configuration it takes looks very
similar.

View File

@ -47,6 +47,7 @@ openstack.cli.extension =
tripleoclient = tripleoclient.plugin
openstack.tripleoclient.v1 =
tripleo_config_generate_ansible = tripleoclient.v1.tripleo_config:GenerateAnsibleConfig
tripleo_deploy = tripleoclient.v1.tripleo_deploy:Deploy
tripleo_upgrade = tripleoclient.v1.tripleo_upgrade:Upgrade
overcloud_netenv_validate = tripleoclient.v1.overcloud_netenv_validate:ValidateOvercloudNetenv

View File

@ -603,6 +603,8 @@ class TestDeployUndercloud(TestPluginV1):
env
)
@mock.patch('tripleo_common.actions.ansible.'
'write_default_ansible_cfg')
# TODO(cjeanner) drop once we have proper oslo.privsep
@mock.patch('os.chmod')
# TODO(cjeanner) drop once we have proper oslo.privsep
@ -647,7 +649,7 @@ class TestDeployUndercloud(TestPluginV1):
mock_cleanupdirs, mock_launchansible,
mock_tarball, mock_templates_dir,
mock_open, mock_os, mock_user, mock_cc,
mock_chmod):
mock_chmod, mock_ac):
parsed_args = self.check_parser(self.cmd,
['--local-ip', '127.0.0.1',

View File

@ -0,0 +1,69 @@
# Copyright 2018 Red Hat, Inc.
#
# 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
import argparse
import logging
from cliff import command
from osc_lib.i18n import _
from tripleoclient import constants
from tripleoclient import utils
# For ansible.cfg generation
from tripleo_common.actions import ansible
class GenerateAnsibleConfig(command.Command):
"""Generate the default ansible.cfg for UC/AIO-standalone deployments."""
log = logging.getLogger(__name__ + ".GenerateAnsibleConfig")
def get_parser(self, prog_name):
parser = argparse.ArgumentParser(
description=self.get_description(),
prog=prog_name,
add_help=False
)
# TODO(bogdando): drop that once using oslo.privsep
parser.add_argument(
'--deployment-user',
dest='deployment_user',
default='stack',
help=_('User who executes the tripleo config generate command. '
'Defaults to stack.')
)
# TODO(bogdando): find a better UNDERCLOUD_OUTPUT_DIR constant name
# Add more params as far as the imported ansible actions support it
parser.add_argument('--output-dir',
dest='output_dir',
help=_("Directory to output ansible.cfg and "
"ansible.log files."),
default=constants.UNDERCLOUD_OUTPUT_DIR)
def take_action(self, parsed_args):
self.log.debug("take_action(%s)" % parsed_args)
if utils.get_deployment_user() != parsed_args.deployment_user:
self.log.warning(
_('The --deployment-user value %s does not '
'match the user name executing this command!') %
parsed_args.deployment_user)
# FIXME(bogdando): unhardcode key/transport for future multi-node
ansible.write_default_ansible_cfg(parsed_args.output_dir,
parsed_args.deployment_user,
ssh_private_key=None,
transport='local')

View File

@ -43,7 +43,8 @@ from tripleoclient import utils
from tripleo_common.image import kolla_builder
from tripleo_common.utils import passwords as password_utils
# For ansible download
# For ansible download and config generation
from tripleo_common.actions import ansible
from tripleo_common.inventory import TripleoInventory
from tripleo_common.utils import config
@ -1012,6 +1013,26 @@ class Deploy(command.Command):
self._download_ansible_playbooks(orchestration_client,
parsed_args.stack,
parsed_args.standalone_role)
# Do not override user's custom ansible configuraition file,
# it may have been pre-created with the tripleo CLI, or the like
ansible_config = os.path.join(self.output_dir, 'ansible.cfg')
if not os.path.isfile(ansible_config):
self.log.warning(
_('Generating default ansible config file %s') %
ansible_config)
# FIXME(bogdando): unhardcode key/transport for future
# multi-node
ansible.write_default_ansible_cfg(
ansible_dir,
parsed_args.deployment_user,
ssh_private_key=None,
transport='local')
else:
self.log.warning(
_('Using the existing %s for deployment') % ansible_config)
shutil.copy(ansible_config, ansible_dir)
# Kill heat, we're done with it now.
if not parsed_args.keep_running:
self._kill_heat(parsed_args)