Allow ansible (ad-hoc) to support --extra-vars also
This commit is contained in:
parent
9fe0b88332
commit
68358e31fb
4 changed files with 24 additions and 16 deletions
|
@ -120,6 +120,8 @@ class Cli(object):
|
||||||
if not options.ask_vault_pass and options.vault_password_file:
|
if not options.ask_vault_pass and options.vault_password_file:
|
||||||
vault_pass = utils.read_vault_file(options.vault_password_file)
|
vault_pass = utils.read_vault_file(options.vault_password_file)
|
||||||
|
|
||||||
|
extra_vars = utils.parse_extra_vars(options.extra_vars, vault_pass)
|
||||||
|
|
||||||
inventory_manager = inventory.Inventory(options.inventory, vault_password=vault_pass)
|
inventory_manager = inventory.Inventory(options.inventory, vault_password=vault_pass)
|
||||||
if options.subset:
|
if options.subset:
|
||||||
inventory_manager.subset(options.subset)
|
inventory_manager.subset(options.subset)
|
||||||
|
@ -168,7 +170,8 @@ class Cli(object):
|
||||||
su=options.su,
|
su=options.su,
|
||||||
su_pass=su_pass,
|
su_pass=su_pass,
|
||||||
su_user=options.su_user,
|
su_user=options.su_user,
|
||||||
vault_pass=vault_pass
|
vault_pass=vault_pass,
|
||||||
|
extra_vars=extra_vars,
|
||||||
)
|
)
|
||||||
|
|
||||||
if options.seconds:
|
if options.seconds:
|
||||||
|
|
|
@ -47,7 +47,6 @@ import ansible.utils.template
|
||||||
from ansible import errors
|
from ansible import errors
|
||||||
from ansible import callbacks
|
from ansible import callbacks
|
||||||
from ansible import utils
|
from ansible import utils
|
||||||
from ansible.utils import to_unicode
|
|
||||||
from ansible.color import ANSIBLE_COLOR, stringc
|
from ansible.color import ANSIBLE_COLOR, stringc
|
||||||
from ansible.callbacks import display
|
from ansible.callbacks import display
|
||||||
|
|
||||||
|
@ -84,8 +83,6 @@ def main(args):
|
||||||
)
|
)
|
||||||
#parser.add_option('--vault-password', dest="vault_password",
|
#parser.add_option('--vault-password', dest="vault_password",
|
||||||
# help="password for vault encrypted files")
|
# help="password for vault encrypted files")
|
||||||
parser.add_option('-e', '--extra-vars', dest="extra_vars", action="append",
|
|
||||||
help="set additional variables as key=value or YAML/JSON", default=[])
|
|
||||||
parser.add_option('-t', '--tags', dest='tags', default='all',
|
parser.add_option('-t', '--tags', dest='tags', default='all',
|
||||||
help="only run plays and tasks tagged with these values")
|
help="only run plays and tasks tagged with these values")
|
||||||
parser.add_option('--skip-tags', dest='skip_tags',
|
parser.add_option('--skip-tags', dest='skip_tags',
|
||||||
|
@ -143,18 +140,7 @@ def main(args):
|
||||||
if not options.ask_vault_pass and options.vault_password_file:
|
if not options.ask_vault_pass and options.vault_password_file:
|
||||||
vault_pass = utils.read_vault_file(options.vault_password_file)
|
vault_pass = utils.read_vault_file(options.vault_password_file)
|
||||||
|
|
||||||
extra_vars = {}
|
extra_vars = utils.parse_extra_vars(options.extra_vars, vault_pass)
|
||||||
for extra_vars_opt in options.extra_vars:
|
|
||||||
extra_vars_opt = to_unicode(extra_vars_opt)
|
|
||||||
if extra_vars_opt.startswith(u"@"):
|
|
||||||
# Argument is a YAML file (JSON is a subset of YAML)
|
|
||||||
extra_vars = utils.combine_vars(extra_vars, utils.parse_yaml_from_file(extra_vars_opt[1:], vault_password=vault_pass))
|
|
||||||
elif extra_vars_opt and extra_vars_opt[0] in u'[{':
|
|
||||||
# Arguments as YAML
|
|
||||||
extra_vars = utils.combine_vars(extra_vars, utils.parse_yaml(extra_vars_opt))
|
|
||||||
else:
|
|
||||||
# Arguments as Key-value
|
|
||||||
extra_vars = utils.combine_vars(extra_vars, utils.parse_kv(extra_vars_opt))
|
|
||||||
|
|
||||||
only_tags = options.tags.split(",")
|
only_tags = options.tags.split(",")
|
||||||
skip_tags = options.skip_tags
|
skip_tags = options.skip_tags
|
||||||
|
|
|
@ -189,6 +189,8 @@ def main(args):
|
||||||
cmd = 'ansible localhost -i "%s" %s -m %s -a "%s"' % (
|
cmd = 'ansible localhost -i "%s" %s -m %s -a "%s"' % (
|
||||||
inv_opts, base_opts, options.module_name, repo_opts
|
inv_opts, base_opts, options.module_name, repo_opts
|
||||||
)
|
)
|
||||||
|
for ev in options.extra_vars:
|
||||||
|
cmd += ' -e "%s"' % ev
|
||||||
|
|
||||||
if options.sleep:
|
if options.sleep:
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -978,6 +978,8 @@ def base_parser(constants=C, usage="", output_opts=False, runas_opts=False,
|
||||||
parser.add_option('-i', '--inventory-file', dest='inventory',
|
parser.add_option('-i', '--inventory-file', dest='inventory',
|
||||||
help="specify inventory host file (default=%s)" % constants.DEFAULT_HOST_LIST,
|
help="specify inventory host file (default=%s)" % constants.DEFAULT_HOST_LIST,
|
||||||
default=constants.DEFAULT_HOST_LIST)
|
default=constants.DEFAULT_HOST_LIST)
|
||||||
|
parser.add_option('-e', '--extra-vars', dest="extra_vars", action="append",
|
||||||
|
help="set additional variables as key=value or YAML/JSON", default=[])
|
||||||
parser.add_option('-k', '--ask-pass', default=False, dest='ask_pass', action='store_true',
|
parser.add_option('-k', '--ask-pass', default=False, dest='ask_pass', action='store_true',
|
||||||
help='ask for SSH password')
|
help='ask for SSH password')
|
||||||
parser.add_option('--private-key', default=C.DEFAULT_PRIVATE_KEY_FILE, dest='private_key_file',
|
parser.add_option('--private-key', default=C.DEFAULT_PRIVATE_KEY_FILE, dest='private_key_file',
|
||||||
|
@ -1048,6 +1050,21 @@ def base_parser(constants=C, usage="", output_opts=False, runas_opts=False,
|
||||||
|
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
|
def parse_extra_vars(extra_vars_opts, vault_pass):
|
||||||
|
extra_vars = {}
|
||||||
|
for extra_vars_opt in extra_vars_opts:
|
||||||
|
extra_vars_opt = to_unicode(extra_vars_opt)
|
||||||
|
if extra_vars_opt.startswith(u"@"):
|
||||||
|
# Argument is a YAML file (JSON is a subset of YAML)
|
||||||
|
extra_vars = combine_vars(extra_vars, parse_yaml_from_file(extra_vars_opt[1:], vault_password=vault_pass))
|
||||||
|
elif extra_vars_opt and extra_vars_opt[0] in u'[{':
|
||||||
|
# Arguments as YAML
|
||||||
|
extra_vars = combine_vars(extra_vars, parse_yaml(extra_vars_opt))
|
||||||
|
else:
|
||||||
|
# Arguments as Key-value
|
||||||
|
extra_vars = combine_vars(extra_vars, parse_kv(extra_vars_opt))
|
||||||
|
return extra_vars
|
||||||
|
|
||||||
def ask_vault_passwords(ask_vault_pass=False, ask_new_vault_pass=False, confirm_vault=False, confirm_new=False):
|
def ask_vault_passwords(ask_vault_pass=False, ask_new_vault_pass=False, confirm_vault=False, confirm_new=False):
|
||||||
|
|
||||||
vault_pass = None
|
vault_pass = None
|
||||||
|
|
Loading…
Reference in a new issue