From 4fad1567687407af8ed5438f48ab5827f172416d Mon Sep 17 00:00:00 2001 From: Flavio Percoco Premoli Date: Mon, 7 Aug 2017 19:04:03 +0200 Subject: [PATCH] Allow for generating kubernetes YAML files from modules (#27833) * Return the request dictionary in the results It's sometimes useful to have access to the request params in a k8s style. The dictionary returned by the request_params call can be serialized into YAML to produce a k8s like file. * Add dry_run option to skip module execution By having support for dry_run executions, it'll be possible to generate YAML files from the results dictionary by using the data in the `requests` key. --- lib/ansible/module_utils/k8s_common.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/ansible/module_utils/k8s_common.py b/lib/ansible/module_utils/k8s_common.py index c54c046870f..7800898fff7 100644 --- a/lib/ansible/module_utils/k8s_common.py +++ b/lib/ansible/module_utils/k8s_common.py @@ -85,7 +85,17 @@ class KubernetesAnsibleModule(AnsibleModule): :return: dict: a valid Ansible argument spec """ if not self.argspec_cache: - spec = {} + spec = { + 'dry_run': { + 'type': 'bool', + 'default': False, + 'description': [ + "If set to C(True) the module will exit without executing any action." + "Useful to only generate YAML file definitions for the resources in the tasks." + ] + } + } + for arg_name, arg_properties in self.helper.argspec.items(): spec[arg_name] = {} for option, option_value in arg_properties.items(): @@ -125,13 +135,19 @@ class KubernetesAnsibleModule(AnsibleModule): state = self.params.get('state', None) force = self.params.get('force', False) + dry_run = self.params.pop('dry_run', False) name = self.params.get('name') namespace = self.params.get('namespace', None) existing = None - return_attributes = dict(changed=False, api_version=self.api_version) + return_attributes = dict(changed=False, + api_version=self.api_version, + request=self.helper.request_body_from_params(self.params)) return_attributes[self.helper.base_model_name_snake] = {} + if dry_run: + self.exit_json(**return_attributes) + try: auth_options = {} for key, value in self.helper.argspec.items():