diff --git a/README.md b/README.md index bd0b041779f0875fca6449738828fcb2a241d864..778558345113503e136546b97e6b913a0e6130cb 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ There are plugin parameters to define how the plugin will behave: * **cache** (optional, defaults to None): Fully qualified filename for a cache of der Server Density data * **cleanup** (optional, defaults to False): Decides if undefined alerts in your Ansible inventory available at Server Density should be deleted * **readonly** (optional, defaults to False): If set to True it will only read the current settings from SD and stores them in a temporary file and does nothing else. This is useful when you want to find out some variable names for alerts or similar things +* **output** (optional, defaults to False): If provided, all current settings that are currently set upstream at Server Density will be written in YAML to this file ##Installation## diff --git a/action_plugins/serverdensity.py b/action_plugins/serverdensity.py index 622d615b99680ba694e9579bbfdab11ce25d16fa..9961292cdb746e983bdbb9e5005ac3fc8d975fba 100644 --- a/action_plugins/serverdensity.py +++ b/action_plugins/serverdensity.py @@ -38,34 +38,44 @@ class ActionModule(object): if complex_args: args.update(complex_args) args.update(parse_kv(module_args)) - if 'api_token' not in args: - raise ae("'api_token' is a required argument.") - - self.api_token = args.get('api_token') + if 'api_token' in args: + self.api_token = args.get('api_token') + else: + allgroup = self.runner.inventory.get_group('all') + allvariables = allgroup.get_variables() + if 'sd_api_token' in allvariables: + self.api_token = allvariables.get('sd_api_token') + else: + raise ae("'api_token' is a required argument.") self.force_update = args.get('force', False) self.cache_file_name = args.get('cache', False) cleanup = args.get('cleanup', False) just_download = args.get('readonly', False) + output = args.get('output', False) if just_download: self.force_update = False self.cache_file_name = tempfile.mktemp(prefix='sd_', suffix='.json') cleanup = False + if output and self.cache_file_name != 'False': + self.cache_file_name = tempfile.mktemp(prefix='sd_', suffix='.json') + result = {} self.list_all() + if output: + if os.path.exists(self.cache_file_name): + vv('Writing upstream settings to %s' % os.path.abspath(output)) + with open(self.cache_file_name, 'r') as content_file: + content = json.load(content_file) + with open(output, 'w') as output_file: + yaml.safe_dump(content, output_file, encoding='utf-8', allow_unicode=True) + if just_download: vv('Downloaded settings to %s' % self.cache_file_name) - output = args.get('output', False) - if output: - if os.path.exists(self.cache_file_name): - with open(self.cache_file_name, 'r') as content_file: - content = json.load(content_file) - with open(output, 'w') as output_file: - yaml.safe_dump(content, output_file, encoding='utf-8', allow_unicode=True) return ReturnData(conn=conn, comm_ok=True, result=result) services = {}