| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
from __future__ import absolute_import, division, print_function |
| 5 |
|
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
# Copyright (c) 2020-2021, Red Hat |
| 9 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 10 |
|
| 11 |
# STARTREMOVE (downstream) |
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: openshift_process |
| 14 |
|
| 15 |
short_description: Process an OpenShift template.openshift.io/v1 Template |
| 16 |
|
| 17 |
version_added: "0.3.0" |
| 18 |
|
| 19 |
author: "Fabian von Feilitzsch (@fabianvf)" |
| 20 |
|
| 21 |
description: |
| 22 |
- Processes a specified OpenShift template with the provided template. |
| 23 |
- Templates can be provided inline, from a file, or specified by name and namespace in the cluster. |
| 24 |
- Analogous to `oc process`. |
| 25 |
- For CRUD operations on Template resources themselves, see the community.okd.k8s module. |
| 26 |
|
| 27 |
extends_documentation_fragment: |
| 28 |
- kubernetes.core.k8s_auth_options |
| 29 |
- kubernetes.core.k8s_wait_options |
| 30 |
- kubernetes.core.k8s_resource_options |
| 31 |
|
| 32 |
requirements: |
| 33 |
- "python >= 3.6" |
| 34 |
- "kubernetes >= 12.0.0" |
| 35 |
- "PyYAML >= 3.11" |
| 36 |
|
| 37 |
options: |
| 38 |
name: |
| 39 |
description: |
| 40 |
- The name of the Template to process. |
| 41 |
- The Template must be present in the cluster. |
| 42 |
- When provided, I(namespace) is required. |
| 43 |
- Mutually exclusive with I(resource_definition) or I(src) |
| 44 |
type: str |
| 45 |
namespace: |
| 46 |
description: |
| 47 |
- The namespace that the template can be found in. |
| 48 |
type: str |
| 49 |
namespace_target: |
| 50 |
description: |
| 51 |
- The namespace that resources should be created, updated, or deleted in. |
| 52 |
- Only used when I(state) is present or absent. |
| 53 |
type: str |
| 54 |
parameters: |
| 55 |
description: |
| 56 |
- 'A set of key: value pairs that will be used to set/override values in the Template.' |
| 57 |
- Corresponds to the `--param` argument to oc process. |
| 58 |
type: dict |
| 59 |
parameter_file: |
| 60 |
description: |
| 61 |
- A path to a file containing template parameter values to override/set values in the Template. |
| 62 |
- Corresponds to the `--param-file` argument to oc process. |
| 63 |
type: str |
| 64 |
state: |
| 65 |
description: |
| 66 |
- Determines what to do with the rendered Template. |
| 67 |
- The state I(rendered) will render the Template based on the provided parameters, and return the rendered |
| 68 |
objects in the I(resources) field. These can then be referenced in future tasks. |
| 69 |
- The state I(present) will cause the resources in the rendered Template to be created if they do not |
| 70 |
already exist, and patched if they do. |
| 71 |
- The state I(absent) will delete the resources in the rendered Template. |
| 72 |
type: str |
| 73 |
default: rendered |
| 74 |
choices: [ absent, present, rendered ] |
| 75 |
""" |
| 76 |
|
| 77 |
EXAMPLES = r""" |
| 78 |
- name: Process a template in the cluster |
| 79 |
community.okd.openshift_process: |
| 80 |
name: nginx-example |
| 81 |
namespace: openshift # only needed if using a template already on the server |
| 82 |
parameters: |
| 83 |
NAMESPACE: openshift |
| 84 |
NAME: test123 |
| 85 |
state: rendered |
| 86 |
register: result |
| 87 |
|
| 88 |
- name: Create the rendered resources using apply |
| 89 |
community.okd.k8s: |
| 90 |
namespace: default |
| 91 |
definition: '{{ item }}' |
| 92 |
wait: true |
| 93 |
apply: true |
| 94 |
loop: '{{ result.resources }}' |
| 95 |
|
| 96 |
- name: Process a template with parameters from an env file and create the resources |
| 97 |
community.okd.openshift_process: |
| 98 |
name: nginx-example |
| 99 |
namespace: openshift |
| 100 |
namespace_target: default |
| 101 |
parameter_file: 'files/nginx.env' |
| 102 |
state: present |
| 103 |
wait: true |
| 104 |
|
| 105 |
- name: Process a local template and create the resources |
| 106 |
community.okd.openshift_process: |
| 107 |
src: files/example-template.yaml |
| 108 |
parameter_file: files/example.env |
| 109 |
namespace_target: default |
| 110 |
state: present |
| 111 |
|
| 112 |
- name: Process a local template, delete the resources, and wait for them to terminate |
| 113 |
community.okd.openshift_process: |
| 114 |
src: files/example-template.yaml |
| 115 |
parameter_file: files/example.env |
| 116 |
namespace_target: default |
| 117 |
state: absent |
| 118 |
wait: true |
| 119 |
""" |
| 120 |
|
| 121 |
RETURN = r""" |
| 122 |
result: |
| 123 |
description: |
| 124 |
- The created, patched, or otherwise present object. Will be empty in the case of a deletion. |
| 125 |
returned: on success when state is present or absent |
| 126 |
type: complex |
| 127 |
contains: |
| 128 |
apiVersion: |
| 129 |
description: The versioned schema of this representation of an object. |
| 130 |
returned: success |
| 131 |
type: str |
| 132 |
kind: |
| 133 |
description: Represents the REST resource this object represents. |
| 134 |
returned: success |
| 135 |
type: str |
| 136 |
metadata: |
| 137 |
description: Standard object metadata. Includes name, namespace, annotations, labels, etc. |
| 138 |
returned: success |
| 139 |
type: complex |
| 140 |
contains: |
| 141 |
name: |
| 142 |
description: The name of the resource |
| 143 |
type: str |
| 144 |
namespace: |
| 145 |
description: The namespace of the resource |
| 146 |
type: str |
| 147 |
spec: |
| 148 |
description: Specific attributes of the object. Will vary based on the I(api_version) and I(kind). |
| 149 |
returned: success |
| 150 |
type: dict |
| 151 |
status: |
| 152 |
description: Current status details for the object. |
| 153 |
returned: success |
| 154 |
type: complex |
| 155 |
contains: |
| 156 |
conditions: |
| 157 |
type: complex |
| 158 |
description: Array of status conditions for the object. Not guaranteed to be present |
| 159 |
items: |
| 160 |
description: Returned only when multiple yaml documents are passed to src or resource_definition |
| 161 |
returned: when resource_definition or src contains list of objects |
| 162 |
type: list |
| 163 |
duration: |
| 164 |
description: elapsed time of task in seconds |
| 165 |
returned: when C(wait) is true |
| 166 |
type: int |
| 167 |
sample: 48 |
| 168 |
resources: |
| 169 |
type: complex |
| 170 |
description: |
| 171 |
- The rendered resources defined in the Template |
| 172 |
returned: on success when state is rendered |
| 173 |
contains: |
| 174 |
apiVersion: |
| 175 |
description: The versioned schema of this representation of an object. |
| 176 |
returned: success |
| 177 |
type: str |
| 178 |
kind: |
| 179 |
description: Represents the REST resource this object represents. |
| 180 |
returned: success |
| 181 |
type: str |
| 182 |
metadata: |
| 183 |
description: Standard object metadata. Includes name, namespace, annotations, labels, etc. |
| 184 |
returned: success |
| 185 |
type: complex |
| 186 |
contains: |
| 187 |
name: |
| 188 |
description: The name of the resource |
| 189 |
type: str |
| 190 |
namespace: |
| 191 |
description: The namespace of the resource |
| 192 |
type: str |
| 193 |
spec: |
| 194 |
description: Specific attributes of the object. Will vary based on the I(api_version) and I(kind). |
| 195 |
returned: success |
| 196 |
type: dict |
| 197 |
status: |
| 198 |
description: Current status details for the object. |
| 199 |
returned: success |
| 200 |
type: dict |
| 201 |
contains: |
| 202 |
conditions: |
| 203 |
type: complex |
| 204 |
description: Array of status conditions for the object. Not guaranteed to be present |
| 205 |
""" |
| 206 |
# ENDREMOVE (downstream) |
| 207 |
|
| 208 |
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( |
| 209 |
AUTH_ARG_SPEC, |
| 210 |
RESOURCE_ARG_SPEC, |
| 211 |
WAIT_ARG_SPEC, |
| 212 |
) |
| 213 |
|
| 214 |
|
| 215 |
def argspec(): |
| 216 |
argument_spec = {} |
| 217 |
argument_spec.update(AUTH_ARG_SPEC) |
| 218 |
argument_spec.update(WAIT_ARG_SPEC) |
| 219 |
argument_spec.update(RESOURCE_ARG_SPEC) |
| 220 |
argument_spec["state"] = dict( |
| 221 |
type="str", default="rendered", choices=["present", "absent", "rendered"] |
| 222 |
) |
| 223 |
argument_spec["namespace"] = dict(type="str") |
| 224 |
argument_spec["namespace_target"] = dict(type="str") |
| 225 |
argument_spec["parameters"] = dict(type="dict") |
| 226 |
argument_spec["name"] = dict(type="str") |
| 227 |
argument_spec["parameter_file"] = dict(type="str") |
| 228 |
|
| 229 |
return argument_spec |
| 230 |
|
| 231 |
|
| 232 |
def main(): |
| 233 |
from ansible_collections.community.okd.plugins.module_utils.openshift_process import ( |
| 234 |
OpenShiftProcess, |
| 235 |
) |
| 236 |
|
| 237 |
module = OpenShiftProcess(argument_spec=argspec(), supports_check_mode=True) |
| 238 |
module.run_module() |
| 239 |
|
| 240 |
|
| 241 |
if __name__ == "__main__": |
| 242 |
main() |