From e93a61ac534333f1de63790bb4b86eca0b6a0c57 Mon Sep 17 00:00:00 2001 From: Bill Nottingham Date: Thu, 25 May 2017 16:51:09 -0400 Subject: [PATCH] Allow setting the enabled/disabled flag for more than just cloud inventory sources. Leverage the same _ENABLED_VAR (and other) variables that exist for cloud sources, and set some defaults for custom scripts. --- awx/main/tasks.py | 65 +++++++++++++++++----------------------- awx/settings/defaults.py | 10 +++++++ 2 files changed, 37 insertions(+), 38 deletions(-) diff --git a/awx/main/tasks.py b/awx/main/tasks.py index cb8da9633b..c0eeec4764 100644 --- a/awx/main/tasks.py +++ b/awx/main/tasks.py @@ -1755,52 +1755,41 @@ class RunInventoryUpdate(BaseTask): args.append('--overwrite') if inventory_update.overwrite_vars: args.append('--overwrite-vars') - args.append('--source') - # If this is a cloud-based inventory (e.g. from AWS, Rackspace, etc.) - # then we need to set some extra flags based on settings in - # Tower. - # - # These settings are "per-cloud-provider"; it's entirely possible that + src = inventory_update.source + + # Add several options to the shell arguments based on the + # inventory-source-specific setting in the Tower configuration. + # These settings are "per-source"; it's entirely possible that # they will be different between cloud providers if a Tower user # actively uses more than one. - if inventory_update.source in CLOUD_PROVIDERS: - # We need to reference the source's code frequently, assign it - # to a shorter variable. :) - src = inventory_update.source - + if getattr(settings, '%s_ENABLED_VAR' % src.upper(), False): + args.extend(['--enabled-var', + getattr(settings, '%s_ENABLED_VAR' % src.upper())]) + if getattr(settings, '%s_ENABLED_VALUE' % src.upper(), False): + args.extend(['--enabled-value', + getattr(settings, '%s_ENABLED_VALUE' % src.upper())]) + if getattr(settings, '%s_GROUP_FILTER' % src.upper(), False): + args.extend(['--group-filter', + getattr(settings, '%s_GROUP_FILTER' % src.upper())]) + if getattr(settings, '%s_HOST_FILTER' % src.upper(), False): + args.extend(['--host-filter', + getattr(settings, '%s_HOST_FILTER' % src.upper())]) + if getattr(settings, '%s_EXCLUDE_EMPTY_GROUPS' % src.upper()): + args.append('--exclude-empty-groups') + if getattr(settings, '%s_INSTANCE_ID_VAR' % src.upper(), False): + args.extend(['--instance-id-var', + getattr(settings, '%s_INSTANCE_ID_VAR' % src.upper()),]) + # Add arguments for the source inventory script + args.append('--source') + if src in CLOUD_PROVIDERS: # Get the path to the inventory plugin, and append it to our # arguments. plugin_path = self.get_path_to('..', 'plugins', 'inventory', '%s.py' % src) args.append(plugin_path) - - # Add several options to the shell arguments based on the - # cloud-provider-specific setting in the Tower configuration. - args.extend(['--enabled-var', - getattr(settings, '%s_ENABLED_VAR' % src.upper())]) - args.extend(['--enabled-value', - getattr(settings, '%s_ENABLED_VALUE' % src.upper())]) - args.extend(['--group-filter', - getattr(settings, '%s_GROUP_FILTER' % src.upper())]) - args.extend(['--host-filter', - getattr(settings, '%s_HOST_FILTER' % src.upper())]) - - # We might have a flag set to exclude empty groups; if we do, - # add that flag to the shell arguments. - if getattr(settings, '%s_EXCLUDE_EMPTY_GROUPS' % src.upper()): - args.append('--exclude-empty-groups') - - # We might have a flag for an instance ID variable; if we do, - # add it to the shell arguments. - if getattr(settings, '%s_INSTANCE_ID_VAR' % src.upper(), False): - args.extend([ - '--instance-id-var', - getattr(settings, '%s_INSTANCE_ID_VAR' % src.upper()), - ]) - - elif inventory_update.source == 'scm': + elif src == 'scm': args.append(inventory_update.get_actual_source_path()) - elif inventory_update.source == 'custom': + elif src == 'custom': runpath = tempfile.mkdtemp(prefix='ansible_tower_launch_') handle, path = tempfile.mkstemp(dir=runpath) f = os.fdopen(handle, 'w') diff --git a/awx/settings/defaults.py b/awx/settings/defaults.py index 2d97a3e152..3c6709c92c 100644 --- a/awx/settings/defaults.py +++ b/awx/settings/defaults.py @@ -805,6 +805,16 @@ CLOUDFORMS_HOST_FILTER = r'^.+$' CLOUDFORMS_EXCLUDE_EMPTY_GROUPS = True CLOUDFORMS_INSTANCE_ID_VAR = 'id' +# --------------------- +# ----- Custom ----- +# --------------------- +#CUSTOM_ENABLED_VAR = +#CUSTOM_ENABLED_VALUE = +CUSTOM_GROUP_FILTER = r'^.+$' +CUSTOM_HOST_FILTER = r'^.+$' +CUSTOM_EXCLUDE_EMPTY_GROUPS = True +#CUSTOM_INSTANCE_ID_VAR = + # --------------------- # -- Activity Stream -- # ---------------------