| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
|
| 14 |
module: k8s_exec |
| 15 |
|
| 16 |
short_description: Execute command in Pod |
| 17 |
|
| 18 |
version_added: 0.10.0 |
| 19 |
|
| 20 |
author: "Tristan de Cacqueray (@tristanC)" |
| 21 |
|
| 22 |
description: |
| 23 |
- Use the Kubernetes Python client to execute command on K8s pods. |
| 24 |
|
| 25 |
extends_documentation_fragment: |
| 26 |
- kubernetes.core.k8s_auth_options |
| 27 |
|
| 28 |
requirements: |
| 29 |
- "python >= 3.9" |
| 30 |
- "kubernetes >= 24.2.0" |
| 31 |
- "PyYAML >= 3.11" |
| 32 |
|
| 33 |
notes: |
| 34 |
- Return code C(rc) for the command executed is added in output in version 2.2.0, and deprecates return code C(return_code). |
| 35 |
- Return code C(return_code) for the command executed is added in output in version 1.0.0. |
| 36 |
- The authenticated user must have at least read access to the pods resource and write access to the pods/exec resource. |
| 37 |
|
| 38 |
options: |
| 39 |
proxy: |
| 40 |
description: |
| 41 |
- The URL of an HTTP proxy to use for the connection. |
| 42 |
- Can also be specified via I(K8S_AUTH_PROXY) environment variable. |
| 43 |
- Please note that this module does not pick up typical proxy settings from the environment (for example, HTTP_PROXY). |
| 44 |
type: str |
| 45 |
namespace: |
| 46 |
description: |
| 47 |
- The pod namespace name. |
| 48 |
type: str |
| 49 |
required: yes |
| 50 |
pod: |
| 51 |
description: |
| 52 |
- The pod name. |
| 53 |
type: str |
| 54 |
required: yes |
| 55 |
container: |
| 56 |
description: |
| 57 |
- The name of the container in the pod to connect to. |
| 58 |
- Defaults to only container if there is only one container in the pod. |
| 59 |
- If not specified, will choose the first container from the given pod as kubectl cmdline does. |
| 60 |
type: str |
| 61 |
required: no |
| 62 |
command: |
| 63 |
description: |
| 64 |
- The command to execute. |
| 65 |
type: str |
| 66 |
required: yes |
| 67 |
""" |
| 68 |
|
| 69 |
EXAMPLES = r""" |
| 70 |
- name: Execute a command |
| 71 |
kubernetes.core.k8s_exec: |
| 72 |
namespace: myproject |
| 73 |
pod: zuul-scheduler |
| 74 |
command: zuul-scheduler full-reconfigure |
| 75 |
|
| 76 |
- name: Check RC status of command executed |
| 77 |
kubernetes.core.k8s_exec: |
| 78 |
namespace: myproject |
| 79 |
pod: busybox-test |
| 80 |
command: cmd_with_non_zero_exit_code |
| 81 |
register: command_status |
| 82 |
ignore_errors: True |
| 83 |
|
| 84 |
- name: Check last command status |
| 85 |
debug: |
| 86 |
msg: "cmd failed" |
| 87 |
when: command_status.rc != 0 |
| 88 |
|
| 89 |
- name: Specify a container name to execute the command on |
| 90 |
kubernetes.core.k8s_exec: |
| 91 |
namespace: myproject |
| 92 |
pod: busybox-test |
| 93 |
container: manager |
| 94 |
command: echo "hello" |
| 95 |
""" |
| 96 |
|
| 97 |
RETURN = r""" |
| 98 |
result: |
| 99 |
description: |
| 100 |
- The command object |
| 101 |
returned: success |
| 102 |
type: complex |
| 103 |
contains: |
| 104 |
stdout: |
| 105 |
description: The command stdout |
| 106 |
type: str |
| 107 |
stdout_lines: |
| 108 |
description: The command stdout |
| 109 |
type: str |
| 110 |
stderr: |
| 111 |
description: The command stderr |
| 112 |
type: str |
| 113 |
stderr_lines: |
| 114 |
description: The command stderr |
| 115 |
type: str |
| 116 |
rc: |
| 117 |
description: The command status code |
| 118 |
type: int |
| 119 |
version_added: 2.2.0 |
| 120 |
return_code: |
| 121 |
description: The command status code. This attribute is deprecated and will be removed in a future release. Please use rc instead. |
| 122 |
type: int |
| 123 |
""" |
| 124 |
|
| 125 |
import copy |
| 126 |
import shlex |
| 127 |
|
| 128 |
try: |
| 129 |
import yaml |
| 130 |
except ImportError: |
| 131 |
|
| 132 |
pass |
| 133 |
|
| 134 |
from ansible.module_utils.common.text.converters import to_native |
| 135 |
from ansible_collections.kubernetes.core.plugins.module_utils.ansiblemodule import ( |
| 136 |
AnsibleModule, |
| 137 |
) |
| 138 |
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( |
| 139 |
AUTH_ARG_SPEC, |
| 140 |
) |
| 141 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.client import ( |
| 142 |
get_api_client, |
| 143 |
) |
| 144 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.core import ( |
| 145 |
AnsibleK8SModule, |
| 146 |
) |
| 147 |
from ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions import ( |
| 148 |
CoreException, |
| 149 |
) |
| 150 |
|
| 151 |
try: |
| 152 |
from kubernetes.client.apis import core_v1_api |
| 153 |
from kubernetes.client.exceptions import ApiException |
| 154 |
from kubernetes.stream import stream |
| 155 |
except ImportError: |
| 156 |
|
| 157 |
pass |
| 158 |
|
| 159 |
|
| 160 |
def argspec(): |
| 161 |
spec = copy.deepcopy(AUTH_ARG_SPEC) |
| 162 |
spec["namespace"] = dict(type="str", required=True) |
| 163 |
spec["pod"] = dict(type="str", required=True) |
| 164 |
spec["container"] = dict(type="str") |
| 165 |
spec["command"] = dict(type="str", required=True) |
| 166 |
return spec |
| 167 |
|
| 168 |
|
| 169 |
def execute_module(module, client): |
| 170 |
|
| 171 |
api = core_v1_api.CoreV1Api(client.client) |
| 172 |
|
| 173 |
|
| 174 |
optional_kwargs = {} |
| 175 |
if module.params.get("container"): |
| 176 |
optional_kwargs["container"] = module.params["container"] |
| 177 |
else: |
| 178 |
|
| 179 |
resp = None |
| 180 |
try: |
| 181 |
resp = api.read_namespaced_pod( |
| 182 |
name=module.params["pod"], namespace=module.params["namespace"] |
| 183 |
) |
| 184 |
except ApiException: |
| 185 |
pass |
| 186 |
|
| 187 |
if resp and len(resp.spec.containers) >= 1: |
| 188 |
optional_kwargs["container"] = resp.spec.containers[0].name |
| 189 |
|
| 190 |
try: |
| 191 |
resp = stream( |
| 192 |
api.connect_get_namespaced_pod_exec, |
| 193 |
module.params["pod"], |
| 194 |
module.params["namespace"], |
| 195 |
command=shlex.split(module.params["command"]), |
| 196 |
stdout=True, |
| 197 |
stderr=True, |
| 198 |
stdin=False, |
| 199 |
tty=False, |
| 200 |
_preload_content=False, |
| 201 |
**optional_kwargs |
| 202 |
) |
| 203 |
except Exception as e: |
| 204 |
module.fail_json( |
| 205 |
msg="Failed to execute on pod %s" |
| 206 |
" due to : %s" % (module.params.get("pod"), to_native(e)) |
| 207 |
) |
| 208 |
stdout, stderr, rc = [], [], 0 |
| 209 |
while resp.is_open(): |
| 210 |
resp.update(timeout=1) |
| 211 |
if resp.peek_stdout(): |
| 212 |
stdout.append(resp.read_stdout()) |
| 213 |
if resp.peek_stderr(): |
| 214 |
stderr.append(resp.read_stderr()) |
| 215 |
err = resp.read_channel(3) |
| 216 |
err = yaml.safe_load(err) |
| 217 |
if err["status"] == "Success": |
| 218 |
rc = 0 |
| 219 |
else: |
| 220 |
rc = int(err["details"]["causes"][0]["message"]) |
| 221 |
|
| 222 |
module.exit_json( |
| 223 |
|
| 224 |
changed=True, |
| 225 |
stdout="".join(stdout), |
| 226 |
stderr="".join(stderr), |
| 227 |
rc=rc, |
| 228 |
return_code=rc, |
| 229 |
) |
| 230 |
|
| 231 |
|
| 232 |
def main(): |
| 233 |
module = AnsibleK8SModule( |
| 234 |
module_class=AnsibleModule, |
| 235 |
check_pyyaml=False, |
| 236 |
argument_spec=argspec(), |
| 237 |
supports_check_mode=True, |
| 238 |
) |
| 239 |
|
| 240 |
try: |
| 241 |
client = get_api_client(module) |
| 242 |
execute_module(module, client.client) |
| 243 |
except CoreException as e: |
| 244 |
module.fail_from_exception(e) |
| 245 |
|
| 246 |
|
| 247 |
if __name__ == "__main__": |
| 248 |
main() |