Browse Source

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
changes/99/782899/1
Gael Chamoulaud (Strider) 2 months ago
parent
commit
eee9d74c43
No known key found for this signature in database GPG Key ID: 4119D0305C651D66
1 changed files with 37 additions and 14 deletions
  1. +37
    -14
      tripleoclient/v1/tripleo_validator.py

+ 37
- 14
tripleoclient/v1/tripleo_validator.py View File

@ -20,6 +20,7 @@ import yaml
from openstack import exceptions as os_exceptions
from osc_lib import exceptions
from osc_lib.cli import parseractions
from osc_lib.i18n import _
from prettytable import PrettyTable
@ -257,12 +258,13 @@ class TripleOValidatorRun(command.Command):
extra_vars_group.add_argument(
'--extra-vars',
action='store',
default={},
type=json.loads,
metavar="key1=<val1>[,key2=val2 --extra-vars key3=<val3>]",
action=parseractions.MultiKeyValueAction,
help=_(
"Add a dictionary as extra variable to a validation: "
"--extra-vars '{\"min_undercloud_ram_gb\": 24}'")
"Add Ansible extra variables to the validation(s) execution "
"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(
@ -280,13 +282,14 @@ class TripleOValidatorRun(command.Command):
extra_vars_group.add_argument(
'--extra-env-vars',
action='store',
default={},
type=json.loads,
metavar="key1=<val1>[,key2=val2 --extra-env-vars key3=<val3>]",
action=parseractions.MultiKeyValueAction,
help=_(
"A dictionary as extra environment variables you may need "
"to provide to your Ansible execution example:"
"ANSIBLE_STDOUT_CALLBACK=default")
"Add extra environment variables you may need "
"to provide to your Ansible execution "
"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(
@ -358,7 +361,14 @@ class TripleOValidatorRun(command.Command):
msg = "Running Validations without Overcloud settings."
LOG.warning("{}{}{}".format(YELLOW, msg, RESET))
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:
try:
with open(parsed_args.extra_vars_file, 'r') as env_file:
@ -369,6 +379,19 @@ class TripleOValidatorRun(command.Command):
"Details: %s." % e)
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
# passing a string such as "localhost,", like we can do with
# the "-i" option of ansible-playbook.
@ -392,8 +415,8 @@ class TripleOValidatorRun(command.Command):
extra_vars=extra_vars,
validations_dir=constants.ANSIBLE_VALIDATION_DIR,
validation_name=parsed_args.validation_name,
extra_env_vars=parsed_args.extra_env_vars,
quiet=True)
extra_env_vars=extra_env_vars,
quiet=quiet_mode)
except RuntimeError as e:
raise exceptions.CommandError(e)


Loading…
Cancel
Save