Debian Codegraph @

ansible_collections/kubernetes/core/docs/kubernetes.core.kubectl_connection.rst

raw
1 .. _kubernetes.core.kubectl_connection:
2
3
4 ***********************
5 kubernetes.core.kubectl
6 ***********************
7
8 **Execute tasks in pods running on Kubernetes.**
9
10
11
12 .. contents::
13 :local:
14 :depth: 1
15
16
17 Synopsis
18 --------
19 - Use the kubectl exec command to run tasks in, or put/fetch files to, pods running on the Kubernetes container platform.
20
21
22
23 Requirements
24 ------------
25 The below requirements are needed on the local Ansible controller node that executes this connection.
26
27 - kubectl (go binary)
28
29
30 Parameters
31 ----------
32
33 .. raw:: html
34
35 <table border=0 cellpadding=0 class="documentation-table">
36 <tr>
37 <th colspan="1">Parameter</th>
38 <th>Choices/<font color="blue">Defaults</font></th>
39 <th>Configuration</th>
40 <th width="100%">Comments</th>
41 </tr>
42 <tr>
43 <td colspan="1">
44 <div class="ansibleOptionAnchor" id="parameter-"></div>
45 <b>ca_cert</b>
46 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
47 <div style="font-size: small">
48 <span style="color: purple">-</span>
49 </div>
50 </td>
51 <td>
52 <b>Default:</b><br/><div style="color: blue">""</div>
53 </td>
54 <td>
55 <div>env:K8S_AUTH_SSL_CA_CERT</div>
56 <div>var: ansible_kubectl_ssl_ca_cert</div>
57 <div>var: ansible_kubectl_ca_cert</div>
58 </td>
59 <td>
60 <div>Path to a CA certificate used to authenticate with the API.</div>
61 <div style="font-size: small; color: darkgreen"><br/>aliases: kubectl_ssl_ca_cert</div>
62 </td>
63 </tr>
64 <tr>
65 <td colspan="1">
66 <div class="ansibleOptionAnchor" id="parameter-"></div>
67 <b>client_cert</b>
68 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
69 <div style="font-size: small">
70 <span style="color: purple">-</span>
71 </div>
72 </td>
73 <td>
74 <b>Default:</b><br/><div style="color: blue">""</div>
75 </td>
76 <td>
77 <div>env:K8S_AUTH_CERT_FILE</div>
78 <div>var: ansible_kubectl_cert_file</div>
79 <div>var: ansible_kubectl_client_cert</div>
80 </td>
81 <td>
82 <div>Path to a certificate used to authenticate with the API.</div>
83 <div style="font-size: small; color: darkgreen"><br/>aliases: kubectl_cert_file</div>
84 </td>
85 </tr>
86 <tr>
87 <td colspan="1">
88 <div class="ansibleOptionAnchor" id="parameter-"></div>
89 <b>client_key</b>
90 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
91 <div style="font-size: small">
92 <span style="color: purple">-</span>
93 </div>
94 </td>
95 <td>
96 <b>Default:</b><br/><div style="color: blue">""</div>
97 </td>
98 <td>
99 <div>env:K8S_AUTH_KEY_FILE</div>
100 <div>var: ansible_kubectl_key_file</div>
101 <div>var: ansible_kubectl_client_key</div>
102 </td>
103 <td>
104 <div>Path to a key file used to authenticate with the API.</div>
105 <div style="font-size: small; color: darkgreen"><br/>aliases: kubectl_key_file</div>
106 </td>
107 </tr>
108 <tr>
109 <td colspan="1">
110 <div class="ansibleOptionAnchor" id="parameter-"></div>
111 <b>kubectl_container</b>
112 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
113 <div style="font-size: small">
114 <span style="color: purple">-</span>
115 </div>
116 </td>
117 <td>
118 <b>Default:</b><br/><div style="color: blue">""</div>
119 </td>
120 <td>
121 <div>env:K8S_AUTH_CONTAINER</div>
122 <div>var: ansible_kubectl_container</div>
123 </td>
124 <td>
125 <div>Container name.</div>
126 <div>Required when a pod contains more than one container.</div>
127 </td>
128 </tr>
129 <tr>
130 <td colspan="1">
131 <div class="ansibleOptionAnchor" id="parameter-"></div>
132 <b>kubectl_context</b>
133 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
134 <div style="font-size: small">
135 <span style="color: purple">-</span>
136 </div>
137 </td>
138 <td>
139 <b>Default:</b><br/><div style="color: blue">""</div>
140 </td>
141 <td>
142 <div>env:K8S_AUTH_CONTEXT</div>
143 <div>var: ansible_kubectl_context</div>
144 </td>
145 <td>
146 <div>The name of a context found in the K8s config file.</div>
147 </td>
148 </tr>
149 <tr>
150 <td colspan="1">
151 <div class="ansibleOptionAnchor" id="parameter-"></div>
152 <b>kubectl_extra_args</b>
153 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
154 <div style="font-size: small">
155 <span style="color: purple">-</span>
156 </div>
157 </td>
158 <td>
159 <b>Default:</b><br/><div style="color: blue">""</div>
160 </td>
161 <td>
162 <div>env:K8S_AUTH_EXTRA_ARGS</div>
163 <div>var: ansible_kubectl_extra_args</div>
164 </td>
165 <td>
166 <div>Extra arguments to pass to the kubectl command line.</div>
167 <div>Please be aware that this passes information directly on the command line and it could expose sensitive data.</div>
168 </td>
169 </tr>
170 <tr>
171 <td colspan="1">
172 <div class="ansibleOptionAnchor" id="parameter-"></div>
173 <b>kubectl_host</b>
174 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
175 <div style="font-size: small">
176 <span style="color: purple">-</span>
177 </div>
178 </td>
179 <td>
180 <b>Default:</b><br/><div style="color: blue">""</div>
181 </td>
182 <td>
183 <div>env:K8S_AUTH_HOST</div>
184 <div>env:K8S_AUTH_SERVER</div>
185 <div>var: ansible_kubectl_host</div>
186 <div>var: ansible_kubectl_server</div>
187 </td>
188 <td>
189 <div>URL for accessing the API.</div>
190 </td>
191 </tr>
192 <tr>
193 <td colspan="1">
194 <div class="ansibleOptionAnchor" id="parameter-"></div>
195 <b>kubectl_kubeconfig</b>
196 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
197 <div style="font-size: small">
198 <span style="color: purple">-</span>
199 </div>
200 </td>
201 <td>
202 <b>Default:</b><br/><div style="color: blue">""</div>
203 </td>
204 <td>
205 <div>env:K8S_AUTH_KUBECONFIG</div>
206 <div>var: ansible_kubectl_kubeconfig</div>
207 <div>var: ansible_kubectl_config</div>
208 </td>
209 <td>
210 <div>Path to a kubectl config file. Defaults to <em>~/.kube/config</em></div>
211 <div>The configuration can be provided as dictionary. Added in version 2.4.0.</div>
212 </td>
213 </tr>
214 <tr>
215 <td colspan="1">
216 <div class="ansibleOptionAnchor" id="parameter-"></div>
217 <b>kubectl_local_env_vars</b>
218 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
219 <div style="font-size: small">
220 <span style="color: purple">dictionary</span>
221 </div>
222 <div style="font-style: italic; font-size: small; color: darkgreen">added in 3.1.0</div>
223 </td>
224 <td>
225 <b>Default:</b><br/><div style="color: blue">{}</div>
226 </td>
227 <td>
228 <div>var: ansible_kubectl_local_env_vars</div>
229 </td>
230 <td>
231 <div>Local enviromantal variable to be passed locally to the kubectl command line.</div>
232 <div>Please be aware that this passes information directly on the command line and it could expose sensitive data.</div>
233 </td>
234 </tr>
235 <tr>
236 <td colspan="1">
237 <div class="ansibleOptionAnchor" id="parameter-"></div>
238 <b>kubectl_namespace</b>
239 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
240 <div style="font-size: small">
241 <span style="color: purple">-</span>
242 </div>
243 </td>
244 <td>
245 <b>Default:</b><br/><div style="color: blue">""</div>
246 </td>
247 <td>
248 <div>env:K8S_AUTH_NAMESPACE</div>
249 <div>var: ansible_kubectl_namespace</div>
250 </td>
251 <td>
252 <div>The namespace of the pod</div>
253 </td>
254 </tr>
255 <tr>
256 <td colspan="1">
257 <div class="ansibleOptionAnchor" id="parameter-"></div>
258 <b>kubectl_password</b>
259 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
260 <div style="font-size: small">
261 <span style="color: purple">-</span>
262 </div>
263 </td>
264 <td>
265 <b>Default:</b><br/><div style="color: blue">""</div>
266 </td>
267 <td>
268 <div>env:K8S_AUTH_PASSWORD</div>
269 <div>var: ansible_kubectl_password</div>
270 </td>
271 <td>
272 <div>Provide a password for authenticating with the API.</div>
273 <div>Please be aware that this passes information directly on the command line and it could expose sensitive data. We recommend using the file based authentication options instead.</div>
274 </td>
275 </tr>
276 <tr>
277 <td colspan="1">
278 <div class="ansibleOptionAnchor" id="parameter-"></div>
279 <b>kubectl_pod</b>
280 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
281 <div style="font-size: small">
282 <span style="color: purple">-</span>
283 </div>
284 </td>
285 <td>
286 <b>Default:</b><br/><div style="color: blue">""</div>
287 </td>
288 <td>
289 <div>env:K8S_AUTH_POD</div>
290 <div>var: ansible_kubectl_pod</div>
291 </td>
292 <td>
293 <div>Pod name.</div>
294 <div>Required when the host name does not match pod name.</div>
295 </td>
296 </tr>
297 <tr>
298 <td colspan="1">
299 <div class="ansibleOptionAnchor" id="parameter-"></div>
300 <b>kubectl_token</b>
301 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
302 <div style="font-size: small">
303 <span style="color: purple">-</span>
304 </div>
305 </td>
306 <td>
307 </td>
308 <td>
309 <div>env:K8S_AUTH_TOKEN</div>
310 <div>env:K8S_AUTH_API_KEY</div>
311 <div>var: ansible_kubectl_token</div>
312 <div>var: ansible_kubectl_api_key</div>
313 </td>
314 <td>
315 <div>API authentication bearer token.</div>
316 <div>Please be aware that this passes information directly on the command line and it could expose sensitive data. We recommend using the file based authentication options instead.</div>
317 </td>
318 </tr>
319 <tr>
320 <td colspan="1">
321 <div class="ansibleOptionAnchor" id="parameter-"></div>
322 <b>kubectl_username</b>
323 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
324 <div style="font-size: small">
325 <span style="color: purple">-</span>
326 </div>
327 </td>
328 <td>
329 <b>Default:</b><br/><div style="color: blue">""</div>
330 </td>
331 <td>
332 <div>env:K8S_AUTH_USERNAME</div>
333 <div>var: ansible_kubectl_username</div>
334 <div>var: ansible_kubectl_user</div>
335 </td>
336 <td>
337 <div>Provide a username for authenticating with the API.</div>
338 </td>
339 </tr>
340 <tr>
341 <td colspan="1">
342 <div class="ansibleOptionAnchor" id="parameter-"></div>
343 <b>validate_certs</b>
344 <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a>
345 <div style="font-size: small">
346 <span style="color: purple">-</span>
347 </div>
348 </td>
349 <td>
350 <b>Default:</b><br/><div style="color: blue">""</div>
351 </td>
352 <td>
353 <div>env:K8S_AUTH_VERIFY_SSL</div>
354 <div>var: ansible_kubectl_verify_ssl</div>
355 <div>var: ansible_kubectl_validate_certs</div>
356 </td>
357 <td>
358 <div>Whether or not to verify the API server&#x27;s SSL certificate. Defaults to <em>true</em>.</div>
359 <div style="font-size: small; color: darkgreen"><br/>aliases: kubectl_verify_ssl</div>
360 </td>
361 </tr>
362 </table>
363 <br/>
364
365
366
367
368 Examples
369 --------
370
371 .. code-block:: yaml
372
373 - name: Run a command in a pod using local kubectl with kubeconfig file ~/.kube/config
374 hosts: localhost
375 gather_facts: no
376 vars:
377 ansible_connection: kubernetes.core.kubectl
378 ansible_kubectl_namespace: my-namespace
379 ansible_kubectl_pod: my-pod
380 ansible_kubectl_container: my-container
381 tasks:
382 # be aware that the command is executed as the user that started the container
383 # and requires python to be installed in the image
384 - name: Run a command in a pod
385 ansible.builtin.command: echo "Hello, World!"
386
387 - name: Run a command in a pod using local kubectl with inventory variables
388 # Example inventory:
389 # k8s:
390 # hosts:
391 # foo.example.com:
392 # ansible_connection: kubernetes.core.kubectl
393 # ansible_kubectl_kubeconfig: /root/.kube/foo.example.com.config
394 # ansible_kubectl_pod: my-foo-pod
395 # ansible_kubectl_container: my-foo-container
396 # ansible_kubectl_namespace: my-foo-namespace
397 # bar.example.com:
398 # ansible_connection: kubernetes.core.kubectl
399 # ansible_kubectl_kubeconfig: /root/.kube/bar.example.com.config
400 # ansible_kubectl_pod: my-bar-pod
401 # ansible_kubectl_container: my-bar-container
402 # ansible_kubectl_namespace: my-bar-namespace
403 hosts: k8s
404 gather_facts: no
405 tasks:
406 # be aware that the command is executed as the user that started the container
407 # and requires python to be installed in the image
408 - name: Run a command in a pod
409 ansible.builtin.command: echo "Hello, World!"
410
411 - name: Run a command in a pod using dynamic inventory
412 hosts: localhost
413 gather_facts: no
414 vars:
415 kubeconfig: /root/.kube/config
416 namespace: my-namespace
417 my_app: my-app
418 tasks:
419 - name: Get My App pod info based on label
420 kubernetes.core.k8s_info:
421 kubeconfig: "{{ kubeconfig }}"
422 namespace: "{{ namespace }}"
423 kind: Pod
424 label_selectors: app.kubernetes.io/name = "{{ my_app }}"
425 register: my_app_pod
426
427 - name: Get My App pod name
428 ansible.builtin.set_fact:
429 my_app_pod_name: "{{ my_app_pod.resources[0].metadata.name }}"
430
431 - name: Add My App pod to inventory
432 ansible.builtin.add_host:
433 name: "{{ my_app_pod_name }}"
434 ansible_connection: kubernetes.core.kubectl
435 ansible_kubectl_kubeconfig: "{{ kubeconfig }}"
436 ansible_kubectl_pod: "{{ my_app_pod_name }}"
437 ansible_kubectl_namespace: "{{ namespace }}"
438
439 - name: Run a command in My App pod
440 # be aware that the command is executed as the user that started the container
441 # and requires python to be installed in the image
442 ansible.builtin.command: echo "Hello, World!"
443 delegate_to: "{{ my_app_pod_name }}"
444
445
446
447
448 Status
449 ------
450
451
452 Authors
453 ~~~~~~~
454
455 - xuxinkun (@xuxinkun)
456
457
458 .. hint::
459 Configuration entries for each entry type have a low to high priority order. For example, a variable that is lower in the list will override a variable that is higher up.