| 1 |
.. _kubernetes.core.k8s_lookup: |
| 2 |
|
| 3 |
|
| 4 |
******************* |
| 5 |
kubernetes.core.k8s |
| 6 |
******************* |
| 7 |
|
| 8 |
**Query the K8s API** |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
.. contents:: |
| 13 |
:local: |
| 14 |
:depth: 1 |
| 15 |
|
| 16 |
|
| 17 |
Synopsis |
| 18 |
-------- |
| 19 |
- Uses the Kubernetes Python client to fetch a specific object by name, all matching objects within a namespace, or all matching objects for all namespaces, as well as information about the cluster. |
| 20 |
- Provides access the full range of K8s APIs. |
| 21 |
- Enables authentication via config file, certificates, password or token. |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
Requirements |
| 26 |
------------ |
| 27 |
The below requirements are needed on the local Ansible controller node that executes this lookup. |
| 28 |
|
| 29 |
- python >= 3.9 |
| 30 |
- kubernetes >= 24.2.0 |
| 31 |
- PyYAML >= 3.11 |
| 32 |
|
| 33 |
|
| 34 |
Parameters |
| 35 |
---------- |
| 36 |
|
| 37 |
.. raw:: html |
| 38 |
|
| 39 |
<table border=0 cellpadding=0 class="documentation-table"> |
| 40 |
<tr> |
| 41 |
<th colspan="1">Parameter</th> |
| 42 |
<th>Choices/<font color="blue">Defaults</font></th> |
| 43 |
<th>Configuration</th> |
| 44 |
<th width="100%">Comments</th> |
| 45 |
</tr> |
| 46 |
<tr> |
| 47 |
<td colspan="1"> |
| 48 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 49 |
<b>api_key</b> |
| 50 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 51 |
<div style="font-size: small"> |
| 52 |
<span style="color: purple">-</span> |
| 53 |
</div> |
| 54 |
</td> |
| 55 |
<td> |
| 56 |
</td> |
| 57 |
<td> |
| 58 |
</td> |
| 59 |
<td> |
| 60 |
<div>Token used to authenticate with the API. Can also be specified via K8S_AUTH_API_KEY environment variable.</div> |
| 61 |
</td> |
| 62 |
</tr> |
| 63 |
<tr> |
| 64 |
<td colspan="1"> |
| 65 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 66 |
<b>api_version</b> |
| 67 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 68 |
<div style="font-size: small"> |
| 69 |
<span style="color: purple">-</span> |
| 70 |
</div> |
| 71 |
</td> |
| 72 |
<td> |
| 73 |
<b>Default:</b><br/><div style="color: blue">"v1"</div> |
| 74 |
</td> |
| 75 |
<td> |
| 76 |
</td> |
| 77 |
<td> |
| 78 |
<div>Use to specify the API version. If <em>resource definition</em> is provided, the <em>apiVersion</em> from the <em>resource_definition</em> will override this option.</div> |
| 79 |
</td> |
| 80 |
</tr> |
| 81 |
<tr> |
| 82 |
<td colspan="1"> |
| 83 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 84 |
<b>ca_cert</b> |
| 85 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 86 |
<div style="font-size: small"> |
| 87 |
<span style="color: purple">-</span> |
| 88 |
</div> |
| 89 |
</td> |
| 90 |
<td> |
| 91 |
</td> |
| 92 |
<td> |
| 93 |
</td> |
| 94 |
<td> |
| 95 |
<div>Path to a CA certificate used to authenticate with the API. Can also be specified via K8S_AUTH_SSL_CA_CERT environment variable.</div> |
| 96 |
<div style="font-size: small; color: darkgreen"><br/>aliases: ssl_ca_cert</div> |
| 97 |
</td> |
| 98 |
</tr> |
| 99 |
<tr> |
| 100 |
<td colspan="1"> |
| 101 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 102 |
<b>client_cert</b> |
| 103 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 104 |
<div style="font-size: small"> |
| 105 |
<span style="color: purple">-</span> |
| 106 |
</div> |
| 107 |
</td> |
| 108 |
<td> |
| 109 |
</td> |
| 110 |
<td> |
| 111 |
</td> |
| 112 |
<td> |
| 113 |
<div>Path to a certificate used to authenticate with the API. Can also be specified via K8S_AUTH_CERT_FILE environment variable.</div> |
| 114 |
<div style="font-size: small; color: darkgreen"><br/>aliases: cert_file</div> |
| 115 |
</td> |
| 116 |
</tr> |
| 117 |
<tr> |
| 118 |
<td colspan="1"> |
| 119 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 120 |
<b>client_key</b> |
| 121 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 122 |
<div style="font-size: small"> |
| 123 |
<span style="color: purple">-</span> |
| 124 |
</div> |
| 125 |
</td> |
| 126 |
<td> |
| 127 |
</td> |
| 128 |
<td> |
| 129 |
</td> |
| 130 |
<td> |
| 131 |
<div>Path to a key file used to authenticate with the API. Can also be specified via K8S_AUTH_KEY_FILE environment variable.</div> |
| 132 |
<div style="font-size: small; color: darkgreen"><br/>aliases: key_file</div> |
| 133 |
</td> |
| 134 |
</tr> |
| 135 |
<tr> |
| 136 |
<td colspan="1"> |
| 137 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 138 |
<b>cluster_info</b> |
| 139 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 140 |
<div style="font-size: small"> |
| 141 |
<span style="color: purple">-</span> |
| 142 |
</div> |
| 143 |
</td> |
| 144 |
<td> |
| 145 |
</td> |
| 146 |
<td> |
| 147 |
</td> |
| 148 |
<td> |
| 149 |
<div>Use to specify the type of cluster information you are attempting to retrieve. Will take priority over all the other options.</div> |
| 150 |
</td> |
| 151 |
</tr> |
| 152 |
<tr> |
| 153 |
<td colspan="1"> |
| 154 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 155 |
<b>context</b> |
| 156 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 157 |
<div style="font-size: small"> |
| 158 |
<span style="color: purple">-</span> |
| 159 |
</div> |
| 160 |
</td> |
| 161 |
<td> |
| 162 |
</td> |
| 163 |
<td> |
| 164 |
</td> |
| 165 |
<td> |
| 166 |
<div>The name of a context found in the config file. Can also be specified via K8S_AUTH_CONTEXT environment variable.</div> |
| 167 |
</td> |
| 168 |
</tr> |
| 169 |
<tr> |
| 170 |
<td colspan="1"> |
| 171 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 172 |
<b>field_selector</b> |
| 173 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 174 |
<div style="font-size: small"> |
| 175 |
<span style="color: purple">-</span> |
| 176 |
</div> |
| 177 |
</td> |
| 178 |
<td> |
| 179 |
</td> |
| 180 |
<td> |
| 181 |
</td> |
| 182 |
<td> |
| 183 |
<div>Specific fields on which to query. Ignored when <em>resource_name</em> is provided.</div> |
| 184 |
</td> |
| 185 |
</tr> |
| 186 |
<tr> |
| 187 |
<td colspan="1"> |
| 188 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 189 |
<b>host</b> |
| 190 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 191 |
<div style="font-size: small"> |
| 192 |
<span style="color: purple">-</span> |
| 193 |
</div> |
| 194 |
</td> |
| 195 |
<td> |
| 196 |
</td> |
| 197 |
<td> |
| 198 |
</td> |
| 199 |
<td> |
| 200 |
<div>Provide a URL for accessing the API. Can also be specified via K8S_AUTH_HOST environment variable.</div> |
| 201 |
</td> |
| 202 |
</tr> |
| 203 |
<tr> |
| 204 |
<td colspan="1"> |
| 205 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 206 |
<b>kind</b> |
| 207 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 208 |
<div style="font-size: small"> |
| 209 |
<span style="color: purple">-</span> |
| 210 |
/ <span style="color: red">required</span> |
| 211 |
</div> |
| 212 |
</td> |
| 213 |
<td> |
| 214 |
</td> |
| 215 |
<td> |
| 216 |
</td> |
| 217 |
<td> |
| 218 |
<div>Use to specify an object model. If <em>resource definition</em> is provided, the <em>kind</em> from a <em>resource_definition</em> will override this option.</div> |
| 219 |
</td> |
| 220 |
</tr> |
| 221 |
<tr> |
| 222 |
<td colspan="1"> |
| 223 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 224 |
<b>kubeconfig</b> |
| 225 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 226 |
<div style="font-size: small"> |
| 227 |
<span style="color: purple">-</span> |
| 228 |
</div> |
| 229 |
</td> |
| 230 |
<td> |
| 231 |
</td> |
| 232 |
<td> |
| 233 |
</td> |
| 234 |
<td> |
| 235 |
<div>Path to an existing Kubernetes config file. If not provided, and no other connection options are provided, the Kubernetes client will attempt to load the default configuration file from <em>~/.kube/config</em>. Can also be specified via K8S_AUTH_KUBECONFIG environment variable.</div> |
| 236 |
</td> |
| 237 |
</tr> |
| 238 |
<tr> |
| 239 |
<td colspan="1"> |
| 240 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 241 |
<b>label_selector</b> |
| 242 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 243 |
<div style="font-size: small"> |
| 244 |
<span style="color: purple">-</span> |
| 245 |
</div> |
| 246 |
</td> |
| 247 |
<td> |
| 248 |
</td> |
| 249 |
<td> |
| 250 |
</td> |
| 251 |
<td> |
| 252 |
<div>Additional labels to include in the query. Ignored when <em>resource_name</em> is provided.</div> |
| 253 |
</td> |
| 254 |
</tr> |
| 255 |
<tr> |
| 256 |
<td colspan="1"> |
| 257 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 258 |
<b>namespace</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 |
</td> |
| 266 |
<td> |
| 267 |
</td> |
| 268 |
<td> |
| 269 |
<div>Limit the objects returned to a specific namespace. If <em>resource definition</em> is provided, the <em>metadata.namespace</em> value from the <em>resource_definition</em> will override this option.</div> |
| 270 |
</td> |
| 271 |
</tr> |
| 272 |
<tr> |
| 273 |
<td colspan="1"> |
| 274 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 275 |
<b>password</b> |
| 276 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 277 |
<div style="font-size: small"> |
| 278 |
<span style="color: purple">-</span> |
| 279 |
</div> |
| 280 |
</td> |
| 281 |
<td> |
| 282 |
</td> |
| 283 |
<td> |
| 284 |
</td> |
| 285 |
<td> |
| 286 |
<div>Provide a password for authenticating with the API. Can also be specified via K8S_AUTH_PASSWORD environment variable.</div> |
| 287 |
</td> |
| 288 |
</tr> |
| 289 |
<tr> |
| 290 |
<td colspan="1"> |
| 291 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 292 |
<b>resource_definition</b> |
| 293 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 294 |
<div style="font-size: small"> |
| 295 |
<span style="color: purple">-</span> |
| 296 |
</div> |
| 297 |
</td> |
| 298 |
<td> |
| 299 |
</td> |
| 300 |
<td> |
| 301 |
</td> |
| 302 |
<td> |
| 303 |
<div>Provide a YAML configuration for an object. NOTE: <em>kind</em>, <em>api_version</em>, <em>resource_name</em>, and <em>namespace</em> will be overwritten by corresponding values found in the provided <em>resource_definition</em>.</div> |
| 304 |
</td> |
| 305 |
</tr> |
| 306 |
<tr> |
| 307 |
<td colspan="1"> |
| 308 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 309 |
<b>resource_name</b> |
| 310 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 311 |
<div style="font-size: small"> |
| 312 |
<span style="color: purple">-</span> |
| 313 |
</div> |
| 314 |
</td> |
| 315 |
<td> |
| 316 |
</td> |
| 317 |
<td> |
| 318 |
</td> |
| 319 |
<td> |
| 320 |
<div>Fetch a specific object by name. If <em>resource definition</em> is provided, the <em>metadata.name</em> value from the <em>resource_definition</em> will override this option.</div> |
| 321 |
</td> |
| 322 |
</tr> |
| 323 |
<tr> |
| 324 |
<td colspan="1"> |
| 325 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 326 |
<b>src</b> |
| 327 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 328 |
<div style="font-size: small"> |
| 329 |
<span style="color: purple">-</span> |
| 330 |
</div> |
| 331 |
</td> |
| 332 |
<td> |
| 333 |
</td> |
| 334 |
<td> |
| 335 |
</td> |
| 336 |
<td> |
| 337 |
<div>Provide a path to a file containing a valid YAML definition of an object dated. Mutually exclusive with <em>resource_definition</em>. NOTE: <em>kind</em>, <em>api_version</em>, <em>resource_name</em>, and <em>namespace</em> will be overwritten by corresponding values found in the configuration read in from the <em>src</em> file.</div> |
| 338 |
<div>Reads from the local file system. To read from the Ansible controller's file system, use the file lookup plugin or template lookup plugin, combined with the from_yaml filter, and pass the result to <em>resource_definition</em>. See Examples below.</div> |
| 339 |
</td> |
| 340 |
</tr> |
| 341 |
<tr> |
| 342 |
<td colspan="1"> |
| 343 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 344 |
<b>username</b> |
| 345 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 346 |
<div style="font-size: small"> |
| 347 |
<span style="color: purple">-</span> |
| 348 |
</div> |
| 349 |
</td> |
| 350 |
<td> |
| 351 |
</td> |
| 352 |
<td> |
| 353 |
</td> |
| 354 |
<td> |
| 355 |
<div>Provide a username for authenticating with the API. Can also be specified via K8S_AUTH_USERNAME environment variable.</div> |
| 356 |
</td> |
| 357 |
</tr> |
| 358 |
<tr> |
| 359 |
<td colspan="1"> |
| 360 |
<div class="ansibleOptionAnchor" id="parameter-"></div> |
| 361 |
<b>validate_certs</b> |
| 362 |
<a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> |
| 363 |
<div style="font-size: small"> |
| 364 |
<span style="color: purple">boolean</span> |
| 365 |
</div> |
| 366 |
</td> |
| 367 |
<td> |
| 368 |
<ul style="margin: 0; padding: 0"><b>Choices:</b> |
| 369 |
<li>no</li> |
| 370 |
<li>yes</li> |
| 371 |
</ul> |
| 372 |
</td> |
| 373 |
<td> |
| 374 |
</td> |
| 375 |
<td> |
| 376 |
<div>Whether or not to verify the API server's SSL certificates. Can also be specified via K8S_AUTH_VERIFY_SSL environment variable.</div> |
| 377 |
<div style="font-size: small; color: darkgreen"><br/>aliases: verify_ssl</div> |
| 378 |
</td> |
| 379 |
</tr> |
| 380 |
</table> |
| 381 |
<br/> |
| 382 |
|
| 383 |
|
| 384 |
Notes |
| 385 |
----- |
| 386 |
|
| 387 |
.. note:: |
| 388 |
- While querying, please use ``query`` or ``lookup`` format with ``wantlist=True`` to provide an easier and more consistent interface. For more details, see https://docs.ansible.com/ansible/latest/plugins/lookup.html#forcing-lookups-to-return-lists-query-and-wantlist-true. |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
Examples |
| 393 |
-------- |
| 394 |
|
| 395 |
.. code-block:: yaml |
| 396 |
|
| 397 |
- name: Fetch a list of namespaces |
| 398 |
set_fact: |
| 399 |
projects: "{{ query('kubernetes.core.k8s', api_version='v1', kind='Namespace') }}" |
| 400 |
|
| 401 |
- name: Fetch all deployments |
| 402 |
set_fact: |
| 403 |
deployments: "{{ query('kubernetes.core.k8s', kind='Deployment') }}" |
| 404 |
|
| 405 |
- name: Fetch all deployments in a namespace |
| 406 |
set_fact: |
| 407 |
deployments: "{{ query('kubernetes.core.k8s', kind='Deployment', namespace='testing') }}" |
| 408 |
|
| 409 |
- name: Fetch a specific deployment by name |
| 410 |
set_fact: |
| 411 |
deployments: "{{ query('kubernetes.core.k8s', kind='Deployment', namespace='testing', resource_name='elastic') }}" |
| 412 |
|
| 413 |
- name: Fetch with label selector |
| 414 |
set_fact: |
| 415 |
service: "{{ query('kubernetes.core.k8s', kind='Service', label_selector='app=galaxy') }}" |
| 416 |
|
| 417 |
# Use parameters from a YAML config |
| 418 |
|
| 419 |
- name: Load config from the Ansible controller filesystem |
| 420 |
set_fact: |
| 421 |
config: "{{ lookup('file', 'service.yml') | from_yaml }}" |
| 422 |
|
| 423 |
- name: Using the config (loaded from a file in prior task), fetch the latest version of the object |
| 424 |
set_fact: |
| 425 |
service: "{{ query('kubernetes.core.k8s', resource_definition=config) }}" |
| 426 |
|
| 427 |
- name: Use a config from the local filesystem |
| 428 |
set_fact: |
| 429 |
service: "{{ query('kubernetes.core.k8s', src='service.yml') }}" |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
Return Values |
| 434 |
------------- |
| 435 |
Common return values are documented `here <https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#common-return-values>`_, the following are the fields unique to this lookup: |
| 436 |
|
| 437 |
.. raw:: html |
| 438 |
|
| 439 |
<table border=0 cellpadding=0 class="documentation-table"> |
| 440 |
<tr> |
| 441 |
<th colspan="1">Key</th> |
| 442 |
<th>Returned</th> |
| 443 |
<th width="100%">Description</th> |
| 444 |
</tr> |
| 445 |
<tr> |
| 446 |
<td colspan="1"> |
| 447 |
<div class="ansibleOptionAnchor" id="return-"></div> |
| 448 |
<b>_list</b> |
| 449 |
<a class="ansibleOptionLink" href="#return-" title="Permalink to this return value"></a> |
| 450 |
<div style="font-size: small"> |
| 451 |
<span style="color: purple">list</span> |
| 452 |
/ <span style="color: purple">elements=dictionary</span> |
| 453 |
</div> |
| 454 |
</td> |
| 455 |
<td></td> |
| 456 |
<td> |
| 457 |
<div>One ore more object definitions returned from the API.</div> |
| 458 |
<br/> |
| 459 |
<div style="font-size: smaller"><b>Sample:</b></div> |
| 460 |
<div style="font-size: smaller; color: blue; word-wrap: break-word; word-break: break-all;">[{'kind': 'ConfigMap', 'apiVersion': 'v1', 'metadata': {'creationTimestamp': '2022-03-04T13:59:49Z', 'name': 'my-config-map', 'namespace': 'default', 'resourceVersion': '418', 'uid': '5714b011-d090-4eac-8272-a0ea82ec0abd'}, 'data': {'key1': 'val1'}}]</div> |
| 461 |
</td> |
| 462 |
</tr> |
| 463 |
</table> |
| 464 |
<br/><br/> |
| 465 |
|
| 466 |
|
| 467 |
Status |
| 468 |
------ |
| 469 |
|
| 470 |
|
| 471 |
Authors |
| 472 |
~~~~~~~ |
| 473 |
|
| 474 |
- Chris Houseknecht (@chouseknecht) |
| 475 |
- Fabian von Feilitzsch (@fabianvf) |
| 476 |
|
| 477 |
|
| 478 |
.. hint:: |
| 479 |
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. |