Use MultiKeyActionValue action for extra-[env-]vars arguments

This patch adds the ability to pass extra variables and extra
environment variables to the Ansible execution as
key1=value1,key2=value2 instead of using a dict.

This action ensure that ``dest`` is a list. The list will finally
contain multiple dicts, with key=value pairs in them.

This patch also enables the Ansible execution verbosity when using a
different callback by passing --extra-env-vars
ANSIBLE_STDOUT_CALLBACK=default in the command line.

Finally, this change will make the CLI easier to use and faster to type.

Co-Authored-By: Gaël Chamoulaud <gchamoul@redhat.com>
Co-Authored-By: David J. Peacock <dpeacock@redhat.com>

Signed-off-by: Gael Chamoulaud (Strider) <gchamoul@redhat.com>
Change-Id: I643b27a19f985af2cbd8749139532348cf695462
(cherry picked from commit eee9d74c43)
This commit is contained in:
Gael Chamoulaud (Strider) 2021-03-25 07:00:45 +01:00
parent 2326bb49f6
commit 196c1e0c1f
No known key found for this signature in database
GPG Key ID: 4119D0305C651D66
1 changed files with 37 additions and 14 deletions

View File

@ -20,6 +20,7 @@ import yaml
from openstack import exceptions as os_exceptions from openstack import exceptions as os_exceptions
from osc_lib import exceptions from osc_lib import exceptions
from osc_lib.cli import parseractions
from osc_lib.i18n import _ from osc_lib.i18n import _
from prettytable import PrettyTable from prettytable import PrettyTable
@ -257,12 +258,13 @@ class TripleOValidatorRun(command.Command):
extra_vars_group.add_argument( extra_vars_group.add_argument(
'--extra-vars', '--extra-vars',
action='store', metavar="key1=<val1>[,key2=val2 --extra-vars key3=<val3>]",
default={}, action=parseractions.MultiKeyValueAction,
type=json.loads,
help=_( help=_(
"Add a dictionary as extra variable to a validation: " "Add Ansible extra variables to the validation(s) execution "
"--extra-vars '{\"min_undercloud_ram_gb\": 24}'") "as KEY=VALUE pair(s). Note that if you pass the same "
"KEY multiple times, the last given VALUE for that same KEY "
"will override the other(s)")
) )
extra_vars_group.add_argument( extra_vars_group.add_argument(
@ -280,13 +282,14 @@ class TripleOValidatorRun(command.Command):
extra_vars_group.add_argument( extra_vars_group.add_argument(
'--extra-env-vars', '--extra-env-vars',
action='store', metavar="key1=<val1>[,key2=val2 --extra-env-vars key3=<val3>]",
default={}, action=parseractions.MultiKeyValueAction,
type=json.loads,
help=_( help=_(
"A dictionary as extra environment variables you may need " "Add extra environment variables you may need "
"to provide to your Ansible execution example:" "to provide to your Ansible execution "
"ANSIBLE_STDOUT_CALLBACK=default") "as KEY=VALUE pairs. Note that if you pass the same "
"KEY multiple times, the last given VALUE for that same KEY "
"will override the other(s)")
) )
extra_vars_group.add_argument( extra_vars_group.add_argument(
@ -358,7 +361,14 @@ class TripleOValidatorRun(command.Command):
msg = "Running Validations without Overcloud settings." msg = "Running Validations without Overcloud settings."
LOG.warning("{}{}{}".format(YELLOW, msg, RESET)) LOG.warning("{}{}{}".format(YELLOW, msg, RESET))
limit = parsed_args.limit limit = parsed_args.limit
extra_vars = parsed_args.extra_vars
extra_vars = dict()
if parsed_args.extra_vars:
# if using multiple --extra-vars argument in the command-line
# we will get a list of multiple dictionaries.
for keypair in parsed_args.extra_vars:
extra_vars.update(keypair)
if parsed_args.extra_vars_file: if parsed_args.extra_vars_file:
try: try:
with open(parsed_args.extra_vars_file, 'r') as env_file: with open(parsed_args.extra_vars_file, 'r') as env_file:
@ -369,6 +379,19 @@ class TripleOValidatorRun(command.Command):
"Details: %s." % e) "Details: %s." % e)
raise exceptions.CommandError(error_msg) raise exceptions.CommandError(error_msg)
# Ansible execution should be quiet while using the validations_json
# default callback and be verbose while passing ANSIBLE_SDTOUT_CALLBACK
# environment variable to Ansible through the --extra-env-vars argument
quiet_mode = True
extra_env_vars = dict()
if parsed_args.extra_env_vars:
# if using multiple --extra-env-vars argument in the command-line
# we will get a list of multiple dictionaries.
for keypair in parsed_args.extra_env_vars:
if "ANSIBLE_STDOUT_CALLBACK" in keypair.keys():
quiet_mode = False
extra_env_vars.update(keypair)
# We don't check if the file exists in order to support # We don't check if the file exists in order to support
# passing a string such as "localhost,", like we can do with # passing a string such as "localhost,", like we can do with
# the "-i" option of ansible-playbook. # the "-i" option of ansible-playbook.
@ -392,8 +415,8 @@ class TripleOValidatorRun(command.Command):
extra_vars=extra_vars, extra_vars=extra_vars,
validations_dir=constants.ANSIBLE_VALIDATION_DIR, validations_dir=constants.ANSIBLE_VALIDATION_DIR,
validation_name=parsed_args.validation_name, validation_name=parsed_args.validation_name,
extra_env_vars=parsed_args.extra_env_vars, extra_env_vars=extra_env_vars,
quiet=True) quiet=quiet_mode)
except RuntimeError as e: except RuntimeError as e:
raise exceptions.CommandError(e) raise exceptions.CommandError(e)