update module arguments to allow resource_definition to be a string (#40730)

pull/42112/merge
Fabian von Feilitzsch 6 years ago committed by Adam Miller
parent 5f5fbffe8a
commit d75e49693b

@ -63,7 +63,6 @@ COMMON_ARG_SPEC = {
'default': False,
},
'resource_definition': {
'type': 'dict',
'aliases': ['definition', 'inline']
},
'src': {

@ -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)

@ -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
'''

@ -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

Loading…
Cancel
Save