From d75e49693bacf0f92a500332dbd007e527a8a298 Mon Sep 17 00:00:00 2001 From: Fabian von Feilitzsch Date: Fri, 29 Jun 2018 10:21:47 -0400 Subject: [PATCH] update module arguments to allow resource_definition to be a string (#40730) --- lib/ansible/module_utils/k8s/common.py | 1 - lib/ansible/module_utils/k8s/raw.py | 11 +++++++++-- lib/ansible/modules/clustering/k8s/k8s.py | 8 ++++---- .../module_docs_fragments/k8s_resource_options.py | 4 ++-- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/ansible/module_utils/k8s/common.py b/lib/ansible/module_utils/k8s/common.py index 32e2e6e3a6..ca08264ce0 100644 --- a/lib/ansible/module_utils/k8s/common.py +++ b/lib/ansible/module_utils/k8s/common.py @@ -63,7 +63,6 @@ COMMON_ARG_SPEC = { 'default': False, }, 'resource_definition': { - 'type': 'dict', 'aliases': ['definition', 'inline'] }, 'src': { diff --git a/lib/ansible/module_utils/k8s/raw.py b/lib/ansible/module_utils/k8s/raw.py index 0d62c190c0..630096957f 100644 --- a/lib/ansible/module_utils/k8s/raw.py +++ b/lib/ansible/module_utils/k8s/raw.py @@ -23,9 +23,10 @@ from ansible.module_utils.k8s.common import KubernetesAnsibleModule try: + import yaml from openshift.dynamic.exceptions import DynamicApiError, NotFoundError, ConflictError except ImportError: - # Exception handled in common + # Exceptions handled in common pass @@ -49,7 +50,13 @@ class KubernetesRawModule(KubernetesAnsibleModule): namespace = self.params.pop('namespace') resource_definition = self.params.pop('resource_definition') if resource_definition: - self.resource_definitions = [resource_definition] + if isinstance(resource_definition, str): + try: + self.resource_definitions = yaml.safe_load_all(resource_definition) + except (IOError, yaml.YAMLError) as exc: + self.fail(msg="Error loading resource_definition: {0}".format(exc)) + else: + self.resource_definitions = [resource_definition] src = self.params.pop('src') if src: self.resource_definitions = self.load_resource_definitions(src) diff --git a/lib/ansible/modules/clustering/k8s/k8s.py b/lib/ansible/modules/clustering/k8s/k8s.py index af265201ab..6fb6084757 100644 --- a/lib/ansible/modules/clustering/k8s/k8s.py +++ b/lib/ansible/modules/clustering/k8s/k8s.py @@ -99,12 +99,12 @@ EXAMPLES = ''' - name: Read definition file from the Ansible controller file system k8s: state: present - definition: "{{ lookup('file', '/testing/deployment.yml') | from_yaml }}" + definition: "{{ lookup('file', '/testing/deployment.yml') }}" - name: Read definition file from the Ansible controller file system after Jinja templating k8s: state: present - definition: "{{ lookup('template', '/testing/deployment.yml') | from_yaml }}" + definition: "{{ lookup('template', '/testing/deployment.yml') }}" ''' RETURN = ''' @@ -135,8 +135,8 @@ result: returned: success type: complex items: - description: Returned only when the I(kind) is a List type resource. Contains a set of objects. - returned: when resource is a List + description: Returned only when multiple yaml documents are passed to src or resource_definition + returned: when resource_definition or src contains list of objects type: list ''' diff --git a/lib/ansible/utils/module_docs_fragments/k8s_resource_options.py b/lib/ansible/utils/module_docs_fragments/k8s_resource_options.py index 8f091a4923..1466210e1f 100644 --- a/lib/ansible/utils/module_docs_fragments/k8s_resource_options.py +++ b/lib/ansible/utils/module_docs_fragments/k8s_resource_options.py @@ -25,14 +25,14 @@ class ModuleDocFragment(object): options: resource_definition: description: - - "Provide a valid YAML definition for an object when creating or updating. NOTE: I(kind), I(api_version), I(name), + - "Provide a valid YAML definition (either as a string or a dict) for an object when creating or updating. NOTE: I(kind), I(api_version), I(name), and I(namespace) will be overwritten by corresponding values found in the provided I(resource_definition)." aliases: - definition - inline src: description: - - "Provide a path to a file containing a valid YAML definition of an object to be created or updated. Mutually + - "Provide a path to a file containing a valid YAML definition of an object or objects to be created or updated. Mutually exclusive with I(resource_definition). NOTE: I(kind), I(api_version), I(name), and I(namespace) will be overwritten by corresponding values found in the configuration read in from the I(src) file." - Reads from the local file system. To read from the Ansible controller's file system, use the file lookup